end ;this sub-procedure is required whenever you use the display-bits command to display-bits :d1 :d2 :d3 :d4 bsend $110 bsend 64 bsend low-byte :d1 bsend low-byte :d2 bsend low-byte :d3 bsend low-byte :d4 end
sendmessagetomicroworlds: This command will send a numerical message to the MicroWorlds program. For example, if you tell the Cricket "sendmessagetomicroworlds 1" the robot will send the number 1 to MicroWorlds.
messagefromrobot: This command reads the message sent from the robot. For example, if you tell MicroWorlds
if messagefromrobot = 1 [melody1]
MicroWorlds will play a melody file if it reads the number 1 from the robot. You would want to loop this, as in a button set to forever.
Until now you've had to write each new number you want the LED to display as it counts to 10. If you wanted it to count higher this would start to get tedious. To avoid the tedium you can use a variable. It is similar to a variable in algebra in that you use a name that stands for a value that can be changed. Here is how: global [number];this creates a variable and names it to count
setnumber 1;this sets the value of the variable
repeat 10 [
display number;this displays the current value of the variable
setnumber number + 1;this sets the value again, adding one to the current value
Tail recursionis another way to loop commands. With loop or repeat you give the command and then in brackets specify what is to be repeated. Tail recursion is simpler and a bit of a trick. Here's how: to count
end Bits are the individual bars that can light up in each of the four spaces. D1, d2, d3, and d4 stand for the four spaces. To make all of the bits turn off, you would send the command "display-bits $00 $00 $00 $00."
Applying the solution
If you only sent this command, the Cricket wouldn't know how to "display-bits," so here is an example of how you use this command. Notice that you have to include the "display-bits" procedure because it's acting as a sub-procedure to "count." The "count" procedure will make it count 1, 2, then go blank.
What is a conditional statement? It's when you tell your robot to do something only if a certain condition is true.If the condition is not true, the robot won't do it. We use conditional statements all the time in real life, like when we say, for example, "If they are serving fish for lunch, I'm going to the salad bar." In Logo, this would look something like this:
if fish [eat saladbar]
(Note: I don't have anything against fish.)
Your robot can't eat, though, so let's see what it could do as a result of a conditional statement. If you want your robot to start driving if you press a switch, this will do it:
[ab, onfor 20]
Notice where the brackets go! They enclose the 'if true' command like bread on a sandwich. If you want your robot to stop driving if you press a switch, this will do it:
ab, on if switcha
Well, actually, I kind of lied. The two examples above won't really work because they check if the switch is pressed only once. We need it to check continuously so it will carry out the commands whenever we press the switch. So we have to "loop" the procedure, like this:
[ab, onfor 20]
end This example will keep checking whether the switch is pressed because loop makes the part inside the brackets repeat indefinitely. Here's how you can use a switch to control your robot: Use a switch to start your robot driving on its road course. First plug a switch into sensor port 'a'. You can keep your program the way it is and just wrap a conditional statement around it.
loop [ if switcha
ab, thisway ab, on wait 20 etc, etc,
Now be sure to put the name 'start' in the 'run this' window so when you press the run button on the robot, it will start the loop instead of just skipping the loop and going straight to the 'drive' procedure.
Sub-procedures are separate procedures in your program that are called upon in the main procedure. Here is what one looks like:
ab, thisway ab, on wait 20 turnright;sub-procedure is being called upon here wait 30
to turnright;here is the sub-procedure
b, off wait 10 b, on
So "turnright" is a sub-procedure of "drive." "Drive" is the main procedure. In other programming languages a sub-procedure is called afunctionbut it basically works the same. The benefit of using a sub-procedure is that it makes your program much more efficient and can save you a lot of work. How will you use this in your program? In your procedures window, add a procedure called "turnright" and type in the commands that will make your car turn right. Do the same to add another procedure called "turnleft." In your main procedure every time there is a right turn, remove those commands and just type "turnright." Do the same for left turns. You will have to test your program to make sure the turnright and turnleft sub-procedures turn the correct amount, but all of the turns are right angles, so if it works once it should work for the rest.
The first procedure beeps, turns motors a and b on, waits 3 seconds, and turns motors a and b off. The second procedure beepsafterthe motors turn on and off. What is the rule the Cricket is following? The Cricket will follow the sequence of commands from the top of the procedure to the bottom.