Timed if statement waiting for something else

I want to stop a motor if a limit switch has been hit for a certain amount of time without another limitswitch.
For example

if limitswitch hit
if longer than 200ms without other limitswitch hit
stop

How would I code this? Thanks

Can you get this far?

Hint: don't use delay().

The millisDelay class in my tutorial How to write Timers and Delays in Arduino
makes it easy to do things like this.

the code would look like


millisDelay timer;
unsigned long TIMER_MS = 200;

if (limitswitchHit) {
  if (!timer.running()) {
     timer.start(TIMER_MS);
  }
} else { 
   // not at limit any more stop timer
  timer.stop();
}
if (timer.justFinished() ) {
   // at limit for 200ms
  // do some thing
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.