Go Down

Topic: Set pin HIGH for x millis (Read 1 time) previous topic - next topic

GoForSmoke

Right. I could save millis and call that L8R then add 15000 to that. Then in a loop check if millis = L8R. But if I missed the 1 millisecond window to check when they actually are equal it will turn into a long, long wait.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Nick Gammon

You just save the current time, and check for >= the interval, like this:

Code: [Select]

unsigned long start_time = millis ();  // when we start

...

// later we check if time is up
if (millis () - start_time >= 15000)
  {

  // time is up
  }



This code always works. It works if millis () wraps around, and it doesn't matter if you miss the exact moment.
http://www.gammon.com.au/electronics

PaulS

Code: [Select]
      vanntid_tens = Serial.read() - 0x30;
would be a lot easier to understand as
Code: [Select]
      vanntid_tens = Serial.read() - '0';

GoForSmoke

So that's because of unsigned numbers then Nick? I knew I missed something.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Nick Gammon

Yes it is. It's like magic. :)

Try it with some example numbers and you'll see.
http://www.gammon.com.au/electronics

Go Up