Nesting loops in Boe-Bot whisker navigation
Greetings. We are new to Basic Stamp and the forum. We are going through the Boe-Bot Manual, and have a question about using whisker navigation. We used the code from the manual (Example Program: RoamingWithWhiskers.bs2, page 192 of the Acrobat version). The whisker navigation works great, and we decided to add flashing LED’S. (LED’S continually flash as the Boe- Bot moves)
·
We used the following code to flash LED’s
·
Do
High 1························· ‘LED ·1 on
High 10······················· ‘LED 2 on
Pause 500
Low 1························· ‘LED off
High 10······················· ‘LED off
Pause 500
Loop
·
This code works on it’s own, but we can’t seem to figure out where to put it without it messing up the main program or the subroutines.
·
In other words, we want the flashing LED subroutine to be continuously executed as the whisker decision trees for the navigation work independently.
·
We’ve tried it at every place in the program we can think of, and it messes up the subroutines.
·
Are we missing something basic or is this actually more complicated to do than we think?
·
PS we did not write out the code from the book to save space, but can if that will help get the question answered.
Any ideas?
Many thanks
·
We used the following code to flash LED’s
·
Do
High 1························· ‘LED ·1 on
High 10······················· ‘LED 2 on
Pause 500
Low 1························· ‘LED off
High 10······················· ‘LED off
Pause 500
Loop
·
This code works on it’s own, but we can’t seem to figure out where to put it without it messing up the main program or the subroutines.
·
In other words, we want the flashing LED subroutine to be continuously executed as the whisker decision trees for the navigation work independently.
·
We’ve tried it at every place in the program we can think of, and it messes up the subroutines.
·
Are we missing something basic or is this actually more complicated to do than we think?
·
PS we did not write out the code from the book to save space, but can if that will help get the question answered.
Any ideas?
Many thanks

Comments
Recall from Chapter 2, Activity #6 that servo pulses repeat about every 25 ms.· To delay for 0.5 seconds, this program's Flash_Leds subroutine·counts up to 20 servo pulses (20 x .025 s = 0.5 s), then changes the state of the LEDs.· The Flash_Leds subroutine is called from everywhere the program delivers a servo pulse.· More importantly, the program is doing it from inside the various FOR...NEXT loops.· If those Flash_Leds subroutine calls were outside the FOR...NEXT loops, there would be interruptions in the flashing every time a maneuver was executed.
BTW, another interesting modification is to light up the left LED while the Boe-Bot is avoiding an object on the left, the right LED while the Boe-Bot is avoiding an object on its right, etc.
' -----[noparse][[/noparse] Title ]-------------------------------------------------------------- ' Robotics with the Boe-Bot - RoamingWithWhiskers.bs2 ' Boe-Bot uses whiskers to detect objects, and navigates around them. ' {$STAMP BS2} ' Stamp directive. ' {$PBASIC 2.5} ' PBASIC directive. DEBUG "Program Running!" ' -----[noparse][[/noparse] Variables ]---------------------------------------------------------- pulseCount VAR Byte ' FOR...NEXT loop counter. counter VAR Byte ' -----[noparse][[/noparse] Initialization ]----------------------------------------------------- FREQOUT 4, 2000, 3000 ' Signal program start/reset. LOW 1 LOW 10 ' -----[noparse][[/noparse] Main Routine ]------------------------------------------------------- DO [color=#000000] IF (IN5 = 0) AND (IN7 = 0) THEN ' Both whiskers detect obstacle[/color] [color=#000000] GOSUB Back_Up ' Back up & U-turn (left twice)[/color] [color=#000000] GOSUB Turn_Left[/color] [color=#000000] GOSUB Turn_Left[/color] [color=#000000] ELSEIF (IN5 = 0) THEN ' Left whisker contacts[/color] [color=#000000] GOSUB Back_Up ' Back up & turn right[/color] [color=#000000] GOSUB Turn_Right[/color] [color=#000000] ELSEIF (IN7 = 0) THEN ' Right whisker contacts[/color] [color=#000000] GOSUB Back_Up ' Back up & turn left[/color] [color=#000000] GOSUB Turn_Left[/color] [color=#000000] ELSE ' Both whiskers 1, no contacts[/color] [color=#000000] GOSUB Forward_Pulse ' Apply a forward pulse[/color] [color=#000000] ENDIF ' and check again[/color] LOOP ' -----[noparse][[/noparse] Subroutines ]-------------------------------------------------------- Forward_Pulse: ' Send a single forward pulse. [color=#000000] PULSOUT 13,850[/color] [color=#000000] PULSOUT 12,650[/color] [color=#000000] PAUSE 20[/color] [color=#000000] GOSUB Flash_Leds RETURN[/color] Turn_Left: ' Left turn, about 90-degrees. [color=#000000] FOR pulseCount = 0 TO 20[/color] [color=#000000] PULSOUT 13, 650[/color] [color=#000000] PULSOUT 12, 650[/color] [color=#000000] PAUSE 20[/color] [color=#000000] GOSUB Flash_Leds NEXT[/color] [color=#000000] RETURN[/color] Turn_Right: [color=#000000] FOR pulseCount = 0 TO 20 ' Right turn, about 90-degrees.[/color] [color=#000000] PULSOUT 13, 850[/color] [color=#000000] PULSOUT 12, 850[/color] [color=#000000] PAUSE 20[/color] [color=#000000] GOSUB Flash_Leds NEXT[/color] [color=#000000] RETURN[/color] Back_Up: ' Back up. [color=#000000] FOR pulseCount = 0 TO 40[/color] [color=#000000] PULSOUT 13, 650[/color] [color=#000000] PULSOUT 12, 850[/color] [color=#000000] PAUSE 20[/color] [color=#000000] GOSUB Flash_Leds NEXT[/color] [color=#000000] RETURN[/color] Flash_Leds: counter = counter + 1 IF counter = 20 THEN counter = 1 TOGGLE 1 TOGGLE 10 ENDIF RETURNPost Edited (Andy Lindsay (Parallax)) : 1/23/2006 4:05:00 AM GMT