;; Jerry Simple

;; this is all programming by Julie and Katie, last modified 1/24/01

global [roomlight carpet flashlight white direction]

; programs

to jerry
print "jerry
initialize
launch [spytom]
linesense?

end

to initialize
print "initialize
setroomlight (right-eye + left-eye) / 2 
;; this averages all the light sensors
setcarpet linesense
setflashlight 20 ;; flashlight is the difference between ;; roomlight and a sensor
;; setwhite 40 
;; white is the difference between carpet 
;; and linesense
end

to whatiswhite ;; makes it easy to reset value of white
print "whatiswhite
setwhite 0 ;; but doesn't actually work
loop [print white 
if (frontbumper = 1) 
[waituntil [frontbumper = 0] setwhite white + 10]
]
end

; sounds

to scream
print "scream
note 96 1 note 90 1 note 96 1 note 90 1
note 96 1 note 90 1 note 96 1 note 90 1
note 96 1 note 90 1 note 96 1 note 90 1
note 82 .5 note 81 .5 note 80 .5
end

to burp
note 40 6
note 42 1
note 43 1
note 47 1
note 50 1
note 55 1
note 60 1
note 70 1
end

to tomjerrysong
note 65 2
note 67 2
note 62 2
note 65 4
wait .5
note 65 3
wait .5
note 65 2
wait .5
note 65 2
note 67 2
note 62 2
note 65 2
end

; motion programs

to run
print "run
left-wheel on thisway
right-wheel on thisway
end

to stopmotion
print "stopmotion
left-wheel off 
right-wheel off
end

to backup
print "backup
left-wheel on thatway
right-wheel on thatway
end

to makefastleft
print "makefastleft
left-wheel on thatway
right-wheel on thisway
end

to makefastright
print "makefastright
right-wheel on thatway
left-wheel on thisway
end

to slowright
print "slowright
left-wheel on thisway
right-wheel off
end

to slowleft
print "slowleft
right-wheel on thisway
left-wheel off
end

to tailwag
print "tailwag 
loop[tail setpower 5 onfor 2 thisway
tail setpower 5 onfor 2 thatway]
end

to shorttailwag
print "shorttailwag
repeat 4 [tail setpower 5 onfor 2 thisway
tail setpower 5 onfor 2 thatway]
end

to tailwag-off
tail off
end

to twist
print "twist
makefastright wait 5
makefastleft wait 10
makefastright wait random 10
makefastleft wait random 10
stopmotion
shorttailwag
end

; light programs
to spytom
top print "spytom bottom print candlesensor
;waituntil [or ((roomlight - left-eye) > flashlight) ((roomlight - ;right-eye) > flashlight)] stoprules follow-light
waituntil [candlesensor > 100] stoprules run 
waituntil [candlesensor < 90] stopmotion
scream turnaround 
waituntil [candlesensor > 100] run
waituntil [(carpet - linesense) > white] wait 5 stopmotion
end

to follow-light
print "follow-light
top print roomlight
loop [if ((roomlight - left-eye) > flashlight) [slowright]
if ((roomlight - left-eye) < flashlight) [left-wheel off]
if ((roomlight - right-eye) > flashlight) [slowleft]
if ((roomlight - right-eye) < flashlight) [right-wheel off]
bottom print right-eye bottom print left-eye]
end



; other programs

to linesense? ; should work well
print "linesense?
run
loop [
sees-line?
frontbumper?
]
end

to sees-line?
if ((carpet - linesense) > white) 
[backup wait 15 stopmotion wait 2 pickdirection ifelse (direction = 0) [makefastright] [makefastleft] wait random 15 run]
end

to pickdirection
setdirection random 2
end

to frontbumper?
if frontbumper [stopmotion pat backup wait 15 stopmotion wait 2 pickdirection ifelse (direction = 0) [makefastright] [makefastleft] wait random 15 run]
end

to frontbumpertom? ; works well
waituntil [frontbumper] 
scream turnaround run
end


to turnaround
print "180
backup wait 5 stopmotion
left-wheel on thatway
right-wheel on thisway
wait 11
left-wheel off
right-wheel off
end

to pat
waituntil [(patsensor )] waituntil [not (patsensor)] 
launch [tomjerrysong]
shorttailwag wait 10
launch [twist] launch [tomjerrysong] shorttailwag 
end


; motors and sensors

; motors

to left-wheel
a,
end

to right-wheel
b,
end

to tail
c,
end

;; sensors

to candlesensor
output sensor 5
end

to left-eye ; light sensor
output sensor 3
end

to right-eye ; light sensor
output sensor 4
end

to linesense ; reflectance sensor
output sensor 1
end

to nose ; reflectance sensor 
output sensor 0 
end

to frontbumper ; touch sensor
output switch 10
end

to patsensor
output switch 11
end

control buttons =
jerry
whatiswhite
frontbumper?
pat
spytom
follow-light
top print candlesensor

control box =

backup wait 10 stopmotion

print white

taillight?

loop [print taillight wait 1]
top print left-eye bottom print right-eye