'{$STAMP BS2sx, Start_Patterns.bsx, Search&Destroy.bsx, Remote_Control.bsx}

' --------------Variables------------------------
'
Time VAR Word 'Button push time accumulator
Pattern VAR Byte 'The Start pattern to be run
C VAR Byte 'Counter
D VAR Byte 'Counter
IRout VAR Byte 'Sensor output to Branch

SPin CON 15 'Serial out pin
RPin CON 14 'Reset pin
Bmode CON 240 'Serial out Baudmode 240 = 9.6Kbit, 110 = 19.2Kbit
RFWD CON 0

' --------------Initializations------------------
'
LOW 1 'Preset sensors and outputs
LOW 2
LOW 3
LOW 4
LOW 5
LOW 8
LOW 9
LOW 10
LOW 11
IRout = 0

Initialize_Motor_Control:
LOW RPin 'Reset motor control
HIGH RPin
SEROUT SPin, Bmode, [$80, 0, RFWD, 0]




'**************************************************************************
'**************************************************************************
'***************Main Program***********************************************


Main:
IF IN0 = 0 THEN SetStartMode 'If Aux PB is pushed, run the Start Mode routine
READ 0, Pattern 'Retrieve the existing pattern from EEPROM
PUT 0, Pattern 'Store existing pattern in Scratch RAM
WRITE 0, 0 'Clear old pattern from EEPROM so that another
'reset will NOT rerun the old start pattern
RUN 1 'Run Start Patterns routine

SetStartMode:
LOW 10: HIGH 9: PAUSE 100 'Flash red and green until the
LOW 9: HIGH 10: PAUSE 100 'Aux PB is released
IF IN0 = 0 THEN SetStartMode

LOW 9: LOW 10: Pattern = 2 'Turn off LED and set the first start pattern
WRITE 0, Pattern 'Store the present pattern in RRPROM

Watch:
LOW 10: LOW 9 'Turn off LED

FOR D = 0 TO 40 'Pause for .4 seconds while watching for
IF IN0 = 0 THEN Timer 'Aux PB push
PAUSE 10
NEXT

FOR C = 1 TO Pattern STEP 2 'Flash the pattern number
LOW 10: HIGH 9 'Turn on Red LED

FOR D = 0 TO 15 'Pause for .15 seconds while watching for
IF IN0 = 0 THEN Timer 'Aux PB push
PAUSE 10
NEXT

LOW 10: LOW 9 'Turn off LED

FOR D = 0 TO 15 'Pause for .15 seconds while watching for
IF IN0 = 0 THEN Timer 'Aux PB push
PAUSE 10
NEXT

NEXT

FOR D = 0 TO 40 'Pause for .15 seconds while watching for
IF IN0 = 0 THEN Timer 'Aux PB push
PAUSE 10
NEXT


FOR D = 0 TO 50 'Pause for 2 seconds while watching for
IF IN0 = 0 THEN Timer 'Aux PB push
GOSUB Sensors 'Check the sensors turning on the Red or Green LED
PAUSE 20
NEXT

GOTO Watch

Timer:

LOW 9: LOW 10: Time = 0 'Reset timer couter

Pressed:
Time = Time + 1 'Increment timer counter
PAUSE 10
IF IN0 = 1 THEN Up

GOTO Pressed

Up:

IF TIME > 25 THEN Begin 'If the Aux PB is held down, Begin the start routine
Pattern = Pattern + 2 'If the Aux PB is flashed, increment pattern,
IF Pattern > 8 THEN Remote
WRITE 0, Pattern 'Store the new pattern in EEPROM

GOTO Watch 'Flash LED and wait for next Aux PB push
Remote:
RUN 3
Begin:

Pattern = Pattern + 1 'Set RightSearch to 1, i.e. search to the right
PUT 0, Pattern 'Store the new pattern in Scratch RAM
WRITE 0, 0 'Clear old pattern from EEPROM so that another
'reset will NOT rerun the old start pattern

RUN 1 'Run Start Patterns routine




'**************************************************************************
'**************************************************************************
'***************Check IR sensors*******************************************


Sensors:

FREQOUT 3, 1, 14450 'Center IR sensor
IROUT.BIT2 = IN6

FREQOUT 2, 1, 14450 'Left Center IR sensor
IROUT.BIT1 = IN6

FREQOUT 4, 1, 14450 'Right Center IR sensor
IROUT.BIT3 = IN6

FREQOUT 1, 1, 14493 'Left IR sensor
IROUT.BIT0 = IN6

FREQOUT 5, 1, 14450 'Right IR sensor
IROUT.BIT4 = IN6

LOW 9
LOW 10
IF IRout = 31 THEN IRoff 'If no sensors respond then LED is off
HIGH 10 'If any sensor responds then LED is green
IRoff:
IF IRout <> 0 THEN IRred
LOW 10 'If all sensor responds then LED is red
HIGH 9
IRred:
RETURN