Answers.Body: <p>You have not provided your programming language of choice, so I'll answer in pseudocode which can then be adapted to the specific environment.</p>
  
  <p>It should be possible with the following construction:</p>
  
  <pre><code>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
      follow_line_as_usual()
    else {                         // if the line is currently lost
      if (now() - last_time_line_seen &gt; 2_SECONDS) {
        turn_around_go_forward_catch_line()
      } else {                     // we are off the line but the two seconds have not passed yet
        do_something_else_like_go_forward()
      }
    }
    wait_some_time()               // optional, may not be needed
  } while(stop_condition)          // if you have a stop condition
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-05T12:25:27.040Z
Answers.LastActivityDate: 2019-02-08T07:45:17.767Z
Answers.LastEditDate: 2019-02-08T07:45:17.767Z
Answers.UserDisplayName: zovits
Body: <p>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.</p>
  
  <p>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?</p>
  
Tags: <mindstorms><programming>
Title: Unsure of how to use a switch that is time dependent
UserDisplayName: Gustav
ThreadText: # Unsure of how to use a switch that is time dependent
  
  (Asked by: username_0 on 2019-02-05)
  
  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?
  
  **Tags:** mindstorms, programming
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-05. Score: 1
  
  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
      follow_line_as_usual()
    else {                         // if the line is currently lost
      if (now() - last_time_line_seen > 2_SECONDS) {
        turn_around_go_forward_catch_line()
      } else {                     // we are off the line but the two seconds have not passed yet
        do_something_else_like_go_forward()
      }
    }
    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.
  
  
  ---
  
  