]> git.piffa.net Git - sketchbook_andrea/blobdiff - multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino
State machine e blinks con millis()
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_3_funzione / BlinkWithoutDelay_3_funzione.ino
index 63a324e9264388cf8c1e846aa43773777ad83a50..e27cf55d8c4e1de6ff5e67fa5022d7826c6b6198 100644 (file)
 
 /////////////
 // First LED
-int ledA =  13;      // the number of the LED pin
+const int ledA =  13;      // the number of the LED pin
 // Variables will change:
 int ledStateA = LOW;             // ledState used to set the LED
 long previousMillisA = 0;        // will store last time LED was updated
 // the follow variables is a long because the time, measured in miliseconds,
 // will quickly become a bigger number than can be stored in an int.
 long intervalA = 1000;           // interval at which to blink (milliseconds)
+void lightLedA () ;
 
 //////////////
-// Second LED
-int ledB = 12; //Secondo LED
+//  Second LED
+// Now with less global variables thanks to static (see function body)
+const int ledB = 12; //Secondo LED
            // ledState used to set the LED
 long previousMillisB = 0;        // will store last time LED was updated
            // interval at which to blink (milliseconds)
+void lightLedB () ;
 
 
 void setup() {
@@ -44,7 +47,7 @@ void loop()
 // Funzioni:
 
 void lightLedA () {
-  if(millis() - previousMillisA > intervalA) {
+  if (millis() > previousMillisA + intervalA) {
     // save the last time you blinked the LED 
     previousMillisA = millis();   
 
@@ -61,8 +64,8 @@ void lightLedA () {
 
 void lightLedB () {
   long intervalB = 500;
-   static int ledStateB ;  
-  if(millis() - previousMillisB > intervalB) {
+   static int ledStateB ;  // https://www.arduino.cc/en/Reference/Static
+  if (millis() > previousMillisB + intervalB) {
     // save the last time you blinked the LED 
     previousMillisB = millis();