global [left right epsilon diff]
to run
initialize
candle-seek
end
;initialize will set all the global variables
;epsilon is used as the "buffer" for two values being equal
;diff is used as the base sum; when left + right < sum, --> close to the candle
to initialize
setepsilon 2
setdiff 18
d, on
end
;candle-seek sets FELIX off to find the candle
to candle-seek
go-straight
loop[ setleft left-sensor
setright right-sensor
ifelse ((left + right) < diff)
[find-the-candle stop]
[if (left < right)
[turn-left]
if (right < left)
[turn-right]]]
end
;find-the-candle is called once FELIX is in close range to the candle
to find-the-candle
beep
loop[ setleft left-sensor
setright right-sensor
if (equal? left right)
[halt put-it-out stop]
if (left < right)
[turn-left]
if (right < left)
[turn-right]
]
end
;equal method with a buffer zone set by epsilon
to equal? :val1 :val2
output and ((:val1 - :val2) < epsilon)
((:val2 - :val1) < epsilon)
end
;put-it-out extinguishes the candle
to put-it-out
beep beep beep beep
halt wait 5
beep beep
wait 10
c, onfor 2 thatway onfor 2 thisway
end
to halt
ab, off
end
to go-straight
ab, on thisway
end
to backward
ab, on thatway
end
to left-sensor
output sensor 1
end
to right-sensor
output sensor 0
end
to turn-left
b, off a, on thisway
end
to turn-right
a, off b, on thisway
end