Go from top to bottom, changing memory assets according to sensor data, as if going from the least to most important layer in subsumption architecture; layers override less important ones by either changing or not changing variable assets. Inefficient, yes, but I'm hoping it might work. ' {$STAMP BS1} ' {$PBASIC 1.0} 'Robot has just been turned on 'Initialize variables SYMBOL Rmotor = W0 SYMBOL Lmotor = W1 SYMBOL Lsensor = B4 SYMBOL Rsensor = B5 SYMBOL lines = W3 SYMBOL online = B8 Only a couple of bytes left! i doubt the BS1 has the RAM.... Rmotor = 1 '0 = backwards, 1 = still, 2 = forward Lmotor = 1 'same Lsensor = 0 '1 = line detected Rsensor = 0 lines = 0 'number of lines we have gone across (0-to-4) online = 0 'Whether or not we are sitting on a line 1 = line. '(used in preventing successive recounts of one line) PAUSE 1000 'wait before going start: IF (!Rsensor && !Rsensor) THEN 'if no line detected, go forward Lmotor = 2 Rmotor = 2 online = 0 'We are not even touching a line OR we have cleared it. endif IF (Rsensor) THEN 'if line on right, go forward right Lmotor = 2 Rmotor = 1 endif IF (Lsensor) THEN 'if line on left, go forward left Lmotor = 1 Rmotor = 2 endif IF (Lsensor && Rsensor) THEN 'if line is on both Left and Right, go forward and add one to line count Lmotor = 2 Rmotor = 2 IF (online == 0) THEN 'if we have straightened ourselves up on the line, and we have not seen it before, lines = lines + 1 'increment the number of lines we have encountered. (or, counting the rising edge) online = 1 'reset the variable so we do not see another rising edge until we clear the line. endif 'end of "catch rising edge by comparing current to previous" IF (lines == 2) THEN 'if we have crossed two lines, PAUSE 3000 'wait a bit to clear the line behind us, Lmotor = 0 'then turn. (rotating left) '(Rmotor is still going forward from previous code, no need to set it.) GOSUB SETMOTORS 'update motor states PAUSE 3000 'Wait untill we are approximately turned around, Lmotor = 2 'Then go forward again. (R motor is STILL forward.) GOSUB SETMOTORS endif 'end of "if two lines crossed" if (lines == 4) then 'if lines==4, stop motors, and end program. pause 3000 'Wait until we are over the line.......... Rmotor = 1 Lmotor = 1 gosub SETMOTORS 'Update motor state. end 'end program endif 'end of "if four lines" endif 'end of "if both sensors see a line" SETMOTORS: 'Do Right motor IF (Rmotor == 2) THEN PULSOUT Rightforward elseif (Rmotor == 1) THEN PULSOUT Rightstill else PULSOUT Rightrear endif 'Left motor IF (Lmotor == 2) THEN PULSOUT Leftforward elseif (Lmotor == 1) THEN PULSOUT Leftstill else PULSOUT Leftrear endif RETURN 'go back to gosub call location 'Not sure if a "return" is legal outside of a subroutine call GOTO start 'Loop 'Hmmm, I don't think that ( )'s are allowed in PBasic 1.