X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=multitasking%2FBlinkWithoutDelay_3_funzione%2FBlinkWithoutDelay_3_funzione.ino;h=098b5013035280f6e7195d867107bb1e5cb4a1d0;hb=HEAD;hp=e27cf55d8c4e1de6ff5e67fa5022d7826c6b6198;hpb=ad3aca9c8c8e24f1a1e0326ef5f027ab23b75712;p=sketchbook_andrea diff --git a/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino b/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino index e27cf55..098b501 100644 --- a/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino +++ b/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino @@ -1,4 +1,4 @@ -/* Blink without Delay +/* Blink without Delay: Refactoring Blink con funzione @@ -7,27 +7,25 @@ - Quali variabili determinano il comportamento del LED? - Come cambiano durante il corso dello script? - Sono globali o locali? - + +Variabili: http://www.maffucci.it/2011/12/15/appunti-di-programmazione-su-arduino-variabili/ + */ ///////////// // First LED -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) +const int ledA = 13; // the number of the LED pin +int ledStateA = LOW; // ledState used to set the LED +long previousMillisA = 0; // will store last time LED was updated +long intervalA = 1000; // interval at which to blink (milliseconds) void lightLedA () ; ////////////// // Second LED -// Now with less global variables thanks to static (see function body) +// Ora con meno variabili globali utilizzando static (vedi corpo della funzione) 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) +long previousMillisB = 0; // will store last time LED was updated + // interval at which to blink (milliseconds) void lightLedB () ; @@ -47,15 +45,12 @@ void loop() // Funzioni: void lightLedA () { - if (millis() > previousMillisA + intervalA) { + if (millis() - previousMillisA >= intervalA) { // save the last time you blinked the LED - previousMillisA = millis(); + previousMillisA += intervalA; // if the LED is off turn it on and vice-versa: - if (ledStateA == LOW) - ledStateA = HIGH; - else - ledStateA = LOW; + ledStateA = !ledStateA ; // set the LED with the ledState of the variable: digitalWrite(ledA, ledStateA); } @@ -65,15 +60,12 @@ void lightLedA () { void lightLedB () { long intervalB = 500; static int ledStateB ; // https://www.arduino.cc/en/Reference/Static - if (millis() > previousMillisB + intervalB) { + if (millis() - previousMillisB >= intervalB) { // save the last time you blinked the LED - previousMillisB = millis(); + previousMillisB += intervalB ; // if the LED is off turn it on and vice-versa: - if (ledStateB == LOW) - ledStateB = HIGH; - else - ledStateB = LOW; + ledStateB = !ledStateB; // set the LED with the ledState of the variable: digitalWrite(ledB, ledStateB); }