Clifford goes to Treasure Island

Home Journal Code Photos

Code

global [waittime direction] Defines two global variables

to search

setwaittime ((random 3000) + 2000)
go-straight
loop [if sees-left-magnet? [handle-left-magnet]
if sees-right-magnet? [handle-right-magnet]
if bumper? [handle-bumper]
if time-to-turn? [random-turn]
]

end

The main procedure: searching for treasure
It sets the variable “waittime” to be a random time between 2000 and 5000 millisec. (2 - 5 seconds), then calls the procedure “go-straight”. It runs through these if statements until the program is stopped. If one of the conditions is true, it calls the procedure in brackets.
to go-straight

if (not switch 15) [brush on]
waituntil [switch 15] off
left-motor on thisway
right-motor on thisway

end
To go straight, the brush must be in the up position. The left and right motors are then turned on, to go forward (“thisway”).
to sees-left-magnet?

output (switch 7)

end
Switch 7 is a magnet sensor. The output will be 1 if a magnet is near and 0 if there is no magnet. Essentially, 1 is true and 0 is false.
to handle-left-magnet

print [treasure!]
stop-motion
repeat 2 [beep wait 1]
left-motor, on thatway
right-motor, on thatway wait 5
stop-motion
wait 10
sweep
ab, on thatway wait 30
stop-motion
beep wait 2 beep
wait 100

end
If there is a magnet, the brush sweeps to reveal it.
The LED screen on the handy board reads “treasure!”, and the wheels are stopped. The handy board beeps. Both wheels then reverse (“on thatway”) for 0.5 seconds. They then stop, and the procedure sweep is called. Clifford then backs up so that the observer can take out the treasure. After waiting ten seconds, the program returns to the main procedure, search.
to sweep

repeat 3 [
brush on thisway
waituntil [not (switch 15)]
waituntil [switch 15]
]
brush off

end
This is the sweep procedure, used to reveal the treasure. The motor controlling the brush is turned on, and the brush sweeps three times. There is a touch sensor that is pushed down when the brush is in the upwards position. Once the brush has been not pressed and then pressed three times, the brush stops and the handy board returns to the handle-right-magnet or handle-left-magnet procedure.
to bumper?

output (switch 10)

end
When the bumper is pushed in by the wall, a touch sensor connected to switch port 10 is depressed, making the output of the switch be 1 rather than 0.
to handle-bumper

beep
resett
left-motor, on thatway
right-motor, on thatway
loop [if (timer > 10000) [stop]
if sees-left-magnet? [handle-left-magnet]
if sees-right-magnet? [handle-right-magnet]
]

end
When the bumper is hit, Clifford backs up.
Clifford beeps once to indicate that his bumper was hit. Then he resets the built-in timer. He then turns on the left and right motors in the reverse directions (“thisway” indicates forward, “thatway” indicates reverse). The program then runs through the infinite loop, checking to see if the if statements are true or not. He is still checking for treasure. Once he has backed up for 10 seconds, the program returns to the search procedure.
to time-to-turn?

output (timer > waittime)

end
The variable waittime was set earlier to a number between 2000 and 5000 milliseconds. If the timer reads higher than that number, then it is time to turn.
to random-turn

setdirection (random 6)
if (direction < 4) [go-straight]
if (direction = 4) [turn-left]
if (direction = 5) [turn-right]
setwaittime ((random 3000) + 2000)
resett

end
If it is time to turn, this is the procedure that is called. The variable direction is set to be a random number between 0 and 5. If the number is 0, 1, 2, or 3, Clifford goes straight. If it is 4, he turns left. If it is 5, he turns right. The variable waittime is then re-chosen and the timer is reset before Clifford returns to the main search program.
to stop-motion

left-motor off
right-motor off

end
 
to turn-left

left-motor on thisway
right-motor on thatway

end
To turn, one side turns in the forward direction and one side moves in the backward direction.
to turn-right

right-motor on thisway
left-motor on thatway

end
 
to sees-right-magnet?

output (switch 8)

end
 
to handle-right-magnet

print [treasure!]
stop-motion
repeat 3 [beep wait 1]
ab, on thatway wait 5
stop-motion
sweep
ab, on thatway wait 30
stop-motion
beep wait 2 beep
wait 5

end
 
to right-motor

b,

end
Each of the motor ports is assigned a letter, a through d. The right motor is plugged into motor port b.
to left-motor

a,

end
 
to brush

c,

end
 
to stop-all

abcd, off

end
This stops all of the motors.