Boe-Bot head that looks twords the light, I need help with the code
walice_drel
Posts: 81
Here is the code I have sor far but I keep getting IF without ENDIf error message. can anyone plz help?
'
[noparse][[/noparse] Title ]
' Robotics with the Boe-Bot - LookingTowardTheLight.bs2
' Boe-Bot looks, and turns away from dark areas in favor of brighter areas.
' {$Stamp bs2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
'
[noparse][[/noparse] I/O Definitions ]
Piezo PIN 9 ' Piezo Speaker
Servo PIN 4 ' Servo
'
[noparse][[/noparse] Variables ]
' Declare variables for storing measured RC times of the
' left & right photoresistors.
counter VAR Nib
timeLeft VAR Word
timeRight VAR Word
average VAR Word
difference VAR Word
irDetectLeft VAR Bit
irDetectRight VAR Bit
'
[noparse][[/noparse] Initialization ]
DEBUG "Testing piezospeaker..."
FREQOUT 4, 200, 3000
FREQOUT 4, 100, 2800
FREQOUT 4, 200, 2400
FREQOUT 4, 140, 4200
FREQOUT 4, 30, 2000
FREQOUT 4, 200, 1000
FREQOUT 4, 100, 2500
FREQOUT 4, 80, 1300
FREQOUT 4, 200, 1200
FREQOUT 4, 150, 1000
FREQOUT 4, 35, 2000
FREQOUT 4, 140, 2800
FREQOUT 4, 10, 1000
FREQOUT 4, 100, 2500
FREQOUT 4, 50, 1500
FREQOUT 4, 100, 3800
DEBUG CLS,
"IR DETECTORS", CR,
"Left Right", CR,
"
"
'
[noparse][[/noparse] Main Routine ]
DO
GOSUB Test_Photoresistors
GOSUB Average_And_Difference
LOOP
'
[noparse][[/noparse] Subroutine - Test_Photoresistors ]
Test_Photoresistors:
HIGH 6 ' Left RC time Measurement.
PAUSE 3
RCTIME 6,1,timeLeft
HIGH 3 ' Right RC time measurement.
PAUSE 3
RCTIME 3,1,timeRight
RETURN
'
[noparse][[/noparse] Subroutine - Average_And_Difference ]
Average_And_Difference:
average = timeRight + timeLeft / 2
difference = average / 6
RETURN
'
[noparse][[/noparse] Subroutine - SCAN ]
DO
SCAN:
' Shadow significantly stronger on left detector, turn right.
Left:
IF (timeLeft > timeRight + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 1000
NEXT
RETURN
' Shadow significantly stronger on right detector, turn left.
RIGHT:
IF (timeRight > timeLeft + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 500
NEXT
RETURN
' Shadows in same neighborhood of intensity on both detectors.
Center:
IF (timeLeft = timeRight + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 750
NEXT
RETURN
'
[noparse][[/noparse] Title ]
' Robotics with the Boe-Bot - LookingTowardTheLight.bs2
' Boe-Bot looks, and turns away from dark areas in favor of brighter areas.
' {$Stamp bs2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
'
[noparse][[/noparse] I/O Definitions ]
Piezo PIN 9 ' Piezo Speaker
Servo PIN 4 ' Servo
'
[noparse][[/noparse] Variables ]
' Declare variables for storing measured RC times of the
' left & right photoresistors.
counter VAR Nib
timeLeft VAR Word
timeRight VAR Word
average VAR Word
difference VAR Word
irDetectLeft VAR Bit
irDetectRight VAR Bit
'
[noparse][[/noparse] Initialization ]
DEBUG "Testing piezospeaker..."
FREQOUT 4, 200, 3000
FREQOUT 4, 100, 2800
FREQOUT 4, 200, 2400
FREQOUT 4, 140, 4200
FREQOUT 4, 30, 2000
FREQOUT 4, 200, 1000
FREQOUT 4, 100, 2500
FREQOUT 4, 80, 1300
FREQOUT 4, 200, 1200
FREQOUT 4, 150, 1000
FREQOUT 4, 35, 2000
FREQOUT 4, 140, 2800
FREQOUT 4, 10, 1000
FREQOUT 4, 100, 2500
FREQOUT 4, 50, 1500
FREQOUT 4, 100, 3800
DEBUG CLS,
"IR DETECTORS", CR,
"Left Right", CR,
"
"
'
[noparse][[/noparse] Main Routine ]
DO
GOSUB Test_Photoresistors
GOSUB Average_And_Difference
LOOP
'
[noparse][[/noparse] Subroutine - Test_Photoresistors ]
Test_Photoresistors:
HIGH 6 ' Left RC time Measurement.
PAUSE 3
RCTIME 6,1,timeLeft
HIGH 3 ' Right RC time measurement.
PAUSE 3
RCTIME 3,1,timeRight
RETURN
'
[noparse][[/noparse] Subroutine - Average_And_Difference ]
Average_And_Difference:
average = timeRight + timeLeft / 2
difference = average / 6
RETURN
'
[noparse][[/noparse] Subroutine - SCAN ]
DO
SCAN:
' Shadow significantly stronger on left detector, turn right.
Left:
IF (timeLeft > timeRight + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 1000
NEXT
RETURN
' Shadow significantly stronger on right detector, turn left.
RIGHT:
IF (timeRight > timeLeft + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 500
NEXT
RETURN
' Shadows in same neighborhood of intensity on both detectors.
Center:
IF (timeLeft = timeRight + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 750
NEXT
RETURN
Comments
BTW are you playing a song at the beginning?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'
[noparse][[/noparse] Title ]
' Robotics with the Boe-Bot - LookingTowardTheLight.bs2
' Boe-Bot looks, and turns away from dark areas in favor of brighter areas.
' {$Stamp bs2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
'
[noparse][[/noparse] I/O Definitions ]
Piezo PIN 9 ' Piezo Speaker
Servo PIN 4 ' Servo
'
[noparse][[/noparse] Variables ]
' Declare variables for storing measured RC times of the
' left & right photoresistors.
counter VAR Nib
timeLeft VAR Word
timeRight VAR Word
average VAR Word
difference VAR Word
irDetectLeft VAR Bit
irDetectRight VAR Bit
'
[noparse][[/noparse] Initialization ]
DEBUG "Testing piezospeaker..."
FREQOUT 4, 200, 3000
FREQOUT 4, 100, 2800
FREQOUT 4, 200, 2400
FREQOUT 4, 140, 4200
FREQOUT 4, 30, 2000
FREQOUT 4, 200, 1000
FREQOUT 4, 100, 2500
FREQOUT 4, 80, 1300
FREQOUT 4, 200, 1200
FREQOUT 4, 150, 1000
FREQOUT 4, 35, 2000
FREQOUT 4, 140, 2800
FREQOUT 4, 10, 1000
FREQOUT 4, 100, 2500
FREQOUT 4, 50, 1500
FREQOUT 4, 100, 3800
DEBUG CLS,
"IR DETECTORS", CR,
"Left Right", CR,
"
"
'
[noparse][[/noparse] Main Routine ]
DO
GOSUB Test_Photoresistors
GOSUB Average_And_Difference
LOOP
'
[noparse][[/noparse] Subroutine - Test_Photoresistors ]
Test_Photoresistors:
HIGH 6 ' Left RC time Measurement.
PAUSE 3
RCTIME 6,1,timeLeft
HIGH 3 ' Right RC time measurement.
PAUSE 3
RCTIME 3,1,timeRight
RETURN
'
[noparse][[/noparse] Subroutine - Average_And_Difference ]
Average_And_Difference:
average = timeRight + timeLeft / 2
difference = average / 6
RETURN
'
[noparse][[/noparse] Subroutine - SCAN ]
SCAN:
' Shadow significantly stronger on left detector, turn right.
Left:
IF (timeLeft > timeRight + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 1000
NEXT
ENDIF
RETURN
' Shadow significantly stronger on right detector, turn left.
RIGHT:
IF (timeRight > timeLeft + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 500
NEXT
ENDIF
RETURN
' Shadows in same neighborhood of intensity on both detectors.
Center:
IF (timeLeft = timeRight + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 750
NEXT
ENDIF
RETURN
' What's a Microcontroller - MicroMusicWithRtttl.bs2
' Play Nokia RTTTL format ringtones using DATA.
'{$STAMP BS2}
'{$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]
SpeakerPin CON 9 ' Piezospeaker connected to P9.
'
[noparse][[/noparse] Variables ]
counter VAR Word ' General purpose counter.
char VAR Byte ' Variable stores characters.
index VAR Word ' Index for pointing at data.
noteLetter VAR Byte ' Stores note character.
noteFreq VAR Word ' Stores note frequency.
noteOctave VAR Word ' Stores note octave.
duration VAR Word ' Stores note duration.
tempo VAR Word ' Stores tempo.
default_d VAR Byte ' Stores default duration.
default_o VAR Byte ' Stores default octave.
default_b VAR Word ' Stores default beats/min.
'
[noparse][[/noparse] EEPROM Data ]
RTTTL_File DATA "StarWarImp:d=4,o=6,b=100:g5,g5,g5,8d#.5,16a#5,g5,8d#.5,16a#5,g.5,8p,d6,d6,d6,8d#.6,16a#5,f#5,8d#.5,16a#5,g5,8p,g6,8g.5,16g5,g6,8f#.6,16f6,16e6,16d6,16d#6,8p,8g#5,c#6,8c.6,16b5,16a#5,16a5,8a#5,8p,8d#5,f#5,8e.5,16a#5,g5,8d#.5,16b5,g.5"
Done DATA ",q,"
Notes DATA "p", "a", "#", "b",
"c", "#", "d", "#",
"e", "f", "#", "g",
"#"
Octave8 DATA Word 0, Word 3520, Word 3729, Word 3951,
Word 4186, Word 4435, Word 4699, Word 4978,
Word 5274, Word 5588, Word 5920, Word 6272,
Word 6645
'
[noparse][[/noparse] Initialization ]
counter = 0 ' Initialize counter.
GOSUB FindEquals ' Find first '=' in file.
GOSUB ProcessDuration ' Get default duration.
GOSUB FindEquals ' Find next '='.
GOSUB ProcessOctave ' Get default octave.
GOSUB FindEquals ' Find last '='.
GOSUB GetTempo ' Get default tempo.
'
[noparse][[/noparse] Program Code ]
DO UNTIL char = "q" ' Loop until 'q' in DATA.
GOSUB ProcessDuration ' Get note duration.
GOSUB ProcessNote ' Get index value of note.
GOSUB CheckForDot ' If dot, 3/2 duration.
GOSUB ProcessOctave ' Get octave.
GOSUB PlayNote ' Get freq, play note, next.
LOOP ' End of main loop.
END ' End of program.
'
[noparse][[/noparse] Subroutine - Find Equals Character ]
FindEquals: ' Go through characters in
' RTTTL file looking for
DO ' '='. Increment counter
READ RTTTL_File + counter, char ' until '=' is found, then
counter = counter + 1 ' return.
LOOP UNTIL char = "="
RETURN
'
[noparse][[/noparse] Subroutine - Read Tempo from RTTTL Header ]
' Each keyboard character has a unique number called an ASCII value.
' The characters 0, 1, 2,...9 have ASCII values of 48, 49, 50,...57.
' You can always convert from the character representing a digit to
' to its value by subtracting 48 from the variable storing the digit.
' You can examine this by comparing DEBUG DEC 49 and DEBUG 49.
GetTempo: ' Parse RTTTL file for Tempo.
' Convert characters to
default_b = 0 ' digits by subtracting 48
DO ' from each character's ASCII
READ RTTTL_File + counter, char ' value. Iteratively multiply
IF char = ":" THEN ' each digit by 10 if there
default_b = default_b / 10 ' is another digit, then add
counter = counter + 1 ' the most recent digit to
EXIT ' one's column.
ENDIF ' For example, the string
default_b = default_b + char - 48 ' "120" is (1 X 10 X 10)
counter = counter + 1 ' + (2 X 10) + 0. The '1'
default_b = default_b * 10 ' is converted first, then
LOOP UNTIL char = ":" ' multiplied by 10. The '2'
' is then converted/added.
RETURN ' 0 is converted/added, done.
'
[noparse][[/noparse] Subroutine - Look up Octave ]
ProcessOctave: ' Octave may or may not be
' included in a given note
READ RTTTL_File + counter, char ' because any note that is
SELECT char ' played in the default
CASE "5" TO "8" ' octave does not specify
noteOctave = char - "0" ' the octave. If a char
counter = counter + 1 ' from '5' to '8' then use
CASE ELSE ' it, else use default_o.
noteOctave = default_o ' Characters are converted
ENDSELECT ' to digits by subtracting
IF default_o = 0 THEN ' '0', which is the same as
default_o = noteOctave ' subtracting 48. The first
ENDIF ' time this subroutine is
' called, default_o is 0.
RETURN ' If 0, then set default_o.
'
[noparse][[/noparse] Subroutine - Find Index of Note ]
ProcessNote: ' Set index value for lookup
' of note frequency based on
READ RTTTL_File + counter, char ' note character. If 'p',
SELECT char ' index is 0. If 'a' to 'g',
CASE "p" ' read character values in
index = 0 ' DATA table and find match.
counter = counter + 1 ' Record index value when
CASE "a" TO "g" ' match is found. If next
FOR index = 1 TO 12 ' char is a sharp (#), add
READ Notes + index, noteLetter ' 1 to the index value to
IF noteLetter = char THEN EXIT ' increase the index (and
NEXT ' frequency) by 1 notch.
counter = counter + 1 ' As with other subroutines,
READ RTTTL_File + counter, char ' increment counter for each
SELECT char ' character that is processed.
CASE "#"
index = index + 1
counter = counter + 1
ENDSELECT
ENDSELECT
RETURN
'
[noparse][[/noparse] Subroutine - Determine Note Duration ]
ProcessDuration: ' Check to see if characters
' form 1, 2, 4, 8, 16 or 32.
READ RTTTL_File + counter, char ' If yes, then convert from
' ASCII character to a value
SELECT char ' by subtracting 48. In the
CASE "1", "2", "3", "4", "8" ' case of 16 or 32, multiply
duration = char - 48 ' by 10 and add the next
counter = counter + 1 ' digit to the ones column.
READ RTTTL_File + counter, char
SELECT char
CASE "6", "2"
duration = duration * 10 + char - 48
counter = counter + 1
ENDSELECT
CASE ELSE ' If no duration, use
duration = default_d ' use default.
ENDSELECT
IF default_d <> 0 THEN ' If default_d not defined
duration = 60000/default_b/duration*3 ' (if default_d = 0), then
ELSE ' set default_d = to the
default_d = duration ' duration from the d=#.
ENDIF
RETURN
'
[noparse][[/noparse] Subroutine - Check For '.' Indicating 1.5 Duration ]
CheckForDot: ' Check for dot indicating
' multiply duration by 3/2.
READ RTTTL_File + counter, char ' If dot found, multiply by
SELECT char ' 3/2 and increment counter,
CASE "." ' else, do nothing and
duration = duration * 3 / 2 ' return.
counter = counter + 1
ENDSELECT
RETURN
'
[noparse][[/noparse] Subroutine - Find Comma and Play Note/Duration ]
PlayNote: ' Find last comma in the
' current note entry. Then,
READ RTTTL_File + counter, char ' fetch the note frequency
SELECT char ' from data, and play it, or
CASE "," ' pause if frequency = 0.
counter = counter + 1
READ Octave8 + (index * 2), Word noteFreq
noteOctave = 8 - noteOctave
noteFreq = noteFreq / (DCD noteOctave)
IF noteFreq = 0 THEN
PAUSE duration
ELSE
FREQOUT SpeakerPin, duration, noteFreq
ENDIF
ENDSELECT
RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
What are you running for an IR source and is it modulated?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Post Edited (Franklin) : 8/10/2007 3:43:54 AM GMT
'
[noparse][[/noparse] Title ]
' Robotics with the Boe-Bot - LookingTowardTheLight.bs2
' Boe-Bot looks, and turns away from dark areas in favor of brighter areas.
' {$Stamp bs2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
'
[noparse][[/noparse] I/O Definitions ]
Piezo PIN 9 ' Piezo Speaker
'
[noparse][[/noparse] Variables ]
' Declare variables for storing measured RC times of the
' left & right photoresistors.
counter VAR Word
timeLeft VAR Word
timeRight VAR Word
average VAR Word
difference VAR Word
irDetectLeft VAR Bit
irDetectRight VAR Bit
ime VAR Word
'
[noparse][[/noparse] Initialization ]
FREQOUT 4, 100, 2800
FREQOUT 4, 200, 2400
FREQOUT 4, 140, 4200
FREQOUT 4, 30, 2000
FREQOUT 4, 35, 2000
FREQOUT 4, 140, 2800
FREQOUT 4, 10, 1000
FREQOUT 4, 100, 2500
FREQOUT 4, 50, 1500
FREQOUT 4, 100, 3800
'
[noparse][[/noparse] Main Routine ]
DO
GOSUB Test_Photoresistors
GOSUB Average_And_Difference
LOOP
'
[noparse][[/noparse] Subroutine - Test_Photoresistors ]
Test_Photoresistors:
HIGH 5 ' Left RC time Measurement.
PAUSE 3
RCTIME 5, 1,timeLeft
HIGH 2 ' Right RC time measurement.
PAUSE 3
RCTIME 2, 1,timeRight
RETURN
'
[noparse][[/noparse] Subroutine - Average_And_Difference ]
Average_And_Difference:
average = timeRight + timeLeft / 2
difference = average / 6
RETURN
'
[noparse][[/noparse] Subroutine - SCAN ]
SCAN:
' Shadow significantly stronger on left detector, turn right.
Left:
IF (timeLeft > timeRight + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 1000
NEXT
ENDIF
RETURN
' Shadow significantly stronger on right detector, turn left.
RIGHT:
IF (timeRight > timeLeft + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 500
NEXT
ENDIF
RETURN
' Shadows in same neighborhood of intensity on both detectors.
Center:
IF (timeLeft = timeRight + difference) THEN
FOR counter = 1 TO 150
PULSOUT 4, 750
NEXT
ENDIF
RETURN
here is the corect code.
'
[noparse][[/noparse] Title ]
' Robotics with the Boe-Bot - LookingTowardTheLight.bs2
' Boe-Bot looks, and turns away from dark areas in favor of brighter areas.
' {$Stamp bs2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
'
[noparse][[/noparse] I/O Definitions ]
Piezo PIN 9 ' Piezo Speaker
'
[noparse][[/noparse] Variables ]
' Declare variables for storing measured RC times of the
' left & right photoresistors.
counter VAR Word
timeLeft VAR Word
timeRight VAR Word
average VAR Word
difference VAR Word
time VAR Word
'
[noparse][[/noparse] Initialization ]
FREQOUT 4, 100, 2800
FREQOUT 4, 200, 2400
FREQOUT 4, 140, 4200
FREQOUT 4, 30, 2000
FREQOUT 4, 35, 2000
FREQOUT 4, 140, 2800
FREQOUT 4, 10, 1000
FREQOUT 4, 100, 2500
FREQOUT 4, 50, 1500
FREQOUT 4, 100, 3800
'
[noparse][[/noparse] Main Routine ]
DO
GOSUB Test_Photoresistors
GOSUB Average_And_Difference
GOSUB SCAN
LOOP
'
[noparse][[/noparse] Subroutine - Test_Photoresistors ]
Test_Photoresistors:
HIGH 5 ' Left RC time Measurement.
PAUSE 3
RCTIME 5, 1,timeLeft
HIGH 2 ' Right RC time measurement.
PAUSE 3
RCTIME 2, 1,timeRight
RETURN
'
[noparse][[/noparse] Subroutine - Average_And_Difference ]
Average_And_Difference:
average = timeRight + timeLeft / 4
difference = average / 9
RETURN
'
[noparse][[/noparse] Subroutine - SCAN ]
SCAN:
' Shadow significantly stronger on left detector, turn right.
Left:
IF (timeLeft > timeRight + difference) THEN
PULSOUT 4, 1000
' Shadow significantly stronger on right detector, turn left.
RIGHT:
ELSEIF (timeRight > timeLeft + difference) THEN
PULSOUT 4, 500
' Shadows in same neighborhood of intensity on both detectors.
Center:
ELSE
PULSOUT 4, 750
ENDIF
PAUSE 10
RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔