Ten quick programming examples

From GoGoWiki

Jump to: navigation, search

Contents

Example 1

Make the GoGo board beep five times at one-second intervals.

to example1
    repeat 5 [ beep wait 10 ]
end


Example 2

Make the GoGo board turn on a light connected to port A for two seconds.

to example2
    a, on wait 20 a, off
end


Example 3

Make the GoGo board turn a light connected to port A on for two seconds and off for one second a total of five times.

to example3
    repeat 5 [a, on wait 20 a, off wait 10]
end


Example 4

Make the GoGo board beep whenever a sensor connected to port 1 has a value of less than 500.

to example2
   forever [ if sensor1 < 500 [ beep ] ] 
end


Example 5

Make the GoGo board turn on a light connected to port A whenever a sensor connected to port 1 has a value of less than 500. Otherwise, it should turn light A off.

to example5
    forever [ifelse sensor1 < 500 [a, on] [ a, off ] ] 
end


Example 6

Record the value of a sensor connected to port 1 every second for one minute.

to example6
    resetdp
    repeat 60 [ record sensor1 beep wait 10 ] 
end


Example 7

Make the GoGo board turn on a light connected to port A whenever a sensor connected to port 1 has a value of less than 500 and turn the light off if the sensor has a value of 500 or greater. At the same time, make the GoGo board turn on a light connected to port B whenever a sensor connected to port 2 has a value of less than 500 and turn the light off if the sensor has a value of 500 or greater.

to example7
    forever [
         ifelse sensor1 < 500 [a, on] [a, off] 
         ifelse sensor2 < 500 [b, on] [b, off]
    ]
end


Example 8

Make the GoGo board turn on a light connected to port A whenever a sensor connected to port 1 has a value of less than 500 and at the same time a sensor connected to port 2 has a value of more than 500. Otherwise, it should turn the light off.

to example8
    forever [ ifelse (sensor1 < 500) and (sensor2 > 500)[a, on][a, off]]
end


Example 9

Make the GoGo board turn on a light connected to port A either whenever a sensor connected to port 1 has a value of less than 500 or a sensor connected to port 2 has a value of more than 500. Otherwise, it should turn the light off.

to example9
    forever [ ifelse (sensor1 < 500) or (sensor2 > 500)[a, on][a, off]]
end

Example 10

Make the GoGo board count the number of times the value of a sensor connected to port 1 has a value of less than 500. Each time it does so, make the GoGo board beep with the total number of times the sensor has had a value of less than 500 until then.


to example10
    make “count 0
    forever [ 
         if sensor1 < 500 [ 
              make “count :count + 1 
              repeat :count [beep wait 5 ] 
              waituntil [sensor1 > 499]
         ]
    ]
end