Pages: 1 [2]   Go Down
Author Topic: Set pin HIGH for x millis  (Read 1586 times)
0 Members and 1 Guest are viewing this topic.
Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4689
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4689
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it is. It's like magic. smiley

Try it with some example numbers and you'll see.
Logged

Pages: 1 [2]   Go Up
Jump to: