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