We used two different HandyBoards that were able to communicate using IR. This is the code for each HandyBoard, including our comments.
global [threshold]
menu 1 [find-target]
menu 2 [test-ir]
menu 3 [go-straight]
to test-ir
loop [irsend 10 wait 2]
end
;; main methods
;; drives around until sensor finds beam of light
to find-target
setthreshold 80
left
waituntil [ find-light ]
type find-light
say light-sense
go-to-and-attack
end
;; goes to target and engages in attack sequence
to go-to-and-attack
go-straight
say prox-sense
waituntil [ prox-sense > 55 ]
say prox-sense
a, off c, off
irsend 2
irsend 3
wait 50
go-straight
say prox-sense
waituntil [ prox-sense > 100 ]
say prox-sense
a, off c, off
irsend 1
end
;; goes forward until proximity sensor senses target then stops
;;to go-to-target
;; go-straight
;; type [not yet]
;; waituntil [ prox-sense > 40 ]
;; type [ got it ]
;; a, off c, off
;;end
;;loop [ ifelse find-light [ go-to-and-attack]
;; [left]
;; methods to drive
;; to go straight
to go-straight
a, thisway on c, thisway on
end
;; to turn left a = left motor c = right motor
to left
a, on thisway
c, on thatway
end
;; to turn right a = left motor c = right motor
to right
a, on thatway
c, on thisway
end
;; methods to sense and choose
;; returns whether the light sensor detects the flashlight
to find-light
output (light-sense < threshold)
end
;; returns the light sensor
to light-sense
output (sensor 0)
end
;; sets a threshold to the ambient light in the room
to l-thresh
setthreshold (light-sense - 180)
type threshold
end
;; returns proximity sensor
to prox-sense
output (sensor 6)
end
global [irsig]
Menu 1 [ get-signals ]
menu 2 [test-get ]
to test-get
loop [setirsig (ir)
if (irsig = 10) [beep beep beep]
]
end
to get-signals
loop [setirsig (ir)
ifelse (irsig = 1) [punch]
[ifelse (irsig = 2) [catapault]
[ if (irsig = 3) [string] ] ]
]
end
;; punch methods
;; extends the arm out and back
to punch
a, onfor punchtime
end
;; time for one round of punch
to punchtime
output 44
end
;; catapault methods
;; shoots the item in the catapault and reloads it
to catapault
b, onfor cattime
rd onfor cattime
rd
c, onfor reload
end
;;time it takes to shoot catapault and return to rest
to cattime
output 1
end
;; reloads the catapault
to reload
output 10
end
;; time it takes to load one item onto the catapault
to loadtime
end
;; string methods
;; extends the push arm forward and holds it for a short period of time and
;; then pulls it back
to string
d, onfor pushtime
d, rd
d, onfor pushtime
d, rd
end
;; sets time arm pushes string can
to pushtime
output 20
end
;;ir
;;loop global set to ir with ifelse