Using variables
From GoGoWiki
Variables are very useful when the program needs to track or count something. The gogoboard supports up to 16 variables in your Logo programs. Here's how variables are defined and used in a gogoboard program.
make "Counter 0
The above command creates a global variable called Counter and assigns the value zero to it. Notice how you need a quote in front of the variable name. You can read a value from a variable as follows.
if :counter > 10 [ beep ]
The command reads the value from counter and the program will beep if its value is larger than 10. Note how you now use a colon (:) in front of the variable name. The rule of thumb is the following
- use a quote (") if you are assigning a value to the variable
- use a colon (:) if you are reading from a variable
You will need to use both a quote and a colon in the same command if you want to increase the value of a variable.
make "counter :counter + 1
The above command basically increases the value of counter by one. Note that all variables created with the make command are global variables. That is you can reference it from anywhere in your program.
Here are some examples of Logo programs that uses variables
Example 1
; this program will beep 5 times to BeepManyTimes make "count 5 repeat :count [ beep wait 1 ] end
Example 2
; this program will count the number of times sensor1's value
; becomes less than 500. It will beep for a number of
; times equal to the count value
to countSensor
make "count 0
forever
[
if sensor1 < 500
[
make "count :count + 1
repeat :count [beep wait 5]
waituntil [sensor1 > 500 ]
]
]
end
Example 3
; this example will turn on the light only when there are people
; in a room
;
; The setup is a follows:
; - The light is connected to motor A
; - There are separate doors for people to enter and leave the room
; sensor1 is at the entrance, sensor2 is at the exit.
; - Each door has a switch sensor that triggers (value < 500) when
; someone walks by.
to autoLight
; initialize the variable
make "peopleCount 0
forever
[
; if people enter the room -> increase peopleCount
if sensor1 < 500 [
make "peopleCount :peopleCount + 1
wait 1
]
; if people exit the room -> decrease peopleCount
if sensor2 < 500 [
make "peopleCount :peopleCount - 1
wait 1
]
; turn on the light when peopleCount is more than zero
; otherwise turn the light off
ifelse :peopleCount > 0 [ a, on ] [ a, off]
]
end

