I'm having a problem that I hope someone can see the logic I have possibly overlooked.
The robot is to pass through, say, 5 white lines on a black surface and then come to a rest. I am trying to work out the general logic to the algorithm rather than the proper syntax of the programming language.
I have written the program like so:
initialize variable lines_to_count = 5 initialize variable on_white_line = false
turn motors on and stay on until otherwise turned off
while (lines_to_count > 0)
if (sensor detected white surface) then on_white_line = true
if (on_white_line = true) and (sensor detected black surface) then lines_to_count = lines_to_count -1 on_white_line = false
loop while
turn motors off
All that does, however, if make the motors turn on momentarily and then immediately stop. It's as if the while loop is never entered or is executed so quickly followed by the turn motor off that it seems not to have run at all.
Can anyone see a problem with the logic? I'm stumped....can't explain why the robot just stops immediately upon being activated.
I would think the above program would make the robot move forward, stay in the WHILE loop as it keeps moving forward keepiing track of the number of lines it passeed by and, when the fifth line is passed, the robot stops.
Notice the two IF statements in the WHILE loop are mutually exclusive because one is looking for a white surface, the other a black surface. Line counting is determined by finding a white surface (the first IF) followed some time later by detecting a transition to a black surface (the second IF). The second IF toggles on_white_line variable back to false so that it can be resued for the next white line the robot encounters. You can assume the sensors are working perfectly.
Thanks.