;; Jerry Final Tweaked

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

global [roomlight carpet flashlight white direction tomlight]

;; tomlight is a new follow-light variable
; programs

to jerry
print "jerry
initialize
end

to getonwithit
launch [spytom]
linesense?
end

to testinitialize
print "testinitialize
setcarpet linesense
whatiswhite1
end

to whatiswhite1 ;; makes it easy to reset value of white
print "whatiswhite
setwhite 0 
loop [
top print white bottom print "setwhite
if (frontbumper) [waituntil [not frontbumper] setwhite white + 10]
if (patsensor) [waituntil [not patsensor] linesense?]
]
end


to initialize
print "initialize
setcarpet linesense
;whatiswhite
setwhite 40
settomlight 90
getonwithit
end

to spytom
waituntil [or (right-eye < tomlight) (left-eye < tomlight)] stoprules follow-light1
end

to follow-light1
launch [frontbumpertom?]
launch [athome]
loop [if (left-eye < tomlight) [left-wheel on]
if (left-eye > tomlight) [left-wheel off]
if (right-eye < tomlight) [right-wheel on]
if (right-eye > tomlight) [right-wheel off]
]
end

to athome
waituntil [(carpet - linesense) > white] stoprules
stopmotion
end

to frontbumpertom? ; works well
if (frontbumper)
[waituntil [not frontbumper] scream turnaround run]
end

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


to sees-line?
waituntil [(carpet - linesense) > white] 
backup wait 5 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 5 makefastright wait random 20 run]
end


;to whatiswhite ;; makes it easy to reset value of white
;print "whatiswhite
;setwhite 0 
;loop [
;top print white bottom print "setwhite
;if (frontbumper) [waituntil [not frontbumper] setwhite white ;+ 10]
;if (patsensor) [waituntil [not patsensor] whatistomlight]
;if (patsensor) [waituntil [not patsensor] whatistomlight]
;]
;end

;to whatistomlight ;; makes it easy to reset value of ;; tomlight
;print "whatistomlight
;settomlight 0 
;loop [
;top print tomlight bottom print [set tomlight]
;if (frontbumper) [waituntil [not frontbumper] settomlight ;tomlight + 10]
;if (patsensor) [waituntil [not patsensor] getonwithit]
;]
;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 linebackup
;print "linebackup
;launch [waituntil [(carpet - linesense) > white] stopmotion ;beep run]
;backup
;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 follow-light
initialize
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 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
testinitialize
frontbumper?
tailwag
spytom
follow-light1

control box =
backup wait 10 stopmotion

print white

taillight?

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