// constants won't change. Used here to
// set pin numbers:
-const int ledPin = 13; // the number of the LED pin
+const int ledPin = 13;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// 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 interval = 1000; // interval at which to blink (milliseconds)
+const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
// blink the LED.
if(millis() - previousMillis > interval) {
- // save the last time you blinked the LED
+ // Aggiorniamo il contatore previousMillis
previousMillis = millis();
// if the LED is off turn it on and vice-versa:
ledState = HIGH;
else
ledState = LOW;
- // e' possibile semplificare queta operazione?
+ // e' possibile semplificare questa operazione?
// Hint: lo stato del LED e' binario: ha solo due stati possibili.
// set the LED with the ledState of the variable:
// constants won't change. Used here to
// set pin numbers:
-const int ledA = 13; // the number of the LED pin
-int ledB = 12; //Secondo LED
+const int ledA = 13; // Primo LED
+const int ledB = 12; // Secondo LED
-// Variables will change:
+// Variabbili di stato
int ledStateA = LOW; // ledState used to set the LED
int ledStateB = LOW; // ledState used to set the LED
-
+
long previousMillisA = 0; // will store last time LED was updated
long previousMillisB = 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)
-long intervalB = 500; // interval at which to blink (milliseconds)
+long intervalB = 500; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
void loop()
{
- // here is where you'd put code that needs to be running all the time.
-
- // check to see if it's time to blink the LED; that is, if the
- // difference between the current time and last time you blinked
- // the LED is bigger than the interval at which you want to
- // blink the LED.
-
-// First LED
+// Primo LED
if(millis() - previousMillisA > intervalA) {
// save the last time you blinked the LED
previousMillisA = millis();
digitalWrite(ledA, ledStateA);
}
-// Second LED
+// Secondo LED
if(millis() - previousMillisB > intervalB) {
// save the last time you blinked the LED
previousMillisB = millis();
/////////////
// 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
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 loop()
{
- lightLedA(1000);
+ lightLedA(333);
lightLedB(500);
}
/* Blink without Delay - Pulizia
-Semplificato il ciclo condizionale
+Semplificato il ciclo condizionale, la seconda funzione non necessita
+di una variabile di stato per tracciare il LED.
+
*/
// constants won't change. Used here to
// set pin numbers:
// 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
// Second LED data
-int ledB = 12; //Secondo LED
-int ledStateB = LOW; // ledState used to set the LED
+const int ledB = 12; //Secondo LED
+// int ledStateB = LOW; // Possiamo leggere lo stato del registro del LED
+ // con digitalRead()
long previousMillisB = 0; // will store last time LED was updated
void setup() {
void lightLedB (int interval) {
// Illumina il ledB secondo un intervallo passato come argomento
- if(millis() - previousMillisB > interval) {
- // save the last time you blinked the LED
- previousMillisB = millis();
-
- // if the LED is off turn it on and vice-versa:
- ledStateB = !ledStateB ; // Inverti il LED
+ if(millis() - previousMillisB > interval) {
+ previousMillisB = millis();
+ digitalWrite(ledB, !digitalRead(ledB));
+ // Leggiamo direttamente il registro di ledB e scriviamo il suo opposto,
+ // questo ci permette di non dover avere una variabile per tracciare lo stato.
}
- digitalWrite(ledB, ledStateB);
}
/* Domande:
1. E' possibile avere una sola funzione che permetta di gestire
int ledPin ; // il numero del LED pin
int ledState ; // stato attuale del LED
long interval ; // milliseconds di intervallo nel lampeggiare
- long previousMillis ; //precedente cambio di stato
+ long previousMillis ; // precedente cambio di stato
// Constructor: come viene instanziato un oggetto facente parte della classe
public:
Aggiungere un secondo LED e farlo brillare ogni 500ms
mentre il primo brilla ogni 1000ms
+ Massimo comun denominatore 1000 MCD 500 = 500ms
+ Durata Periodo = 500ms
+
+
+ Stati:
+
a | b Changes
======== =========
1 | 1 x | x
0 | 1 x | x
0 | 0 | x
- Periodo = 500ms
*/
+