baldengineer

Arduino: millis() rollover handling

Jul 15th, 2012
5,887
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Created by James the Enginerd.
  2. // More information at:  http://www.cmiyc.com/blog/2012/07/16/arduino-how-do-you-reset-millis/
  3.  
  4. #define interval 1000
  5.  
  6. unsigned long waitUntil=0;
  7.  
  8. void setup() {
  9.   pinMode(13, OUTPUT);
  10. }
  11.  
  12. void loop() {
  13.   if ((unsigned long)(millis() - waitUntil) >= interval) {  // check for rollover
  14.     // It's time to do something!
  15.     digitalWrite(13, !digitalRead(13));  // Toggle the LED on Pin 13
  16.     waitUntil = waitUntil + interval;  // wait another interval cycle
  17.   }
  18. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×