Record the instant in time when an if statement is run

I am sure this was asked somewhere, but can someone please explain how to store the current time value when an if statement is run. I have a rotary encoder that reads a certain shaft displacement, and when it reaches this amount, the drive motor stops, and there is a 2 sec dwell before a solenoid actuates. So I need to know how I get my delay using millis() based on when the if statement is satisfied.

Look at the change detection example and consider a triggeredTime=millis();

You've practically answered your own question. If you want to store the time, you just go ahead and do that: you store the time!

Just make an unsigned long variable for storing the time when something happens. (Let's call it timeOfSomething.) Then, in your code, when something happens, just store the time in that variable, like so:

if (something) {
  timeOfSomething = millis();
}

Side note:

For 2s it’s OK to use millis() to note the time, if it was a much shorter delay then using micros could also make sense.

This is often forgotten so I thought I would throw that into the discussion for reference

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