Cricket Logo

for Blue Dot Crickets
 
 
 



Motors
Control
Numbers
Timing and Sound
Multitasking / When
Global Variables / Arrays
Sensors / Sensor Page
Recursion
Procedure Inputs / Outputs
Data Recording / Playback
Accessing Bus Devices
Infrared Communication

 
Overview
 
NOTE: A less technical introduction can be found in the document Getting Started With Cricket.
 
Cricket Logo has the following features: 
  • control structures like if, repeat, wait, waituntil and loop
  • motor and sensor primitives
  • global and local variables
  • global arrays 
  • procedure definition with inputs and return values
  • primitives for infrared communication
  • a multitasking when primitive 
  • a 16-bit number system (addition, subtraction, multiplication, division, comparison); 
  • timing functions, a tone-playing function, and a random number function
  • data recording and playback primitives
  • bus-device access and control primitives
When using Cricket Logo, user programs are entered on a desktop computer and compiled into tokens which are beamed via infrared to the Cricket. (An "interface", connected to the desktop computer's serial port, translates these tokens into infrared signals. To download programs, both the interface and the Cricket must be turned on and their infrared ports must face each other.) Cricket Logo is a procedural language; procedures are defined using Logo to and end syntax: 
    to procedure-name 
    procedure-body 
    end
When the Cricket is idle, pressing its start-stop push-button causes it to begin executing remote-start line 1 on the Cricket Logo screen

When the Cricket is running a program, pressing the start-stop button causes it to halt program execution. 


 
Motors
 
The Cricket has two motors, which are named "A" and "B". A bi-color LED indicates the state of each motor. 

Motor commands are used by first selecting the motor (using a, , b, , or ab,) and then telling it what to do (e.g., on, off, rd, etc.). 
 

a, selects motor A to be controlled.
b, selects motor B to be controlled.
ab, selects both motors to be controlled.
on turns the selected motors on.
off turns the selected motors off.
brake actively applies a brake to the selected motors.
onfor duration turns the selected motors on for a duration of  time, where duration is given in tenths-of- seconds. E.g., onfor 10 turns the selected motors on for one second. 
thisway sets the selected motors to go the "thisway" direction, which is defined as the way that makes the  indicator LEDs light up green. 
thatway sets the selected motors to go the "thatway" direction, which is defined as the way that makes the indicator  LEDs light up red. 
rd reverses the direction of the selected motors. Whichever way they were going, they will go the opposite way. 
setpower level sets the selected motor(s) power level. Input is in the range of 0 (coasting with no power) to 8 (full  power).
 
 top

 
Timing and Sound:
 timer/resett, wait, beep, and play a note
 
The timing and sound commands are useful to cause the Cricket to do something for a length of time. The timing primitives are timer/resett and wait. 
timer  reports value of free-running elapsed time device. 
Time units are reported in 1 millisecond counts.
resett  resets elapsed time counter to zero.
 
wait duration  delays for a duration of time, where duration is given in tenths-of-seconds. E.g., wait 10 inserts a delay of one second.
 

For example, one might say 

    ab, on wait 20 off 
to turn the motors on for two seconds. This is equivalent to 
    ab, onfor 20
Please note that there are two different reference values for timing: 0.1 second units, used in wait and in note, and 0.001 second units, used in timer. 
 
Sound primitive, beep and note, can also be used to cause delays. 
beep  plays a short beep. 
note  pitch duration plays a note of a specified pitch and duration. Increasing values of the pitch create lower tones (the pitch value is used 
as a delay counter to generate each half of the tone's square wave). The duration value is specified in tenths-of-seconds units.
 

The correspondence between the numbers to define the pitch and the musical notes in the octave between middle c and high c is shown in the table below 
 

Pitch Number
119
110
110
105
100
100
94
89
84
84
79
74
74
70
66
66
62
59
Musical Notation
c
c#
db
d
d#
eb
e
f
f#
gb
g
g#
ab
a
a#
bb
b
c2
 

For example, 

    note 119 5 
will play a middle ãcä for half a second. Alternatively, the musical notation can be used directly: 
    note c 5 
does the same thing. 
 
Sensors
 
The Cricket has two sensors, named "A" and "B". 
sensora  reports the value of sensor A, as a number from 0 to 255.
sensorb reports the value of sensor B, as a number from 0 to 255.
switcha reports ãtrueä if the switch plugged into sensor A is pressed, and ãfalseä if not.
switchb  reports ãtrueä if the switch plugged into sensor B is pressed, and ãfalseä if not.
 
 
    Data Recording and Playback
 
In addition to the user defined arrays mentioned above there is a single global array for storing data which holds 2500 one-byte numbers. There is no error checking to prevent overrunning the data buffer. The following primitives are available for data recording and playback: 
 
resetdp  reset the value of data pointer to 0.
record  value  records value in the data buffer and advances the data pointer.
recall  value reports the value of the current data point and advances the data pointer.
erase number sets the value of the first number elements of the data array to zero and then sets the data pointer to zero. Because the process of  recording data is relatively slow (about 20 milliseconds per data point) it could take as long as 50 seconds for the erase 2500 command to be executed.
 
 
For example the procedure take-data can be used to store data recorded by a sensor once every second: 
    to take-data  
    erase 2500 
    beep  
    repeat 2500 [record sensora wait 10]  
    end 
The data can be "replayed" using the following send-data procedure: 
    to send-data  
    resetdp  
    repeat 2500 [send recall wait 5]  
    end 
This causes the data to appear in the monitor box on the Cricket Logo screen on the desktop, updating twice a second.
 
Control
 
Cricket Logo supports the following control structures: 
 
loop [body repetitively executes body indefinitely.
repeat times [body] executes body for times repetitions. times may be a constant or a calculated value.
if condition [body if condition is true, the cricket executes body. Note: a condition expression that evaluates to zero is considered "false"; all non-zero expressions are "true".
ifelse condition [body1] [body2 if condition is true, executes body-1; otherwise, executes body-2
waituntil [condition] loops repeatedly testing condition, continuing subsequent program execution after it becomes true. Note that condition must be contained in square brackets; this is unlike the conditions for if and ifelse, which do not use brackets. 
stop terminates execution of procedure, returning control to calling  procedure.
output value terminates execution of procedure, reporting value as result. 
 
 
Multitasking: when, whenoff
 
Blue Dot Cricket Logo contains a when primitive that allows for simple multitasking: 
 
when [condition] [body] launches a parallel process that repeatedly checks condition and executes body whenever condition changes from false to true. The when rule is "edge-triggered" and remains in effect until it is turned off with the whenoff primitive. Only one when rule can be in effect at a time; if a new when rule is executed by the program, this new rule replaces the previous rule.
whenoff  turns off any existing when rule.
 

For example, the following program will beep once every second, while reversing the motor direction every tenth of a second: 

    to beep-and shake  
    resett  
    when [timer > 1000] [beep resett]  
    loop [a, onfor 1 rd]  
    end

Note: Do not use the stop primitive inside the body of the when command. stop only has meaning inside of a procedure, and the when command executes in its own context, outside of any procedure scope.


 
Recursion
 
Cricket Logo supports tail recursion to create infinite loops. For example: 
    to beep-forever  
    beep wait 1  
    beep-forever  
    end 
is equivalent to 
    to beep-forever  
    loop [beep wait 1]  
    end
The recursive call must appear as the last line of the procedure and cannot be part of a control structure like if or waituntil. Thus the following is not valid: 
    to beep-when-pressed  
    beep wait 1  
    if switcha [beep-when-pressed]  
    end

 
Numbers
 
The "Blue Dot" version of Cricket Logo uses 16-bit integers between -32768 and + 32767. 

All arithmetic operators must be separated by a space on either side. E.g., the expression 3+4 is not valid. Use 3 + 4. 
 

+ infix addition
- infix subtraction
* infix multiplication
/ infix division
% infix modulus (remainder after integer division)
and infix logical "and" operation (bitwise and)
or infix logical "or" operation (bitwise or)
xor infix logical "xor" operation (bitwise xor)
not prefix logical not operation. use only with boolean values (1 and 0).
random reports pseudo-random number from 0 to 32767.
 

To perform a bitwise not operation on a 16-bit word value, xor the value with $ffff.


 
Global Variables
 
Global variables are created using the global [variable-list] directive at the beginning of the procedures window. E.g., 
    global [foo bar] 
creates two globals, named foo and bar. Additionally, two global-setting primitives are created: setfoo and setbar. Thus, after the global directive is interpreted, one can say 
    setfoo 3 
to set the value of foo to 3, and 
    setfoo foo + 1 
to increment the value of foo. 
 
Global Arrays
 
Global arrays are created in the Blue Dot version of Cricket Logo using the 
    array [array1-name, array1-length, array2-name, array2-length, etc.] 
directive at the beginning of the procedures window. E.g., 
    array [foo 50 bar 25]

creates two arrays, one named foo, which can hold 50 numbers and another named bar, which can hold 25 numbers. Elements in the array are set and read using the aset and aget primitives: 

aset array-name item-number value sets the item-numberth element of array-name to value
aget array-name item-number reports the item-numberth element of array-name

Thus, for example 

    aset foo 31 1000
sets the 31st element of foo to have a value of 1000 while 
    send aget foo 31
causes the value of the 31st element of foo to be transmitted via infrared. There is no error-checking to prevent arrays from overrunning their boundaries. 
 
Procedure Inputs and Outputs
 
Procedures can accept arguments using Logoâs colon syntax. E.g., 
    to wiggle :times  
    ab,  
    repeat :times [on wait 2 rd]  
    end 
creates a procedure named wiggle that takes an input which is used as the counter in a repeat loop. 

Procedures may return values using the output primitive; e.g.: 

    to go  
    ab,  
    repeat third [on wait 10 rd]  
    end  

    to third  
    if sensora < 20 [output 1]  
    if sensora < 50 [output 2]  
    output 3  
    end

The go procedure will execute 1, 2, or 3 times depending on the value of sensor A. 
 
Infrared Communication
 
Crickets can send infrared signals to each other using the send primitive, and receive them using the ir primitive. The newir? primitive can be used to check if a new infrared signal has been received since the last time ir primitive was used. 
 
send value  transmits value via infrared.
ir reports the byte most recently received by the infrared detector. Note that the Blue Dot crickets do not clear the infrared buffer. Thus the ir primitive reports the most recent byte received.
newir? reports ãtrueä if a new byte has been received by the infrared detector since last time ir was used, and ãfalseä if not. It does not effect the content of the infrared buffer.

For example, consider the following use of the newir? primitive: 

global [temp-ir] 

to thing1-or-thing2  
waituntil [newir?]        ;checks for new infrared byte 
settemp-ir ir                ;set the value of temp-ir to ir 
if temp-ir = 1 [thing1]             
if temp-ir = 2 [thing2]  
end 

to thing1 
     . . . 
end 

to thing2 
     . . . 
end

In this example nothing happens until a new infrared byte is received. Note the use of global variable temp-ir. The value of the most recent infrared byte is stored in the temp-ir global in order to ensure that the same value is used in testing the conditions for the next two if statements. If we instead use 
if ir = 1 [thing1] and if ir = 2 [thing2], though unlikely, the content of the ir buffer may change before both conditions are tested.  
 
There are cases when you may want to use an alternate form of  the thing1-or-thing2 procedure: 
to thing1-or-thing2  
if newir? [                     ;checks for new infrared byte 
settemp-ir ir                ;set the value of temp-ir to ir 
if temp-ir = 1 [thing1]  
if temp-ir = 2 [thing2] 
] 
end
In this case we do not wish to hold everything up until a new infrared byte is received; we only want thing1 or thing2 to happen if a new infrared byte is received. 
 
There are some subtleties with the inrared codes and the ir primitive that can lead to confusion: 
 
  • Infrared codes in the range 128 to129 are interpreted to launch remote-start menu items 1 or 2 on the cricket logo screen. These codes can be generated by pressing buttons #1 or #2 on the interface respectively. Household TV/VCR remotes may be used to cause the cricket to launch its two remote-start lines. Use a sony remote, or a universal remote set to talk to a Sony TV, and use the keys numbered 1 and 2.
  • Infrared codes in the range 130 to 140 are used by the underlying cricket operating system as scape codes for infrared program download. Therefore please restrict general purpose user broadcast of ir codes to the ranges of 1 to 127 or 141 and above. 
  • Received infrared values issued with the send primitive are displayed on the cricket logo screen in the small text box next to the download button.
  • There is a single byte of memory inside each Cricket that acts as the received infrared buffer, storing only the infrared byte that was most recently received. The ir primitive reports the value of the number stored in the buffer. 

  •  
    Accessing Bus Devices
     
    Crickets can send commands and data to bus devices using the bsr and bsend primitives. The bsr is used when the Cricket expects a response from the device such as a sensor reading from a sensor bus device. Each bus device has a unique ID. For example, the ID for 4-digit displays is $70 (the dollar sign indicates that the number is in hex or base 16). The procedure for displaying a number on the display is:

      ;the 1 in $170 in the first command indicates that this is a command and not data.
      ;The next two lines send the display the data that we want displayed:
      ;first the high byte and then the low byte.


      to display :n  
      bsend $170   
      bsend (:n / 256)   
      bsend (:n % 256)   
      end  

    When this procedure is downloaded to the Cricket, the command display sensora will display the reading from sensora. Ordinarily, you need not worry about writing these procedures. The current discussion is presented to help you understand the bus device libraries that we have written in Cricket Logo. Currently, you only need to cut and paste the appropriate libraries into the procedures window of your project and use the commands as primitives. We are currently working on a better way of incorporating the desired libraries.