The Code for Mighty Mouse

 

; This Program controls Mighty Mouse

to mouse
launch [light-to-follow?]
forever [pet-head?]
forever [pet-body?]
end

;----------------------------------------------------------------------------------
;if you pet the head, the mouse goes home

to pet-head?
if head-switch [go-home]
end

to go-home
turn-around
end

;-------------------------------------------------------------------------------
; if you pet the body, the mouse wiggles its tail

to pet-body?
if body-switches [wiggle-tail]
end

to wiggle-tail
c, on repeat 14 [wait 2 rd]
c, off
end

;--------------------------------------------------------------------------------
;the mouse can follow a light when one is shined on it

to light-to-follow?
loop[ if (and(l-light < 40) (r-light < 40)) [follow-light]]
end

to follow-light
loop[
ifelse (and (l-light > 50) (r-light > 50) )
[stop-follow-light]
[ifelse (l-light < r-light)
[turn-left]
[turn-right] ]
]
end

to stop-follow-light
stop-motion wait 5
if (r-light > 200)
[print 10 turn-around
wait 115
stop-motion]
light-to-follow?
end

;-----------------------------------------------------------------------------------
;these methods control the wheels making the mouse go
;left, right, straight, in a circle, or stop

to turn-left
left-wheel off
right-wheel on thisway
end

to turn-right
right-wheel off
left-wheel on thisway
end

to go-straight
left-wheel on thisway
right-wheel on thisway
end

to turn-around
a, on thisway
b, on thatway
end

to stop-motion
left-wheel off
right-wheel off
end

;-----------------------------------------------------------------------------------
;these are aliases for the motors and sensors so that they
;can be used more easily and clearly

to left-wheel
a,
end

to right-wheel
b,
end

to tail
c,
end

to head-switch
output switch 7
end

to body-switches
output (and (switch 8) (switch 9))
end

to l-light
output sensor 4
end

to r-light
output sensor 3
end