}
/* Domande:
+ 1. Come scrivere le istruzioni analog Write in modo da sottrarre i valori?
+ 2. Accendere il LED nei vari colori
+ - http://i.stack.imgur.com/LcBvQ.gif
+ Soluzione: vedi lo sketch rgb_1_all_color
- 1. Accendere il LED nei vari colori
- 2. Come scrivere le istruzioni analog Write in modo da sottrarre i valori?
3. Scrivere una funzione che accetti 3 parametri per impostare i colori
- 4. Scrivere una funzione che accetti i colori in esadecimale
- - http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php
- 5. Scrivere una funzione che accetti come parametro il nome del colore
+ 4. Scrivere una funzione che accetti come parametro il nome del colore
es "blue" e imposti il LED.
+
+ Eventuale:
+ 5. Scrivere una funzione che accetti i colori in esadecimale
+ - http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php
*/
void loop()
{
- setColor(0xFF,0x00,0x00) ; // imposta il LED in rosso
+ setColor(255,0,0) ; // imposta il LED in rosso
+ //setColor(0xFF,0x00,0x00) ; // imposta il LED in rosso in esadecimale
// setName("green") ;
}
- /*
+/*
Adafruit Arduino - Lesson 3. RGB LED
-
- RGB LED: rotazione tra tutti i colori.
-
- Schema: http://lab.piffa.net/schemi/rgb.jpg
-
- */
-
- int redPin = 11;
- int greenPin = 10;
- int bluePin = 9;
-
- //uncomment this line if using a Common Anode LED
- //#define COMMON_ANODE
-
- void setup()
- {
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
- }
-
- void loop()
- {
- setColor(255, 0, 0); // red
- delay(1000);
- setColor(0, 255, 0); // green
- delay(1000);
- setColor(0, 0, 255); // blue
- delay(1000);
- setColor(255, 255, 0); // yellow
- delay(1000);
- setColor(80, 0, 80); // purple
- delay(1000);
- setColor(0, 255, 255); // aqua
- delay(1000);
- }
-
- void setColor(int red, int green, int blue)
- {
- #ifdef COMMON_ANODE
- red = 255 - red;
- green = 255 - green;
- blue = 255 - blue;
- #endif
- analogWrite(redPin, red);
- analogWrite(greenPin, green);
- analogWrite(bluePin, blue);
- }
-
-
+
+ RGB LED: rotazione tra tutti i colori.
+
+ Schema: http://lab.piffa.net/schemi/rgb.jpg
+
+ */
+
+int redPin = 11;
+int greenPin = 10;
+int bluePin = 9;
+
+//uncomment this line if using a Common Anode LED
+//#define COMMON_ANODE
+
+void setup()
+{
+ pinMode(redPin, OUTPUT);
+ pinMode(greenPin, OUTPUT);
+ pinMode(bluePin, OUTPUT);
+}
+
+void loop()
+{
+ setColor(255, 0, 0); // red
+ delay(1000);
+ setColor(0, 255, 0); // green
+ delay(1000);
+ setColor(0, 0, 255); // blue
+ delay(1000);
+ setColor(255, 255, 0); // yellow
+ delay(1000);
+ setColor(80, 0, 80); // purple
+ delay(1000);
+ setColor(0, 255, 255); // aqua
+ delay(1000);
+}
+
+void setColor(int red, int green, int blue)
+{
+#ifdef COMMON_ANODE
+ red = 255 - red;
+ green = 255 - green;
+ blue = 255 - blue;
+#endif
+ analogWrite(redPin, red);
+ analogWrite(greenPin, green);
+ analogWrite(bluePin, blue);
+}
+
+
+
--- /dev/null
+/* Blink without Delay
+
+ Turns on and off a light emitting diode(LED) connected to a digital
+ pin, without using the delay() function. This means that other code
+ can run at the same time without being interrupted by the LED code.
+
+ The circuit:
+ * LED attached from pin 13 to ground.
+ * Note: on most Arduinos, there is already an LED on the board
+ that's attached to pin 13, so no hardware is needed for this example.
+
+
+ created 2005
+ by David A. Mellis
+ modified 8 Feb 2010
+ by Paul Stoffregen
+
+ This example code is in the public domain.
+
+
+ http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
+ */
+
+// constants won't change. Used here to
+// set pin numbers:
+const int ledPin = 13; // the number of the LED pin
+
+// Variables will change:
+int ledState = LOW; // ledState used to set the LED
+long previousMillis = 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 interval = 1000; // interval at which to blink (milliseconds)
+
+void setup() {
+ // set the digital pin as output:
+ pinMode(ledPin, 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.
+ unsigned long currentMillis = millis();
+
+ if(currentMillis - previousMillis > interval) {
+ // save the last time you blinked the LED
+ previousMillis = currentMillis;
+
+ // if the LED is off turn it on and vice-versa:
+ if (ledState == LOW)
+ ledState = HIGH;
+ else
+ ledState = LOW;
+
+ // set the LED with the ledState of the variable:
+ digitalWrite(ledPin, ledState);
+ }
+}
+
+/* Domande
+ 1. Aggioungere un LED che brilli ogni 500ms
+ 2. E' possibile cambiare gli intervalli dei due LED?
+ */
--- /dev/null
+/* Blink without Delay
+
+ Turns on and off a light emitting diode(LED) connected to a digital
+ pin, without using the delay() function. This means that other code
+ can run at the same time without being interrupted by the LED code.
+
+ The circuit:
+ * LED attached from pin 13 to ground.
+ * Note: on most Arduinos, there is already an LED on the board
+ that's attached to pin 13, so no hardware is needed for this example.
+
+
+ created 2005
+ by David A. Mellis
+ modified 8 Feb 2010
+ by Paul Stoffregen
+
+ This example code is in the public domain.
+
+
+ http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
+ */
+
+// 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
+
+// Variables will change:
+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)
+
+void setup() {
+ // set the digital pin as output:
+ pinMode(ledA, OUTPUT);
+ pinMode(ledB, 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.
+ unsigned long currentMillis = millis();
+
+ if(currentMillis - previousMillisA > intervalA) {
+ // save the last time you blinked the LED
+ previousMillisA = currentMillis;
+
+ // 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);
+ }
+
+ if(currentMillis - previousMillisB > intervalB) {
+ // save the last time you blinked the LED
+ previousMillisB = currentMillis;
+
+ // 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
+ Turns on an LED on for one second, then off for one second, repeatedly.
+
+ This example code is in the public domain.
+ */
+
+// Pin 13 has an LED connected on most Arduino boards.
+// give it a name:
+int led = 13;
+
+// the setup routine runs once when you press reset:
+void setup() {
+ // initialize the digital pin as an output.
+ pinMode(led, OUTPUT);
+}
+
+// the loop routine runs over and over again forever:
+void loop() {
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
+ delay(1000); // wait for a second
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
+ delay(1000); // wait for a second
+}
+
+/* Domande
+ 1. Aggiungere un secondo LED e farlo brillare ogni 500ms
+ mentre il primo brilla ogni 1000ms
+ 2. Cosa succederebbe se dovessi anche leggere un input da un sensore / bottone?
+ */
+
--- /dev/null
+/*
+ Blink
+ Turns on an LED on for one second, then off for one second, repeatedly.
+
+ This example code is in the public domain.
+ */
+
+// Pin 13 has an LED connected on most Arduino boards.
+// give it a name:
+int ledA = 13; //Primo LED
+int ledB = 12; //Secondo LED
+
+// the setup routine runs once when you press reset:
+void setup() {
+ // initialize the digital pin as an output.
+ pinMode(ledA, OUTPUT);
+ pinMode(ledB, OUTPUT);
+}
+
+// the loop routine runs over and over again forever:
+void loop() {
+ digitalWrite(ledA, HIGH); // turn the LED on (HIGH is the voltage level)
+ digitalWrite(ledB, HIGH);
+
+ delay(500);
+ digitalWrite(ledB, LOW);
+
+ delay(500);
+ digitalWrite(ledA, LOW);
+ digitalWrite(ledB, HIGH);
+
+
+ delay(500);
+ digitalWrite(ledB, LOW);
+
+ delay(500);
+ digitalWrite(ledA, LOW);
+ digitalWrite(ledB, LOW);
+ ;
+}
+
+/* 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
+ */
+
+
--- /dev/null
+/*
+ Debug con macro per il preprocessore
+
+ Blink v1
+
+ Accensione e spegnimanto di un LED utilizzando variabili
+ per impostare la velocita' del lampeggio.
+
+ Turns on an LED on for one second, then off for one second, repeatedly.
+
+ This example code is in the public domain.
+ */
+
+// Pin 13 has an LED connected on most Arduino boards.
+// give it a name:
+int led = 13;
+int breve = 200; // Variabile richiambile nel corso dell'esecuzione
+
+#define DEBUG
+
+
+// the setup routine runs once when you press reset:
+void setup() {
+ // initialize the digital pin as an output.
+ pinMode(led, OUTPUT);
+ Serial.begin(9600);
+}
+
+// the loop routine runs over and over again forever:
+void loop() {
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
+#ifdef DEBUG
+ Serial.println("High");
+#endif
+
+ delay(breve); // wait for a second
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
+#ifdef DEBUG
+ Serial.println("Low");
+#endif
+ delay(breve); // wait for a second
+}
+
+
+
+
--- /dev/null
+/*
+ Debug con macro per il preprocessore
+
+ Blink v1
+
+ Accensione e spegnimanto di un LED utilizzando variabili
+ per impostare la velocita' del lampeggio.
+
+ Turns on an LED on for one second, then off for one second, repeatedly.
+
+ This example code is in the public domain.
+ */
+
+// Pin 13 has an LED connected on most Arduino boards.
+// give it a name:
+int led = 13;
+int breve = 200; // Variabile richiambile nel corso dell'esecuzione
+
+#define DEBUG
+// Debug
+#ifdef DEBUG
+ #define DEBUG_PRINT(x) Serial.print (x)
+ #define DEBUG_PRINTDEC(x) Serial.print (x, DEC)
+ #define DEBUG_PRINTLN(x) Serial.println (x)
+#else
+ #define DEBUG_PRINT(x)
+ #define DEBUG_PRINTDEC(x)
+ #define DEBUG_PRINTLN(x)
+#endif
+
+// the setup routine runs once when you press reset:
+void setup() {
+ // initialize the digital pin as an output.
+ pinMode(led, OUTPUT);
+ Serial.begin(9600);
+}
+
+// the loop routine runs over and over again forever:
+void loop() {
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
+ DEBUG_PRINTLN("Stato HIGHT");
+ delay(breve); // wait for a second
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
+ DEBUG_PRINTLN("Stato LOW");
+ delay(breve); // wait for a second
+}
+
+
+++ /dev/null
-/*
- Debug con macro per il preprocessore
-
- Blink v1
-
- Accensione e spegnimanto di un LED utilizzando variabili
- per impostare la velocita' del lampeggio.
-
- Turns on an LED on for one second, then off for one second, repeatedly.
-
- This example code is in the public domain.
- */
-
-// Pin 13 has an LED connected on most Arduino boards.
-// give it a name:
-int led = 13;
-int breve = 200; // Variabile richiambile nel corso dell'esecuzione
-
-#define DEBUG
-// Debug
-#ifdef DEBUG
- #define DEBUG_PRINT(x) Serial.print (x)
- #define DEBUG_PRINTDEC(x) Serial.print (x, DEC)
- #define DEBUG_PRINTLN(x) Serial.println (x)
-#else
- #define DEBUG_PRINT(x)
- #define DEBUG_PRINTDEC(x)
- #define DEBUG_PRINTLN(x)
-#endif
-
-// the setup routine runs once when you press reset:
-void setup() {
- // initialize the digital pin as an output.
- pinMode(led, OUTPUT);
- Serial.begin(9600);
-}
-
-// the loop routine runs over and over again forever:
-void loop() {
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- DEBUG_PRINTLN("Stato HIGHT");
- delay(breve); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- DEBUG_PRINTLN("Stato LOW");
- delay(breve); // wait for a second
-}
-
-