Skip to content

Instantly share code, notes, and snippets.

@rwinscot
Created August 10, 2013 12:10
Show Gist options
  • Save rwinscot/6200202 to your computer and use it in GitHub Desktop.
Save rwinscot/6200202 to your computer and use it in GitHub Desktop.
If you're new to Arduino, you might not realize just how badly delays can impact performance. When (not if) you run into problems associated with delay - the Googles might have you believe that a library is the answer to your problems. It can help... but may not be necessary. Consider the following approach as an alternative.
byte LED = 13; // debug LED on the Arduino
byte blnk = 0; // variable used to track LED flip-flop
unsigned long tme = 0; // the last time we processed doBlink()
unsigned long slc = 500; // milliseconds between doBlink() calls
unsigned long ms = 0; // a millis() time-slice
void setup() {
pinMode( LED, OUTPUT );// set Arduino LED as output - for to blink
}
void loop() {
ms = millis();
// are we ready to call doBlink()?
if ( tme + slc < ms ) {
tme = ms;
doBlink();
}
}
void doBlink() {
if ( blnk == 0 ) {
digitalWrite( LED, HIGH );
blnk = 1;
}
else {
digitalWrite( LED, LOW );
blnk = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment