From: eaman Date: Thu, 15 Dec 2016 16:29:56 +0000 (+0100) Subject: oggi X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=commitdiff_plain;h=81656cead477f7c23b904e2e2ec07bfe645bace2 oggi --- diff --git a/multitasking/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino b/multitasking/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino new file mode 100644 index 0000000..21c26eb --- /dev/null +++ b/multitasking/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino @@ -0,0 +1,77 @@ +/* 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 + 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 + modified by eaman + + 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; // Primo LED +const int ledB = 12; // Secondo LED + +// Variabbili di stato +int ledStateA = LOW; // ledState used to set the LED +//int ledStateB = LOW; // Not used + +long previousMillisA = 0; // will store last time LED was updated +unsigned 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() +{ +// Primo LED + 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: + ledStateA = !ledStateA; + // set the LED with the ledState of the variable: + digitalWrite(ledA, ledStateA); + } + +// Secondo LED: contratta + if (millis() - previousMillisB > intervalB) { + digitalWrite(ledB, !digitalRead(ledB)); + previousMillisB = millis(); + } +} + +/* Domande + 1. Provare a isolare il codice per accendere ogni singolo led in una funzione: + - Quali variabili determinano il comportamento del LED? + - Come cambiano durante il corso dello script? + - Sono globali o locali? + - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione? + */ + + + diff --git a/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino index 824c53f..bce667d 100644 --- a/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino +++ b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino @@ -18,7 +18,7 @@ long previousMillisA = 0; // will store last time LED was updated 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 +unsigned long previousMillisB = 0; // millis() ritorna sempre un unsigned long void setup() { // set the digital pin as output: @@ -51,7 +51,7 @@ void lightLedA (int interval) { void lightLedB (int interval) { // Illumina il ledB secondo un intervallo passato come argomento - if (millis() > previousMillisB + interval) { + if (millis() - previousMillisB > interval) { previousMillisB = millis(); digitalWrite(ledB, !digitalRead(ledB)); // Leggiamo direttamente il registro di ledB e scriviamo il suo opposto, diff --git a/oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino b/oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino new file mode 100644 index 0000000..b7a04a9 --- /dev/null +++ b/oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino @@ -0,0 +1,82 @@ +/* Blink without Delay + + Utilizziamo la funzione millis() al posto di delay() + per poter gestire il lampeggio di un LED senza bloccare + il processore. + + Questo esercizio e' strutturato in una serie di passaggi incrementali + nei quali una versione minimale si evolve per introdurre + programmazione ad oggetti, interrupts, pointers. + + 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 + 2015 modified by Andrea Manni + + 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; + +// 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. +const 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. + + if (millis() > previousMillis + interval) { + // Aggiorniamo il contatore previousMillis + previousMillis = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledState == LOW) + ledState = HIGH; + else + ledState = LOW; + // 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: + digitalWrite(ledPin, ledState); + } +} + +/* Domande + 1. Aggioungere un LED che brilli ogni 500ms: iniziare pensando + a quali variabili gestiscono l'attuale LED e a quali si + dovranno aggiungere. + 2. E' ora agevole cambiare gli intervalli dei due LED? + Modificare gli intervalli dei due led (es 500ms - 320ms) + */ diff --git a/oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino b/oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino new file mode 100644 index 0000000..2100ead --- /dev/null +++ b/oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino @@ -0,0 +1,88 @@ +/* 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 + 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 + modified by eaman + + 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; // Primo LED +const int ledB = 12; // Secondo LED + +// 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) + +void setup() { + // set the digital pin as output: + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() +{ +// Primo LED + 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); + } + +// Secondo LED + 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. Provare a isolare il codice per accendere ogni singolo led in una funzione: + - Quali variabili determinano il comportamento del LED? + - Come cambiano durante il corso dello script? + - Sono globali o locali? + - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione? + */ + + + diff --git a/oggi/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino b/oggi/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino new file mode 100644 index 0000000..21c26eb --- /dev/null +++ b/oggi/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino @@ -0,0 +1,77 @@ +/* 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 + 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 + modified by eaman + + 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; // Primo LED +const int ledB = 12; // Secondo LED + +// Variabbili di stato +int ledStateA = LOW; // ledState used to set the LED +//int ledStateB = LOW; // Not used + +long previousMillisA = 0; // will store last time LED was updated +unsigned 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() +{ +// Primo LED + 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: + ledStateA = !ledStateA; + // set the LED with the ledState of the variable: + digitalWrite(ledA, ledStateA); + } + +// Secondo LED: contratta + if (millis() - previousMillisB > intervalB) { + digitalWrite(ledB, !digitalRead(ledB)); + previousMillisB = millis(); + } +} + +/* Domande + 1. Provare a isolare il codice per accendere ogni singolo led in una funzione: + - Quali variabili determinano il comportamento del LED? + - Come cambiano durante il corso dello script? + - Sono globali o locali? + - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione? + */ + + + diff --git a/oggi/RGB_LED/README b/oggi/RGB_LED/README deleted file mode 100644 index 133660a..0000000 --- a/oggi/RGB_LED/README +++ /dev/null @@ -1,2 +0,0 @@ -Ottima lezione in italiano: -- http://www.maffucci.it/2014/09/27/arduino-lezione-09-uso-di-led-rgb-parte-1/ diff --git a/oggi/RGB_LED/rgb_0/rgb_0.ino b/oggi/RGB_LED/rgb_0/rgb_0.ino deleted file mode 100644 index d69d451..0000000 --- a/oggi/RGB_LED/rgb_0/rgb_0.ino +++ /dev/null @@ -1,46 +0,0 @@ -/* - Adafruit Arduino - Lesson 3. RGB LED - - RGB LED: mpostare i colori per un LED RGB - common anode - - Schema: http://lab.piffa.net/schemi/rgb.jpg - */ - -int redPin = 11; // 2v a 20ma: che resistenza dovro usare? -int greenPin = 10; // 3.5v a 20ma: che resistenza dovro usare? -int bluePin = 9; // 3.5v a 20ma: che resistenza dovro usare? - - - -void setup() -{ - pinMode(redPin, OUTPUT); - pinMode(greenPin, OUTPUT); - pinMode(bluePin, OUTPUT); -} - -void loop() -{ - analogWrite(redPin, 255); - analogWrite(greenPin,255); - analogWrite(bluePin, 255); -} - -/* 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 - - 3. Scrivere una funzione che accetti 3 parametri per impostare i colori - 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 - */ - - - diff --git a/oggi/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino b/oggi/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino deleted file mode 100644 index 5a5c971..0000000 --- a/oggi/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino +++ /dev/null @@ -1,71 +0,0 @@ -/* - Adafruit Arduino - Lesson 3. RGB LED - - RGB LED: mpostare i colori per un LED RGB - common anode - */ - -int redPin = 11; // 2v a 20ma: che resistenza dovro usare? -int greenPin = 10; // 3.5v a 20ma: che resistenza dovro usare? -int bluePin = 9; // 3.5v a 20ma: che resistenza dovro usare? - - -void setup() -{ - pinMode(redPin, OUTPUT); - pinMode(greenPin, OUTPUT); - pinMode(bluePin, OUTPUT); -} - -void loop() -{ - - rendiBlu(); - delay(1000); - //setColor(255,0,0) ; // imposta il LED in rosso - //setColor(0xFF,0x00,0x00) ; // imposta il LED in rosso in esadecimale - // setName("green") ; - // delay(1000); -} - -// Funzioni: - -void rendiBlu() { - // Accende di Blu - - analogWrite(redPin, 255 ); - analogWrite(greenPin, 255 ); - analogWrite(bluePin, 0 ); - } - -void setColor(int red, int green, int blue) { -// Imposta i colori di un LED RGB Common Anodote -// in esadecimale - - analogWrite(redPin, 255 -red); - analogWrite(greenPin, 255 - green); - analogWrite(bluePin, 255 - blue); -} - -void setName(String colorName) { -// Imposta i colori di un LED RGB Common Anodote -// tramite una stringa - - if (colorName == "red") { - analogWrite(redPin, 0 ); - analogWrite(greenPin, 255 ); - analogWrite(bluePin, 255 ); - } - else if (colorName == "green") { - analogWrite(redPin, 255 ); - analogWrite(greenPin, 0 ); - analogWrite(bluePin, 255 ); - } - // ... -} -/* Hints: - -1. Per usare un solo valore esadecimale per settare i colori: - - http://ardx.org/src/code/CIRC12-code-MB-SPAR.txt - - */ diff --git a/oggi/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino b/oggi/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino deleted file mode 100644 index a53a02a..0000000 --- a/oggi/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino +++ /dev/null @@ -1,54 +0,0 @@ -/* - 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); -} - - - - diff --git a/oggi/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino b/oggi/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino deleted file mode 100644 index 8e712fc..0000000 --- a/oggi/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino +++ /dev/null @@ -1,69 +0,0 @@ -// RGB LED PWM transizione - -// Transizione di un LED RGB tra rosso - blue -verde -// tramite PWM - -// This is meant for a Common Anodote RGB LED -// See all those (255 - val). - - -// Schema: http://lab.piffa.net/schemi/rgb.jpg - - -#define GREEN 10 -#define BLUE 9 -#define RED 11 -#define delayTime 20 - -void setup() { - - pinMode(GREEN, OUTPUT); - pinMode(BLUE, OUTPUT); - pinMode(RED, OUTPUT); - digitalWrite(GREEN, HIGH); - digitalWrite(BLUE, HIGH); - digitalWrite(RED, HIGH); -} - -int redVal; -int blueVal; -int greenVal; - -void loop() { - - int redVal = 255; - int blueVal = 0; - int greenVal = 0; - for( int i = 0 ; i < 255 ; i += 1 ){ - greenVal += 1; - redVal -= 1; - analogWrite( GREEN, 255 - greenVal ); - analogWrite( RED, 255 - redVal ); - - delay( delayTime ); - } - - redVal = 0; - blueVal = 0; - greenVal = 255; - for( int i = 0 ; i < 255 ; i += 1 ){ - blueVal += 1; - greenVal -= 1; - analogWrite( BLUE, 255 - blueVal ); - analogWrite( GREEN, 255 - greenVal ); - - delay( delayTime ); - } - - redVal = 0; - blueVal = 255; - greenVal = 0; - for( int i = 0 ; i < 255 ; i += 1 ){ - redVal += 1; - blueVal -= 1; - analogWrite( RED, 255 - redVal ); - analogWrite( BLUE, 255 - blueVal ); - - delay( delayTime ); - } -} diff --git a/oggi/RGB_LED/rgb_3_ReadASCIIString/rgb_3_ReadASCIIString.ino b/oggi/RGB_LED/rgb_3_ReadASCIIString/rgb_3_ReadASCIIString.ino deleted file mode 100644 index 13cf2b8..0000000 --- a/oggi/RGB_LED/rgb_3_ReadASCIIString/rgb_3_ReadASCIIString.ino +++ /dev/null @@ -1,81 +0,0 @@ -/* - Reading a serial ASCII-encoded string. - - Beware: set monitor to NL NewLine only - - This sketch demonstrates the Serial parseInt() function. - It looks for an ASCII string of comma-separated values. - It parses them into ints, and uses those to fade an RGB LED. - - - - Once you have programmed the Arduino, open your Serial minitor. - Make sure you have chosen to send a newline character when sending a message. - Enter values between 0-255 for the lights in the following format : - Red,Green,Blue. - - Seriously: did you set the Newline setting in the monitor? - - - Once you have sent the values to the Arduino, - the attached LED will turn the color you specified, - and you will receive the HEX values in the serial monitor. - - created 13 Apr 2012 - by Tom Igoe - - This example code is in the public domain. - - - - Schema: http://lab.piffa.net/schemi/rgb.jpg - - */ - -// pins for the LEDs: -const int redPin = 11; -const int greenPin = 10; -const int bluePin = 9; - -void setup() { - // initialize serial: - Serial.begin(9600); - // make the pins outputs: - pinMode(redPin, OUTPUT); - pinMode(greenPin, OUTPUT); - pinMode(bluePin, OUTPUT); - -} - -void loop() { - // if there's any serial available, read it: - while (Serial.available() > 0) { - - // look for the next valid integer in the incoming serial stream: - int red = Serial.parseInt(); - // do it again: - int green = Serial.parseInt(); - // do it again: - int blue = Serial.parseInt(); - - // look for the newline. That's the end of your - // sentence: - if (Serial.read() == '\n') { - // constrain the values to 0 - 255 and invert - // if you're using a common-cathode LED, just use "constrain(color, 0, 255);" - red = 255 - constrain(red, 0, 255); - green = 255 - constrain(green, 0, 255); - blue = 255 - constrain(blue, 0, 255); - - // fade the red, green, and blue legs of the LED: - analogWrite(redPin, red); - analogWrite(greenPin, green); - analogWrite(bluePin, blue); - - // print the three numbers in one string as hexadecimal: - Serial.print(red, HEX); - Serial.print(green, HEX); - Serial.println(blue, HEX); - } - } -} diff --git a/oggi/RGB_LED/rgb_4_array/rgb_4_array.ino b/oggi/RGB_LED/rgb_4_array/rgb_4_array.ino deleted file mode 100644 index 39060d4..0000000 --- a/oggi/RGB_LED/rgb_4_array/rgb_4_array.ino +++ /dev/null @@ -1,35 +0,0 @@ -/* - RGB Array - - RGB LED: impostare i colori per un LED RGB - common anode tramite array - - Schema: http://lab.piffa.net/schemi/rgb.jpg - */ - -byte pin[3] = {11, 10, 9}; // 2v a 20ma: che resistenza dovro usare? -byte color[3] = {255, 255, 255}; - - - -void setup() -{ - for (byte i = 0; i < 4; i++) { - pinMode(pin[i], OUTPUT); - } -} - - -void loop() -{ - analogWrite(pin[0], color[0]); - analogWrite(pin[1], color[1]); - analogWrite(pin[2], color[2]); -} - -/* Domande: - 1. Che differenza c'e' tra le variabili dei pin e dei colori? - */ - - - diff --git a/oggi/RGB_LED/rgb_5_struct/rgb_5_struct.ino b/oggi/RGB_LED/rgb_5_struct/rgb_5_struct.ino deleted file mode 100644 index dbe0d05..0000000 --- a/oggi/RGB_LED/rgb_5_struct/rgb_5_struct.ino +++ /dev/null @@ -1,80 +0,0 @@ -/* - RGB struct LED - - RGB LED: mpostare i colori per un LED RGB - common anode utilizzando uno struct - - Schema: http://lab.piffa.net/schemi/rgb.jpg - */ - -byte pin[3] = {11, 10, 9}; // 2v a 20ma: che resistenza dovro usare? - -struct color { - byte red ; - byte blue; - byte green; - -} ; - -color led; - - -void setup() -{ - for (byte i = 0; i < 4; i++) { - pinMode(pin[i], OUTPUT); - } -} - - -void loop() -{ - analogWrite(pin[0], led.red); - analogWrite(pin[1], led.green); - analogWrite(pin[2], led.blue); -} - -/* Domande: - 1. Potrei mettere i numeri dei PIN dentro lo stesso struct? - 2. Quale differenza funzionale c'e' tra i numeri dei PIN e i colori? - 2.1 Quando devo settare i pin e quante volte? - 2.2 Quando imposto i colori e quanto spesso? - - 3. Sarebbe piu' elegante scrivere una funzione per gestire il setup dei PIN - e impostare i colori? - 3.1 Servira' una o piu' funzioni? - 4. Esiste un costrutto che mi permetta di legare le differenti proprieta' - del LED RGB e contemporaneamente raggruppare le funzioni che tipicamente uso con questo? - */ - - -/* Risposte: - * 1. - -struct ledRGB { - byte rPin ; // PINS - byte gPin; - byte bPin; - byte blue; // Colors - byte green; - byte red; -}; - -ledRGB led {0,255,255,9,10,11}; - - - -void setup() -{ - pinMode(led.Pin, OUTPUT); - pinMode(led.Pin, OUTPUT); - pinMode(led.Pin, OUTPUT); -} - -void loop() -{ - analogWrite(led.rPin,led.red ); - analogWrite(led.gPin,led.green); - analogWrite(led.bPin,led.blue); -} -*/ diff --git a/oggi/RGB_LED/rgb_6_quasi_obj/rgb_6_quasi_obj.ino b/oggi/RGB_LED/rgb_6_quasi_obj/rgb_6_quasi_obj.ino deleted file mode 100644 index 967cf25..0000000 --- a/oggi/RGB_LED/rgb_6_quasi_obj/rgb_6_quasi_obj.ino +++ /dev/null @@ -1,86 +0,0 @@ -/* - RGB Object (quasi!) - - Gestione di un LED RGB tramite programmazione a oggetti - Esercizio intermedio: manca constructor. - - Schema: http://lab.piffa.net/schemi/rgb.jpg - */ - -class RGBLed { - // Classe rappresentativa di un LED RGB - - byte redPin =11; - byte greenPin =10; - byte bluePin =9; - byte redValue ; - byte greenValue ; - byte blueValue ; - - public: - void Arrossa () { - // Metodo = funzione dell'oggetto - // Imposta il colore di un LED RGB a rosso - - analogWrite(redPin, 0); - analogWrite(greenPin, 255); - analogWrite(bluePin, 255); - } - - void SetColor (byte r, byte g, byte b) { - // Imposta il colore di un LED RGB - - analogWrite(redPin, r); - analogWrite(greenPin, g); - analogWrite(bluePin, b); - } -} led; // Dichiariamo un instanza dell'oggetto - - -void setup() { - pinMode(9, OUTPUT); - pinMode(10, OUTPUT); - pinMode(11, OUTPUT); -} - -void loop() { - led.Arrossa(); - delay(1000); - led.SetColor(255, 0, 255) ; // Mettiamo il LED in Green - delay(1000); - -} - -/* Domande - 1. Potrei istanziare un altro oggetto RGBLed con PIN diversi? - 2. Posso accedere (leggere / modificare) le proprieta' dell'oggetto? - 3. A quali di queste proprieta' potrei voler accedere? - 4. Devo comunque abilitare i PIN come OUTPUT nel setup(): sarebbe possibile - farlo tramite una funione, magari automaticamente? - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - 1. Mi serve una funzione che permetta di assegnare delle proprieta come i PIN - 2. Al momento no: sono tutte PRIVATE. provate a spostarne alcune sotto - la dichiarazione PUBLIC . - 3. Ad es. i colori che vado a modificare, i numeri dei pin su suppone - debbano restare immutati. - 4. Per usare una funzione nel setup() bisognerebbe rendere pubbliche - le proprieta' che indicano i PIM. - Meglio sarebbe avere una funzione che venga invocata una sola volta - al momento di instanziare l'oggetto. Un CONSTRUCTOR . - */ - diff --git a/oggi/RGB_LED/rgb_7_obj/rgb_7_obj.ino b/oggi/RGB_LED/rgb_7_obj/rgb_7_obj.ino deleted file mode 100644 index e63ace4..0000000 --- a/oggi/RGB_LED/rgb_7_obj/rgb_7_obj.ino +++ /dev/null @@ -1,78 +0,0 @@ -/* - RGB Object - - Gestione di un LED RGB tramite programmazione a oggetti - - Schema: http://lab.piffa.net/schemi/rgb.jpg - */ - -class RGBLed { - // Classe rappresentativa di un LED RGB - - // Private properties, le proprieta' sono private per default - byte redPin ; - byte greenPin ; - byte bluePin ; - - public: - // Public properties - byte redValue ; - byte greenValue ; - byte blueValue ; - - // Constructor: come viene instanziato un oggetto facente parte della classe - RGBLed(byte pinR, byte pinG, byte pinB) - { - // Carichiamo i valori dei PIN dentro alle proprieta' - redPin = pinR ; - greenPin = pinG ; - bluePin = pinB ; - - // Equvalente del Setup() per inizializzare i PIN - pinMode(redPin, OUTPUT); - pinMode(greenPin, OUTPUT); - pinMode(greenPin, OUTPUT); - } - - void Arrossa () { - // Metodo = funzione dell'oggetto - // Imposta il colore di un LED RGB a rosso - - analogWrite(redPin, 0); - analogWrite(greenPin, 255); - analogWrite(bluePin, 255); - } - - void SetColor (byte r, byte g, byte b) { - // Imposta il colore di un LED RGB - - analogWrite(redPin, r); - analogWrite(greenPin, g); - analogWrite(bluePin, b); - } -}; - -// Instanziamo un LED -RGBLed led(11, 10, 9); -/* L'oggetto viene istanziato qui e non nella funzione di setup() - perche' altrimenti la sua esistenza sarebbe legata solo - al contesto (scope) del setup(), non sarebbe disponibile nel loop() - */ - -void setup() { - // I PIN mode vengono settati dal constructor -} - -void loop() { - led.Arrossa(); - delay(1000); - led.SetColor(255, 0, 255) ; // Mettiamo il LED in Green - delay(1000); - -} - -/* Domande - 1. Provate ad accedere (serial print oppure modificare) le proprieta private e pubbliche. - - */ - diff --git a/oggi/blink_0/blink_0.ino b/oggi/blink_0/blink_0.ino new file mode 100644 index 0000000..1b7c15b --- /dev/null +++ b/oggi/blink_0/blink_0.ino @@ -0,0 +1,29 @@ +/* + Blink v1 + + Accensione e spegnimanto di un LED utilizzando variabili + per impostare la velocita' del lampeggio. + + */ + +// Pin 13 ha un LED collegato di default +int led = 13; + +void setup() { + // Inizializziamo il PIN 13 come OUTPUT + pinMode(led, OUTPUT); +} + +void loop() { + digitalWrite(led, HIGH); + delay(1000); + digitalWrite(led, LOW); + delay(1000); +} + +/* 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? + */ + diff --git a/oggi/blink_0_soluzione/blink_0_soluzione.ino b/oggi/blink_0_soluzione/blink_0_soluzione.ino new file mode 100644 index 0000000..8c302d9 --- /dev/null +++ b/oggi/blink_0_soluzione/blink_0_soluzione.ino @@ -0,0 +1,81 @@ +/* + Blink due LED - Soluzione + + 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 + 1 | 0 | x + 0 | 1 x | x + 0 | 0 | x + + + */ + +// Pin 13 has an LED connected on most Arduino boards. +// give it a name: +const int ledA = 13; //Primo LED +const int ledB = 12; //Secondo LED, con resistenza + +// 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() { + // Primo periodo + digitalWrite(ledA, HIGH); // turn the LED on (HIGH is the voltage level) + digitalWrite(ledB, HIGH); + delay(500); // Minimo comun denominatore del periodo + + // Secondo periodo + //digitalWrite(ledA, HIGH); // ledA non cambia + digitalWrite(ledB, LOW); + delay(500); + + // Terzo periodo + digitalWrite(ledA, LOW); + digitalWrite(ledB, HIGH); + delay(500); + + // Quarto periodo + //digitalWrite(ledA, LOW); + digitalWrite(ledB, LOW); + delay(500); +} + +/* Domande + 1. Altro scenartio: fare brillare un LED ogni 300ms mentre il secondo brilla ogni 400m + 2. ...valutare come aggiungere un terzo LED, gestire altri intevalli. + */ + + + + + + + + + + + + + + + + + + + + diff --git a/oggi/button_1/button_1.ino b/oggi/button_1/button_1.ino deleted file mode 100644 index f409028..0000000 --- a/oggi/button_1/button_1.ino +++ /dev/null @@ -1,45 +0,0 @@ -/* - Input Condizionale - - Accensione e spegnimanto di un LED utilizzando un pin come input. - - Utilizzare un jumper per collegare il PIN Input - alternativamente a +5 o GND . - -Schema: -- http://lab.piffa.net/schemi/led_condizionale.png - - */ - -// Pin 13 has an LED connected on most Arduino boards. -// give it a name: -int led = 12; -int input = 2; - -// the setup routine runs once when you press reset: -void setup() { - // initialize the digital pin as an output. - pinMode(led, OUTPUT); // Il PIN e' attivato come output - pinMode(input, INPUT); // Il PIN e' attivato come output -} - -// the loop routine runs over and over again forever: -void loop() { - if (digitalRead(input) == HIGH) { // Verifica se il PIN input e' +5V - digitalWrite(led, HIGH); - } - if (digitalRead(input) == LOW) { // Verifica se il PIN input e' 0V - digitalWrite(led, LOW); - } -} - -/* Domande: - 1. Invertire il programma facendo in modo che il led si spenga - quando il bottone e' premuto. Considerare come ottenere lo stesso risultato - modificando il circuito. - 2. Modificare il programma per far brillare il led cinque volte al secondo - quando il bottone e' premuto. - 3. Si potrebbe usare un ciclo iterativo while invece che - un ciclo condizonale if? Che differenza c'e' tra il ciclo while, if e for? - 4. Domanda: cosa succede se il jumper input non e' collegato ne al +5 ne al ground? - */ diff --git a/oggi/button_state_3/button_state_3.ino b/oggi/button_state_3/button_state_3.ino deleted file mode 100644 index 2afa6d9..0000000 --- a/oggi/button_state_3/button_state_3.ino +++ /dev/null @@ -1,47 +0,0 @@ -/* - Stato di un bottone - - Legge lo stato di un input - - */ - -int switchPin = 2; // switch connesso al pin 2 - // Nota: le prossime due variabili sono - // solo "definite" e non "inizializzate" -int statoAttuale; // Variabile per leggere lo stato del bottone -int ultimoStato; // Variabile per registrare l'ultimo stato del bottone - -void setup() { - pinMode(switchPin, INPUT); // Set the switch pin as input - - Serial.begin(9600); // Set up serial communication at 9600bps - ultimoStato = digitalRead(switchPin); // read the initial state -} - - -void loop(){ - statoAttuale = digitalRead(switchPin); // Legge lo stato del bottone e - // lo resistra nella variabile - delay(20); // riduce l'effetto bounce - if (statoAttuale != ultimoStato) { - // verifica due condizioni che devono realizzarsi contemporaneamente - if (statoAttuale == HIGH) { // il bottone e' stato premuto - Serial.println("Bottone premuto"); - } - else { // il bottone non e' premuto... - Serial.println("Bottone rilasciato"); - } - } - - ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale -} - -/* Domande: - - 1. Cosa succde se non uso un delay(20) alla lettura del bottone? - 2. Implementare un LED che cambia stato quando viene premuto il bottone. - 3. Quanti stati ha il LED? - 4. Sarebbe possibile passare rapidamente da uno stato all'altro? - - */ - diff --git a/oggi/button_state_4_final/button_state_4_final.ino b/oggi/button_state_4_final/button_state_4_final.ino deleted file mode 100644 index 4c0593f..0000000 --- a/oggi/button_state_4_final/button_state_4_final.ino +++ /dev/null @@ -1,62 +0,0 @@ -/* - Stato di un bottone - - Legge lo stato di un input - - */ -const int led = 13; -const int buttonPin = 2; -boolean statoAttuale; // Variabile per leggere lo stato del bottone -boolean ultimoStato; // Variabile per registrare l'ultimo stato del bottone - -void setup() { - pinMode(buttonPin, INPUT); // Set the switch pin as input - pinMode(led, OUTPUT); - ultimoStato = digitalRead(buttonPin); // Prima lettura del bottone -} - -void loop(){ - statoAttuale = digitalRead(buttonPin); // Legge lo stato del bottone e lo resistra in val - delay(20); // riduce l'effetto bounce - if (statoAttuale != ultimoStato && statoAttuale == HIGH) { // due condizione contemporanee - // lo stato del bottone e' camabiato AND lo stato attuale e' HIGH - digitalWrite(led, !(digitalRead(led))); - // Il processore setta lo stato di un led - // impostando il relativo PIN: possiamo leggere il relativo registro - // allo stesso modo di un bottone. - } - - ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale -} - - - -/* Domande: - - 1. La variabile ledstatus serve per tenere traccia dello stato del LED: - si potrebbe fare a meno di questa? - Cosa fa Arduino quando deve accendere o spegnere un LED? - Come funziona DigiralRead() ? - - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - Soluzione: - - 1. Per accendere o spegnere un LED Arduino imposta il valore del registro corrispondente - al PIN: se questo e' 0 il circuito e' aperto mentre se e' 1 il circuito e' chiuso. - Allo stesso modo con DigitalRead() e' possibile leggere lo stato di quel regustro - e conoscere se il LED e' acceso o spento. - */ diff --git a/oggi/button_state_4_state/button_state_4_state.ino b/oggi/button_state_4_state/button_state_4_state.ino deleted file mode 100644 index 30a330b..0000000 --- a/oggi/button_state_4_state/button_state_4_state.ino +++ /dev/null @@ -1,49 +0,0 @@ -/* - Stato di un bottone - - Legge lo stato di un input - - */ -int led = 13; // Definizione delle variabili -int buttonPin = 2; - // Dichiarazione di variabili -int statoAttuale; // Variabile per leggere lo stato del bottone -int ultimoStato; // Variabile per registrare l'ultimo stato del bottone -int ledStatus; // varabile per mantenere lo stato del led - -void setup() { - pinMode(buttonPin, INPUT); - pinMode(led, OUTPUT); - Serial.begin(9600); // Attiva la comunicazione seriale a 9600bps - ultimoStato = digitalRead(buttonPin); // Prima lettura del bottone - ledStatus = 0; // Il LED viene inpostato come spento -} - -void loop(){ - statoAttuale = digitalRead(buttonPin); // Legge lo stato del bottone e - // lo registra nella variabile - delay(20); // riduce l'effetto bounce - if (statoAttuale != ultimoStato) { // lo stato del bottone e' cambiato - if (statoAttuale == HIGH) { // il bottone e' stato premuto - Serial.println("Button premuto"); - - ledStatus = !ledStatus ; // Inverte lo stato del LED - // ledStatus = 1 - ledStatus ; // Forma analoga - - Serial.print("Stato del LED: "); // DEBUG - Serial.println(ledStatus) ; - } - } - - ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale - digitalWrite(led, ledStatus); // setta il led allo stato richiesto - -} - -/* Domande: - - 1. I due cicli if verificano che due condizioni siano concomitanti: e' possibile - integrarli semplificando il codice? - - */ - diff --git a/oggi/button_state_4_state_and_condition/button_state_4_state_and_condition.ino b/oggi/button_state_4_state_and_condition/button_state_4_state_and_condition.ino deleted file mode 100644 index 5ac0879..0000000 --- a/oggi/button_state_4_state_and_condition/button_state_4_state_and_condition.ino +++ /dev/null @@ -1,72 +0,0 @@ -/* - Stato di un bottone - - Legge lo stato di un input - - */ -int led = 13; -int buttonPin = 2; -int statoAttuale; // Variabile per leggere lo stato del bottone -int ultimoStato; // Variabile per registrare l'ultimo stato del bottone -int ledStatus; // varabile per mantenere lo stato del led - -void setup() { - pinMode(buttonPin, INPUT); // Set the switch pin as input - pinMode(led, OUTPUT); - Serial.begin(9600); // Attiva la comunicazione seriale a 9600bps - ultimoStato = digitalRead(buttonPin); // Prima lettura del bottone - ledStatus = 0; // Il LED viene inpostato come spento -} - -void loop(){ - statoAttuale = digitalRead(buttonPin); // Legge lo stato del bottone e lo resistra in val - delay(20); // riduce l'effetto bounce - if (statoAttuale != ultimoStato && statoAttuale == HIGH) { // due condizione contemporanee - // lo stato del bottone e' camabiato AND lo stato attuale e' HIGH - Serial.println("Button premuto"); - - ledStatus = !ledStatus ; // Inverte lo stato del LED - // ledStatus = 1 - ledStatus ; // Forma analoga - - Serial.print("Stato del LED: "); // DEBUG - Serial.println(ledStatus) ; - } - - ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale - digitalWrite(led, ledStatus); // setta il led allo stato richiesto - -} - - - -/* Domande: - - 1. La variabile ledstatus serve per tenere traccia dello stato del LED: - si potrebbe fare a meno di questa? - Cosa fa Arduino quando deve accendere o spegnere un LED? - Come funziona DigiralRead() ? - - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - Soluzione: - - 1. Per accendere o spegnere un LED Arduino imposta il valore del registro corrispondente - al PIN: se questo e' 0 il circuito e' aperto mentre se e' 1 il circuito e' chiuso. - Allo stesso modo con DigitalRead() e' possibile leggere lo stato di quel registro - e conoscere se il LED e' acceso o spento. - - https://www.arduino.cc/en/Reference/PortManipulation - - http://www.instructables.com/id/Microcontroller-Register-Manipulation/ - */