by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
+ modified by eaman
This example code is in the public domain.
// 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.
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > interval) {
+ if(millis() - previousMillis > interval) {
// save the last time you blinked the LED
- previousMillis = currentMillis;
+ previousMillis = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
/* Domande
1. Aggioungere un LED che brilli ogni 500ms
- 2. E' possibile cambiare gli intervalli dei due LED?
+ 2. E' ora agevole cambiare gli intervalli dei due LED?
*/
+++ /dev/null
-/* Blink without Delay
-
- Soluzione
-
- 3. Provare a isolare il codice per accendere ogni singolo led in una funzione:
- Quali variabili determinano il comportamento del LED?
- Sono globali o locali?
-
- */
-
-// constants won't change. Used here to
-// set pin numbers:
-
-// 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)
-
-// Second LED
-int ledB = 12; //Secondo LED
-int ledStateB = LOW; // ledState used to set the LED
-long previousMillisB = 0; // will store last time LED was updated
-long intervalB = 500; // interval at which to blink (milliseconds)
-
-
-void setup() {
- // set the digital pin as output:
- pinMode(ledA, OUTPUT);
- pinMode(ledB, OUTPUT);
-}
-
-void loop()
-{
- lightLedA();
- lightLedB();
-}
-
-
-// Funzioni
-
-void lightLedA () {
- if(millis() - previousMillisA > intervalA) {
- // save the last time you blinked the LED
- previousMillisA = millis();
-
- // if the LED is off turn it on and vice-versa:
- if (ledStateA == LOW)
- ledStateA = HIGH;
- else
- ledStateA = LOW;
- // set the LED with the ledState of the variable:
- digitalWrite(ledA, ledStateA);
- }
-
-}
-
-void lightLedB () {
- if(millis() - previousMillisB > intervalB) {
- // save the last time you blinked the LED
- previousMillisB = millis();
-
- // if the LED is off turn it on and vice-versa:
- if (ledStateB == LOW)
- ledStateB = HIGH;
- else
- ledStateB = LOW;
- // set the LED with the ledState of the variable:
- digitalWrite(ledB, ledStateB);
- }
-}
-
-
-/* Domande
- 2. Inserire un secondo LED con intervallo 500ms
- 1. Trasformare il codice utilizzato in una State Machine
- */
-
-
-
-
-
+++ /dev/null
-/* Blink without Delay
- Soluzione
-
- Introdotto un argomento per la funzione che nodifica l'intervallo di lampeggio
-
- */
-
-// constants won't change. Used here to
-// set pin numbers:
-
-// 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.
-
-
-// Second LED data
-int ledB = 12; //Secondo LED
-int ledStateB = LOW; // ledState used to set the LED
-long previousMillisB = 0; // will store last time LED was updated
-
-
-
-void setup() {
- // set the digital pin as output:
- pinMode(ledA, OUTPUT);
- pinMode(ledB, OUTPUT);
-}
-
-void loop()
-{
- lightLedA(40);
- lightLedB(500);
-}
-
-
-// Funzioni
-
-void lightLedA (int interval) {
- // Illumina il ledA secondo un intervallo passato come argomento
-
- if(millis() - previousMillisA > interval) {
- // save the last time you blinked the LED
- previousMillisA = millis();
-
- // if the LED is off turn it on and vice-versa:
- if (ledStateA == LOW)
- ledStateA = HIGH;
- else
- ledStateA = LOW;
- // set the LED with the ledState of the variable:
- digitalWrite(ledA, ledStateA);
- }
-
-}
-
-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:
- if (ledStateB == LOW)
- ledStateB = HIGH;
- else
- ledStateB = LOW;
- // set the LED with the ledState of the variable:
- digitalWrite(ledB, ledStateB);
- }
-}
-
-/* TODO
-- Differenze tra fuznioni e programmazione a oggetti: integrazione tra
-operativita' e dati
-- Rapporto tra global scope e uso di pointers
-- uso di forme di dati strutturate (array, struct) per scambiare dati tra funzioni e programma
-*/
-
-
-
-
-
-
-
-/* Blink without Delay
+/* Blink without Delay - due led
Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
+ modified by eaman
This example code is in the public domain.
// 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.
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillisA > intervalA) {
+ if(millis() - previousMillisA > intervalA) {
// save the last time you blinked the LED
- previousMillisA = currentMillis;
+ previousMillisA = millis();
// if the LED is off turn it on and vice-versa:
if (ledStateA == LOW)
digitalWrite(ledA, ledStateA);
}
- if(currentMillis - previousMillisB > intervalB) {
+ if(millis() - previousMillisB > intervalB) {
// save the last time you blinked the LED
- previousMillisB = currentMillis;
+ previousMillisB = millis();
// if the LED is off turn it on and vice-versa:
if (ledStateB == LOW)
}
/* Domande
- 2. Inserire un secondo LED con intervallo 500ms
- 1. Trasformare il codice utilizzato in una State Machine
-
+ 1. Provare a isolare il codice per accendere ogni singolo led in una funzione:
+ Quali variabili determinano il comportamento del LED?
+ Sono globali o locali?
*/
--- /dev/null
+/* Blink without Delay
+
+ Soluzione
+
+ 3. Provare a isolare il codice per accendere ogni singolo led in una funzione:
+ Quali variabili determinano il comportamento del LED?
+ Sono globali o locali?
+
+ */
+
+// constants won't change. Used here to
+// set pin numbers:
+
+/////////////
+// First LED
+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)
+
+//////////////
+// Second LED
+int ledB = 12; //Secondo LED
+int ledStateB = LOW; // ledState used to set the LED
+long previousMillisB = 0; // will store last time LED was updated
+long intervalB = 500; // interval at which to blink (milliseconds)
+
+
+void setup() {
+ // set the digital pin as output:
+ pinMode(ledA, OUTPUT);
+ pinMode(ledB, OUTPUT);
+}
+
+void loop()
+{
+ lightLedA();
+ lightLedB();
+}
+
+
+// Funzioni
+
+void lightLedA () {
+ if(millis() - previousMillisA > intervalA) {
+ // save the last time you blinked the LED
+ previousMillisA = millis();
+
+ // if the LED is off turn it on and vice-versa:
+ if (ledStateA == LOW)
+ ledStateA = HIGH;
+ else
+ ledStateA = LOW;
+ // set the LED with the ledState of the variable:
+ digitalWrite(ledA, ledStateA);
+ }
+
+}
+
+void lightLedB () {
+ if(millis() - previousMillisB > intervalB) {
+ // save the last time you blinked the LED
+ previousMillisB = millis();
+
+ // if the LED is off turn it on and vice-versa:
+ if (ledStateB == LOW)
+ ledStateB = HIGH;
+ else
+ ledStateB = LOW;
+ // set the LED with the ledState of the variable:
+ digitalWrite(ledB, ledStateB);
+ }
+}
+
+
+/* Domande
+ 1. Modificare le funzioni in modo che accettino come parametro
+ l'intervallo di lampeggio.
+
+ */
+
+
+
+
+
--- /dev/null
+/* Blink without Delay
+ Soluzione
+
+ Introdotto un argomento per la funzione che nodifica l'intervallo di lampeggio
+
+ */
+
+// First LED
+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
+long previousMillisB = 0; // will store last time LED was updated
+
+void setup() {
+ // set the digital pin as output:
+ pinMode(ledA, OUTPUT);
+ pinMode(ledB, OUTPUT);
+}
+
+void loop()
+{
+ lightLedA(1000);
+ lightLedB(500);
+}
+
+//////////////
+// Funzioni
+
+void lightLedA (int interval) {
+ // Illumina il ledA secondo un intervallo passato come argomento
+
+ if(millis() - previousMillisA > interval) {
+ // save the last time you blinked the LED
+ previousMillisA = millis();
+
+ // if the LED is off turn it on and vice-versa:
+ if (ledStateA == LOW)
+ ledStateA = HIGH;
+ else
+ ledStateA = LOW;
+ // set the LED with the ledState of the variable:
+ digitalWrite(ledA, ledStateA);
+ }
+
+}
+
+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:
+ if (ledStateB == LOW)
+ ledStateB = HIGH;
+ else
+ ledStateB = LOW;
+ // set the LED with the ledState of the variable:
+ digitalWrite(ledB, ledStateB);
+ }
+}
+
+/* Approfondimenti
+- Quali similitudini ci sono tra le due funzioni?
+- Come si dovrebbe fare per semplificare il codice
+ evitando di ripetere larti del codice simile tra loro?
+- Distinguere i dati comuni tra le due funzioni che ci servono per
+ far lampeggiare i LED
+- Distinguere i dati che caratterizzano un LED rispetto all'altro
+*/
+
+
+
+
+
+
+
--- /dev/null
+/* Blink without Delay - Pulizia
+
+Semplificato il ciclo condizionale
+ */
+
+// constants won't change. Used here to
+// set pin numbers:
+
+// First LED
+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
+long previousMillisB = 0; // will store last time LED was updated
+
+void setup() {
+ // set the digital pin as output:
+ pinMode(ledA, OUTPUT);
+ pinMode(ledB, OUTPUT);
+}
+
+void loop()
+{
+ lightLedA(1000);
+ lightLedB(500);
+}
+
+////////////////
+// Funzioni
+
+void lightLedA (int interval) {
+ // Illumina il ledA secondo un intervallo passato come argomento
+
+ if(millis() - previousMillisA > interval) {
+ // save the last time you blinked the LED
+ previousMillisA = millis();
+
+ // if the LED is off turn it on and vice-versa:
+ ledStateA = !ledStateA ; // Inverti il LED
+ }
+ digitalWrite(ledA, ledStateA);
+}
+
+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
+ }
+ digitalWrite(ledB, ledStateB);
+}
+
+/* Approfondimenti
+ - integrazione tra funzioni e dati
+ - Rapporto tra global scope e uso di pointers
+ - Uso di forme di dati strutturate (array, struct) per scambiare dati tra funzioni e programma
+ */
+
+
+
+
+
+
+
+
+
+
+
--- /dev/null
+/* Blink without Delay
+ Soluzione
+
+ Introdotto un argomento per la funzione che nodifica l'intervallo di lampeggio
+ */
+
+class Lampeggiatore {
+ // Lampeggia un LED utilizzando millis()
+ // Variabili
+ 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
+
+ // Constructor: come viene instanziato un oggetto facente parte della classe
+public:
+ Lampeggiatore(int pin, long time)
+ {
+ ledPin = pin;
+ pinMode(ledPin, OUTPUT);
+ ledState = LOW;
+ previousMillis = 0;
+ interval = time;
+ }
+
+// Una funzione facente parte di una classe prende il nome di "metodo" della stessa:
+ void Update () {
+ // Illumina il ledB secondo un intervallo passato come argomento
+
+ if(millis() - previousMillis > interval) {
+ // save the last time you blinked the LED
+ previousMillis = millis();
+
+ // if the LED is off turn it on and vice-versa:
+ ledState = !ledState ; // Inverti il LED
+ }
+ // set the LED with the ledState of the variable:
+ digitalWrite(ledPin, ledState);
+ }
+
+};
+
+// Instanziamo i due led dalla classe
+Lampeggiatore ledA(13, 1000);
+Lampeggiatore ledB(12, 500);
+
+void setup() {
+}
+
+void loop()
+{
+ledA.Update();
+ledB.Update();
+}
+
+
/*
- Blink
- Turns on an LED on for one second, then off for one second, repeatedly.
-
- This example code is in the public domain.
+ Blink due LED - Soluzione
+
*/
// Pin 13 has an LED connected on most Arduino boards.
;
}
-/* Domande
- 1. Aggiungere un secondo LED e farlo brillare ogni 500ms
- mentre il primo brilla ogni 1000ms
- */
+++ /dev/null
-/* Blink con state machine
-
- Accendere e spegnere un led utilizzando una state machine
-*/
-
-// These variables store the flash pattern
-// and the current state of the LED
-
-int ledPin = 13; // the number of the LED pin
-int ledState = LOW; // ledState used to set the LED
-unsigned long previousMillis = 0; // will store last time LED was updated
-long interval = 1000; //
-
-void setup()
-{
- // set the digital pin as output:
- pinMode(ledPin, OUTPUT);
-}
-
-void loop()
-{
- // check to see if it's time to change the state of the LED
- unsigned long currentMillis = millis();
- if((ledState == HIGH) && (currentMillis - previousMillis >= interval))
- {
- ledState = !ledState ; // Inverti il LED
- previousMillis = currentMillis; // Remember the time
- digitalWrite(ledPin, ledState); // Update the actual LED
-
- }
- else if((ledState == LOW) && (currentMillis - previousMillis >= interval))
- {
- ledState = !ledState ; // Inverti il LED
- previousMillis = currentMillis; // Remember the time
- digitalWrite(ledPin, ledState); // Update the actual LED
- }
-
-
-}
-
-/*
-1. Modificare il codice in modo che si possa precisare il tempo di HIGH
- e il tempo di LOW
-2. Aggiungere un LED che brilli ogni 500ms
- */
-
+++ /dev/null
-/* Blink con state machine
-
- Accendere e spegnere un led utilizzando una state machine
- specificando sia il tempo accensione che di spegnimento.
-
-*/
-
-// These variables store the flash pattern
-// and the current state of the LED
-
-int ledPin = 13; // the number of the LED pin
-int ledState = LOW; // ledState used to set the LED
-unsigned long previousMillis = 0; // will store last time LED was updated
-long intervalHigh = 1000; //
-long intervalLow = 200; //
-
-void setup()
-{
- // set the digital pin as output:
- pinMode(ledPin, OUTPUT);
-}
-
-void loop()
-{
- // check to see if it's time to change the state of the LED
- unsigned long currentMillis = millis();
- if((ledState == HIGH) && (currentMillis - previousMillis >= intervalHigh))
- {
- ledState = !ledState ; // Inverti il LED
- previousMillis = currentMillis; // Remember the time
- digitalWrite(ledPin, ledState); // Update the actual LED
-
- }
- else if((ledState == LOW) && (currentMillis - previousMillis >= intervalLow))
- {
- ledState = !ledState ; // Inverti il LED
- previousMillis = currentMillis; // Remember the time
- digitalWrite(ledPin, ledState); // Update the actual LED
- }
-
-
-}
-
-/*
-1. Modificare il codice in modo che si possa precisare il tempo di HIGH
- e il tempo di LOW
-2. Aggiungere un LED che brilli ogni 500ms
- */
-
+++ /dev/null
-/* Blink con state machine: due led
-
- Accendere e spegnere due led utilizzando una state machine
- specificando sia il tempo accensione che di spegnimento.
-
- */
-
-// These variables store the flash pattern
-// and the current state of the LED
-
-int ledPinA = 13; // the number of the LED pin
-int ledStateA = LOW; // ledState used to set the LED
-unsigned long previousMillisA = 0; // will store last time LED was updated
-long intervalHighA = 1000; //
-long intervalLowA = 200; //
-
-int ledPinB = 12; // the number of the LED pin
-int ledStateB = LOW; // ledState used to set the LED
-unsigned long previousMillisB = 0; // will store last time LED was updated
-long intervalHighB = 500; //
-long intervalLowB = 100; //
-
-void setup()
-{
- // set the digital pin as output:
- pinMode(ledPinA, OUTPUT);
- pinMode(ledPinB, OUTPUT);
-}
-
-void loop()
-{
- // Primo LED
- // check to see if it's time to change the state of the LED
- unsigned long currentMillis = millis();
- if((ledStateA == HIGH) && (currentMillis - previousMillisA >= intervalHighA))
- {
- ledStateA = 1 - ledStateA ; // Inverti il LED
- previousMillisA = currentMillis; // Remember the time
- digitalWrite(ledPinA, ledStateA); // Update the actual LED
-
- }
- else if((ledStateA == LOW) && (currentMillis - previousMillisA >= intervalLowA))
- {
- ledStateA = 1 - ledStateA ; // Inverti il LED
- previousMillisA = currentMillis; // Remember the time
- digitalWrite(ledPinA, ledStateA); // Update the actual LED
- }
-
-// Secondo LED
-
- // check to see if it's time to change the state of the LED
- unsigned long currentMillis = millis();
- if((ledStateB == HIGH) && (currentMillis - previousMillisB >= intervalHighB))
- {
- ledStateB = 1 - ledStateB ; // Inverti il LED
- previousMillisB = currentMillis; // Remember the time
- digitalWrite(ledPinB, ledStateB); // Update the actual LED
-
- }
- else if((ledStateB == LOW) && (currentMillis - previousMillisB >= intervalLowB))
- {
- ledStateB = 1 - ledStateB ; // Inverti il LED
- previousMillisB = currentMillis; // Remember the time
- digitalWrite(ledPinA, ledStateB); // Update the actual LED
- }
-}
-
-/* Domande
- 1. E' possibile razzinalizzare il codice utilizzando una funzione?
-
-
- Link:
- - Utilizzare codice a oggetti per ottimizzare il codice:
- https://learn.adafruit.com/multi-tasking-the-arduino-part-1?view=all#a-classy-solution
- */
-
-