Have been trying to make a line follower that can auto-correct itself if it would lose the line. The follower is supposed to turn around, go forward and catch the line if the line has not been detected for about two seconds.

Can this be done by using a switch block where my current program goes into the true slot and the auto-correct in the false slot?


You have not provided your programming language of choice, so I'll answer in pseudocode which can then be adapted to the specific environment.

It should be possible with the following construction:

do {
  if (on_line) {                 // this may be a variable you update periodically or a method to call every time
    last_time_line_seen = now()  // remember that we have seen the line at this time
  else {                         // if the line is currently lost
    if (now() - last_time_line_seen > 2_SECONDS) {
    } else {                     // we are off the line but the two seconds have not passed yet
  wait_some_time()               // optional, may not be needed
} while(stop_condition)          // if you have a stop condition

This works by storing the timestamp of the last known time when the line was seen, and checking if that was more than 2 seconds ago in the case the line is currently lost.

I haven't seen in your question what your robot should do if it has lost the line but the two seconds have not passed yet so feel free to fill that part with any behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.