Datasheet

Chapter 4: Navigation Tips ยท Page 177
' -----[ Main Routine ]-------------------------------------------------------
DO
IF qtiLF = 1 THEN ' Left qti sees line?
FOR counter = 1 TO 15 ' State = Avoid tawara left
maneuver = Backward ' Back up
GOSUB Servos_And_Sensors
NEXT
FOR counter = 1 TO 15 ' Turn right
maneuver = RotateRight
GOSUB Servos_And_Sensors
NEXT
ELSEIF qtiRF = 1 THEN ' Right qti sees line?
FOR counter = 1 TO 15 ' State = Avoid tawara right
maneuver = Backward ' Back up
GOSUB Servos_And_Sensors
NEXT
FOR counter = 1 TO 15 ' Turn left
maneuver = RotateLeft
GOSUB Servos_And_Sensors
NEXT
.
.
.
The ELSE condition in the Main Routine performs the same search pattern introduced in
this chapter's Activity #1, but with a twist -
IF sensors <> 0 THEN GOTO
Next_State
. This IF...THEN statement is in all four FOR...NEXT loops in the search
pattern. If none of the sensors see anything, each
FOR...NEXT loop continues to
execute. However, if any sensor sees something, the
sensors variable will no longer be
zero. When that happens, the
ELSE condition terminates. Next, the outermost
DO...LOOP in the Main Routine repeats itself, and the correct state for handling the
sensor(s) that changed from 0 to 1 handle the situation. Since there are no IR detectors in
this program, the only sensors that can cause this condition are the QTIs. In the next
activity, all four IR detectors and both QTIs will have an opportunity to set a bit in the
sensors variable, causing the program to terminate the search condition and transition to
the state that handles the sensor that went high.
.
.
.
ELSE ' No objects detected?
FOR counter = 1 TO 35 ' State = search pattern
maneuver = Forward ' Straight ahead 35 pulses
GOSUB Servos_And_Sensors
IF sensors <> 0 THEN GOTO Next_State
NEXT