' Robot Life - Simulate an animal's existence over a 2 Minute "Day"
'
'  Myke Predko
'
'  Copyright (C) 2001 & 2002 McGraw-Hill
'
' { $STAMP BS2 }

' Variables
i var byte

' Mainline
	high SC			' Set the I/O Bits As O/P
        high SD			'  and High
	high LED		'  Turn OFF LED
 
Loop:				' Repeat from here for each "Day"
	RobotData = Behavior1	' Start Day off by wandering about
	gosub RobotSend
	pause 30000		' Wander for 30 Seconds

	RobotData = Behavior2	' Now, look for "Food" (Light)
	gosub RobotSend
	for i = 1 to 30		'  Search/Feed for 30 Seconds
		RobotData = RobotState
		gosub RobotSendReceive
		if (RobotData = 2) then FeedingSkip
			low LED	'  Indicate Robot is Feeding
FeedingSkip:			'  Finished
		pause 1000	' Search/Feed for 30 Seconds
	next

	high LED		'  Turn the LED off to Indicate Feeding Finished

	RobotData = Behavior1	' Wander again for a few Seconds
	gosub RobotSend		'  after feeding
        pause 10000

	RobotData = Behavior3	' Find a dark spot to sleep for 
        gosub RobotSend		'  the rest of the "Day"
	pause 30000		' Behavior3 stops at obstacle

	pause 20000		' 20 Seconds left in "Day"

	goto Loop		' Start the new "Day"



'  SumoBot Interface Code Follows
'  
'  Myke Predko
'
'  Copyright (C) 2001 & 2002 McGraw-Hill
'
'  Robot Commands
RobotStop     con  0			'  Stop the Robot
Behavior1     con  1			'  Random Movement
Behavior2     con  2			'  Photovore
Behavior3     con  3			'  Photophobe
Behavior4     con  4			'  Wall Hugger/Maze Solver
RobotForward  con  5			'  Move Forward for 200 msecs
RobotReverse  con  6			'  Move Reverse for 200 msecs
RobotLeft     con  7			'  Turn Left for 200 msecs
RobotRight    con  8			'  Turn Right for 200 msecs
RobotLEDOn    con  9			'  Turn on the Robot's LED
RobotLEDOff   con 10			'  Turn off the Robot's LED
RobotPWM0     con 11			'  PWM = 0% Duty Cycle
RobotPWM1     con 12			'  PWM = 1st "Notch"
RobotPWM2     con 13			'  PWM = 2nd "Notch"
RobotPWM3     con 14			'  PWM = 3rd "Notch"
RobotPWM4     con 15			'  PWM = 100% Duty Cycle
RobotPWM      con 16			'  Return the Current PWM Value
RobotState    con 17			'  Return the Executing State
RobotWhiskers con 18			'  Return State of the "Whiskers"
					'   Bit 0 - Left "Whisker"
					'   Bit 1 - Right "Whisker"
RobotCDSL     con 19			'  Return Value of Left CDS Cell
RobotCDSR     con 20			'  Return Value of Right CDS Cell
RobotButton   con 21			'  Return the Last Remote Button Press
					'   0 - No Buttons Pressed
					'   1 - Leftmost Button Pressed
					'   2 - Middle Button Pressed
					'   3 - Rightmost Button Pressed
					'  After "RobotButton" Operation,  
					'   Button Save is Cleared

'  Robot Interface Pins
LED con 11				'  LED, Negative Active On
RIR con 12				'  Right Infra-Red Detector
LIR con 13				'  Left Infra-Red Detector
SC con 14				'  Define the I/O Pins
SD con 15

'  Robot Interface Variables
RobotData var byte			'  Data Byte to Send to/Receive 
					'   from Robot

'  Robot Operation Subroutines
RobotSend				'  Send the Byte in "RobotData"
	low SC				'  Hold Low for 1 msec before 
	pause 1				'   Shifting in Data
	shiftout SD, SC, LSBFIRST, [RobotData]
	high SC
	return

RobotSendReceive			'  Send the Byte in "RobotData"
	low SC				'  Hold Low for 1 msec before 
	pause 1				'   Shifting in Data
	shiftout SD, SC, LSBFIRST, [RobotData]
	pause 1				'  Wait for Operation to Complete
	shiftin SD, SC, LSBPOST, [RobotData]
	high SC
	return
