Thursday, June 3, 2010

Displaying messages

;here is a simple word
to sayHi

display-bits $00 $00 $76 $06
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

Commands for Cricket-MicroWorlds communication

  • 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.

Basic Sender and Receiver Procedures

For communicating between crickets:


to send-it

send 1
wait 10
send-it
end

to receive-it
waituntil [newir?]
if ir = 1 [beep]
receive-it
end

Using a Variable to Count

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
wait 5
setnumber number + 1 ;this sets the value again, adding one to the current value
]
end

What is tail recursion?

Tail recursion is 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

display 1
wait 5
display 2
wait 5
display 3
wait 5
count
end

See? If you put the name of the procedure at the end of the procedure itself, it will just keep looping itself!

Two Ways to Loop

Two Ways to Loop

There are two commands that can make other commands repeat.
Loop makes the commands repeat indefinitely:

to beepForever
loop [beep wait 2]
end

Repeat makes the commands repeat as many times as you tell it to:

to beepFiveTimes
repeat 5 [beep wait 2]
end

What if you want the LED display to not display anything?

Problem

The LED display will automatically turn off after a little while if it's idle to conserve battery power. But how can you make it go blank right away?

Solution

You have to use a procedure called "display-bits." Here it is:
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

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.
to count
display 1
wait 5
display 2
wait 5
display-bits $00 $00 $00 $00
end

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

To Display Numbers

The command that makes the LED display numbers is "display".
Before the Cricket can access this command you will probably have to do the following:

  1. Make sure the procedures window ("Cricket Logo Programs") is empty.
  2. Click the download button to clear out the Cricket's memory.
  3. Now type "display 1234" in the command center and hit enter.
  4. If the LED display shows the number 1234 it is now working.

What is a conditional statement?

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:

to eat.lunch
if fish
[eat saladbar]
end
(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:

to drive
if switcha
[ab, onfor 20]
end
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:

to stop.driving
ab, on
if switcha
[ab, off]
end
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:

to drive

loop [
if switcha
[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.
to start
loop [
if switcha
[drive]
]
end

to drive
ab, thisway
ab, on
wait 20
etc, etc,
end

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.

Using Sub-procedures

Sub-procedures are separate procedures in your program that are called upon in the main procedure. Here is what one looks like:


to drive
ab, thisway
ab, on
wait 20
turnright ;sub-procedure is being called upon here
wait 30
end

to turnright ;here is the sub-procedure
b, off
wait 10
b, on
end

So "turnright" is a sub-procedure of "drive." "Drive" is the main procedure. In other programming languages a sub-procedure is called a function but 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.

Your Robot Follows Commands in a Sequence

How is this:

to drive
beep
ab, on
wait 30
ab, off
end
different from this?
to drive
ab, on
wait 30
ab, off
beep
end
The first procedure beeps, turns motors a and b on, waits 3 seconds, and turns motors a and b off. The second procedure beeps after the 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.