/* * Blink Without Blocking * * Demonstrates how to use an event loop timer to avoid unnecessary delays in code. * This sort of form allows you to juggle multiple inputs and outputs more easily. * * Robert Zacharias, 2/2017 * released to the public domain by the author */ // "global" variables in this section can be used by any function in the whole sketch int wait = 250; // this variable, of type integer, is the number of milliseconds we'll wait between blinks long timer = 0; // this variable, of type "long," will help keep track of passing time bool light = LOW; // this varialbe, of type boolean, will keep track of the current LED state void setup() { pinMode(LED_BUILTIN, OUTPUT); // LED_BUILTIN is a handy shortcut for the pin connected to the on-board LED } void loop(){ blink(); // just a shortcut to go to the blink() function digitalWrite(LED_BUILTIN, light); // tells the LED to either go on or off, depending on the value of "light" } // here's where we define what the blink() function does void blink(){ // if it has been more than "wait" amount of time since the light changed if ( (millis() - timer) > wait ) { // (millis() is the number of milliseconds since the Arduino powered up) light = !light; // this will "toggle" the value of "light" to its opposite timer = millis(); // reset the timer } // if it has *not* been long enough, the control will just go past the if statement and exit the blink() function }