From 396bfe3bf6c89e468fd7a935d7be3a7d00cba04b Mon Sep 17 00:00:00 2001 From: eaman Date: Fri, 14 Oct 2016 16:10:56 +0200 Subject: [PATCH] State and loops, array --- .../buttons/button_state_3/button_state_3.ino | 3 +- .../button_state_4_final.ino | 8 +- .../button_state_4_state.ino | 3 +- .../button_state_4_state_and_condition.ino | 4 +- oggi/RGB_LED/README | 2 + oggi/RGB_LED/rgb_0/rgb_0.ino | 46 ++++++++ .../rgb_0_soluzione/rgb_0_soluzione.ino | 66 +++++++++++ .../rgb_1_all_color/rgb_1_all_color.ino | 54 +++++++++ oggi/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino | 69 +++++++++++ oggi/analogInput_1/analogInput_1.ino | 54 --------- .../analogInput_2_serial.ino | 58 --------- oggi/button_state_3/button_state_3.ino | 47 ++++++++ .../button_state_4_final.ino | 62 ++++++++++ .../button_state_4_state.ino | 49 ++++++++ .../button_state_4_state_and_condition.ino | 72 ++++++++++++ oggi/loops/loop_0_rider/loop_0_rider.ino | 110 ++++++++++++++++++ .../loop_1_array_loop/loop_1_array_loop.ino | 77 ++++++++++++ .../loop_2_array_loop_serial.ino | 63 ++++++++++ oggi/operator_3_logic/operator_3_logic.ino | 57 +++++++++ oggi/photo_3_serial/photo_3_serial.ino | 86 -------------- .../photo_4_calibrated/photo_4_calibrated.ino | 61 ---------- .../photo_5_calibration.ino | 83 ------------- oggi/photo_6_smooth/photo_6_smooth.ino | 68 ----------- .../photo_7_tonePitchFollower.ino | 77 ------------ .../pwm_4_analog_input/pwm_4_analog_input.ino | 29 ----- .../loop_2_array_loop_serial.ino | 5 - 26 files changed, 786 insertions(+), 527 deletions(-) create mode 100644 oggi/RGB_LED/README create mode 100644 oggi/RGB_LED/rgb_0/rgb_0.ino create mode 100644 oggi/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino create mode 100644 oggi/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino create mode 100644 oggi/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino delete mode 100644 oggi/analogInput_1/analogInput_1.ino delete mode 100644 oggi/analogInput_2_serial/analogInput_2_serial.ino create mode 100644 oggi/button_state_3/button_state_3.ino create mode 100644 oggi/button_state_4_final/button_state_4_final.ino create mode 100644 oggi/button_state_4_state/button_state_4_state.ino create mode 100644 oggi/button_state_4_state_and_condition/button_state_4_state_and_condition.ino create mode 100644 oggi/loops/loop_0_rider/loop_0_rider.ino create mode 100644 oggi/loops/loop_1_array_loop/loop_1_array_loop.ino create mode 100644 oggi/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino create mode 100644 oggi/operator_3_logic/operator_3_logic.ino delete mode 100644 oggi/photo_3_serial/photo_3_serial.ino delete mode 100644 oggi/photo_4_calibrated/photo_4_calibrated.ino delete mode 100644 oggi/photo_5_calibration/photo_5_calibration.ino delete mode 100644 oggi/photo_6_smooth/photo_6_smooth.ino delete mode 100644 oggi/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino delete mode 100644 oggi/pwm_4_analog_input/pwm_4_analog_input.ino diff --git a/basic/buttons/button_state_3/button_state_3.ino b/basic/buttons/button_state_3/button_state_3.ino index 2f43ec5..2afa6d9 100644 --- a/basic/buttons/button_state_3/button_state_3.ino +++ b/basic/buttons/button_state_3/button_state_3.ino @@ -20,7 +20,8 @@ void setup() { void loop(){ - statoAttuale = digitalRead(switchPin); // Legge lo stato del bottone e lo resistra in val + 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 diff --git a/basic/buttons/button_state_4_final/button_state_4_final.ino b/basic/buttons/button_state_4_final/button_state_4_final.ino index 26dcbf8..4c0593f 100644 --- a/basic/buttons/button_state_4_final/button_state_4_final.ino +++ b/basic/buttons/button_state_4_final/button_state_4_final.ino @@ -20,11 +20,13 @@ void loop(){ 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. + 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 + ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale } diff --git a/basic/buttons/button_state_4_state/button_state_4_state.ino b/basic/buttons/button_state_4_state/button_state_4_state.ino index d21f27e..30a330b 100644 --- a/basic/buttons/button_state_4_state/button_state_4_state.ino +++ b/basic/buttons/button_state_4_state/button_state_4_state.ino @@ -20,7 +20,8 @@ void setup() { } void loop(){ - statoAttuale = digitalRead(buttonPin); // Legge lo stato del bottone e lo registra in val + 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 diff --git a/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino b/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino index d9bdbba..5ac0879 100644 --- a/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino +++ b/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino @@ -65,6 +65,8 @@ void loop(){ 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 + 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/ */ diff --git a/oggi/RGB_LED/README b/oggi/RGB_LED/README new file mode 100644 index 0000000..133660a --- /dev/null +++ b/oggi/RGB_LED/README @@ -0,0 +1,2 @@ +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 new file mode 100644 index 0000000..d69d451 --- /dev/null +++ b/oggi/RGB_LED/rgb_0/rgb_0.ino @@ -0,0 +1,46 @@ +/* + 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 new file mode 100644 index 0000000..5d46d7e --- /dev/null +++ b/oggi/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino @@ -0,0 +1,66 @@ +/* + 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() +{ + setColor(255,0,0) ; // imposta il LED in rosso + //setColor(0xFF,0x00,0x00) ; // imposta il LED in rosso in esadecimale + + // setName("green") ; +} + +// Funzioni: +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 new file mode 100644 index 0000000..a53a02a --- /dev/null +++ b/oggi/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino @@ -0,0 +1,54 @@ +/* + 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 new file mode 100644 index 0000000..8e712fc --- /dev/null +++ b/oggi/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino @@ -0,0 +1,69 @@ +// 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/analogInput_1/analogInput_1.ino b/oggi/analogInput_1/analogInput_1.ino deleted file mode 100644 index 231a9ab..0000000 --- a/oggi/analogInput_1/analogInput_1.ino +++ /dev/null @@ -1,54 +0,0 @@ -/* - Analog Input - Demonstrates analog input by reading an analog sensor on analog pin 0 and - turning on and off a light emitting diode(LED) connected to digital pin 13. - The amount of time the LED will be on and off depends on - the value obtained by analogRead(). - - The circuit: - * Potentiometer attached to analog input 0 - * center pin of the potentiometer to the analog pin - * one side pin (either one) to ground - * the other side pin to +5V - * LED anode (long leg) attached to digital output 13 - * LED cathode (short leg) attached to ground - - * Note: because most Arduinos have a built-in LED attached - to pin 13 on the board, the LED is optional. - - - Created by David Cuartielles - modified 30 Aug 2011 - By Tom Igoe - - This example code is in the public domain. - - http://arduino.cc/en/Tutorial/AnalogInput - - Schema: http://lab.piffa.net/schemi/potenziometro_bb.png - - */ - -const int sensorPin = A0; // select the input pin for the potentiometer -const int ledPin = 13; // select the pin for the LED -int sensorValue = 0; // variable to store the value coming from the sensor - -void setup() { - // declare the ledPin as an OUTPUT: - pinMode(ledPin, OUTPUT); - // Non e' necessario dichiarare un pin come input: - // tutti i pin di default sono input -} - -void loop() { - // read the value from the sensor: - sensorValue = analogRead(sensorPin); - // turn the ledPin on - digitalWrite(ledPin, HIGH); - // stop the program for milliseconds: - delay(sensorValue); - // turn the ledPin off: - digitalWrite(ledPin, LOW); - // stop the program for for milliseconds: - delay(sensorValue); -} diff --git a/oggi/analogInput_2_serial/analogInput_2_serial.ino b/oggi/analogInput_2_serial/analogInput_2_serial.ino deleted file mode 100644 index 2cce5e2..0000000 --- a/oggi/analogInput_2_serial/analogInput_2_serial.ino +++ /dev/null @@ -1,58 +0,0 @@ -/* - Analog Input - Demonstrates analog input by reading an analog sensor on analog pin 0 and - turning on and off a light emitting diode(LED) connected to digital pin 13. - The amount of time the LED will be on and off depends on - the value obtained by analogRead(). - - The circuit: - * Potentiometer attached to analog input 0 - * center pin of the potentiometer to the analog pin - * one side pin (either one) to ground - * the other side pin to +5V - * LED anode (long leg) attached to digital output 13 - * LED cathode (short leg) attached to ground - - * Note: because most Arduinos have a built-in LED attached - to pin 13 on the board, the LED is optional. - - - Created by David Cuartielles - modified 30 Aug 2011 - By Tom Igoe - - This example code is in the public domain. - - http://arduino.cc/en/Tutorial/AnalogInput - - */ - -const byte sensorPin = A0; // select the input pin for the potentiometer -const int ledPin = 13; // select the pin for the LED -int sensorValue = 0; // variable to store the value coming from the sensor - -void setup() { - // declare the ledPin as an OUTPUT: - pinMode(ledPin, OUTPUT); - // initialize serial communications at 9600 bps: - Serial.begin(9600); -} - -void loop() { - // read the value from the sensor: - sensorValue = analogRead(sensorPin); - // turn the ledPin on - digitalWrite(ledPin, HIGH); - // stop the program for milliseconds: - delay(sensorValue); - // turn the ledPin off: - digitalWrite(ledPin, LOW); - // stop the program for for milliseconds: - - // print the results to the serial monitor: - Serial.print("sensor = " ); - Serial.print(sensorValue); - Serial.print("\t delay = "); // \t is a special character for tab - Serial.println(sensorValue); - delay(sensorValue); -} diff --git a/oggi/button_state_3/button_state_3.ino b/oggi/button_state_3/button_state_3.ino new file mode 100644 index 0000000..2afa6d9 --- /dev/null +++ b/oggi/button_state_3/button_state_3.ino @@ -0,0 +1,47 @@ +/* + 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 new file mode 100644 index 0000000..4c0593f --- /dev/null +++ b/oggi/button_state_4_final/button_state_4_final.ino @@ -0,0 +1,62 @@ +/* + 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 new file mode 100644 index 0000000..30a330b --- /dev/null +++ b/oggi/button_state_4_state/button_state_4_state.ino @@ -0,0 +1,49 @@ +/* + 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 new file mode 100644 index 0000000..5ac0879 --- /dev/null +++ b/oggi/button_state_4_state_and_condition/button_state_4_state_and_condition.ino @@ -0,0 +1,72 @@ +/* + 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/ + */ diff --git a/oggi/loops/loop_0_rider/loop_0_rider.ino b/oggi/loops/loop_0_rider/loop_0_rider.ino new file mode 100644 index 0000000..1549668 --- /dev/null +++ b/oggi/loops/loop_0_rider/loop_0_rider.ino @@ -0,0 +1,110 @@ +/* Knight Rider 1 + * -------------- + * + * Basically an extension of Blink_LED. + * + * + * (cleft) 2005 K3, Malmo University + * @author: David Cuartielles + * @hardware: David Cuartielles, Aaron Hallborg + See: https://www.arduino.cc/en/Tutorial/KnightRider + + Schema semplificato: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + */ + +int pin2 = 2; +int pin3 = 3; +int pin4 = 4; +int pin5 = 5; +int pin6 = 6; +int pin7 = 7; +int pin8 = 8; +int pin9 = 9; +int timer = 100; + +void setup(){ + pinMode(pin2, OUTPUT); + pinMode(pin3, OUTPUT); + pinMode(pin4, OUTPUT); + pinMode(pin5, OUTPUT); + pinMode(pin6, OUTPUT); + pinMode(pin7, OUTPUT); + pinMode(pin8, OUTPUT); + pinMode(pin9, OUTPUT); +} + +void loop() { + digitalWrite(pin2, HIGH); + delay(timer); + digitalWrite(pin2, LOW); + delay(timer); + + digitalWrite(pin3, HIGH); + delay(timer); + digitalWrite(pin3, LOW); + delay(timer); + + digitalWrite(pin4, HIGH); + delay(timer); + digitalWrite(pin4, LOW); + delay(timer); + + digitalWrite(pin5, HIGH); + delay(timer); + digitalWrite(pin5, LOW); + delay(timer); + + digitalWrite(pin6, HIGH); + delay(timer); + digitalWrite(pin6, LOW); + delay(timer); + + digitalWrite(pin7, HIGH); + delay(timer); + digitalWrite(pin7, LOW); + delay(timer); + + digitalWrite(pin8, HIGH); + delay(timer); + digitalWrite(pin8, LOW); + delay(timer); + + digitalWrite(pin9, HIGH); + delay(timer); + digitalWrite(pin9, LOW); + delay(timer); + + // Ding! Mezzo giro + + digitalWrite(pin8, HIGH); + delay(timer); + digitalWrite(pin8, LOW); + delay(timer); + + digitalWrite(pin7, HIGH); + delay(timer); + digitalWrite(pin7, LOW); + delay(timer); + + digitalWrite(pin6, HIGH); + delay(timer); + digitalWrite(pin6, LOW); + delay(timer); + + digitalWrite(pin5, HIGH); + delay(timer); + digitalWrite(pin5, LOW); + delay(timer); + + digitalWrite(pin4, HIGH); + delay(timer); + digitalWrite(pin4, LOW); + delay(timer); + + digitalWrite(pin3, HIGH); + delay(timer); + digitalWrite(pin3, LOW); + delay(timer); +} diff --git a/oggi/loops/loop_1_array_loop/loop_1_array_loop.ino b/oggi/loops/loop_1_array_loop/loop_1_array_loop.ino new file mode 100644 index 0000000..bb9662e --- /dev/null +++ b/oggi/loops/loop_1_array_loop/loop_1_array_loop.ino @@ -0,0 +1,77 @@ +/* Knight Rider 2 + * -------------- + * + * Array e uso dei cicli iterativi. + * + + + Schema semplificato: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + */ + +int pinArray[8] = {2, 3, 4, 5, 6, 7, 8, 9}; +int timer = 100; + +void setup(){ + // we make all the declarations at once + for (int count=0;count<9;count++) { + pinMode(pinArray[count], OUTPUT); + } +} + +void loop() { + for (int count=0;count<8;count++) { // 8 e' un numero magico + digitalWrite(pinArray[count], HIGH); + delay(timer); + digitalWrite(pinArray[count], LOW); + delay(timer); + } + +// Ciclo inverso: dall'alto in basso + for (int count=8;count>=0;count--) { + digitalWrite(pinArray[count], HIGH); + delay(timer); + digitalWrite(pinArray[count], LOW); + delay(timer); + } +} + +/* Domande: + + 1. Come posso fare per saltare un elemento del loop? + 2. Come posso fare per uscire completamente dal loop? + 3. 8 e' un numero magico: come posso evitarlo? + +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +Soluzioni: + 1. utilizzare continue + 2. utilizzare break + 3. Utilizzare un variabile sarebbe gia' un inizio, ancora meglio estrarre il + valore tramite la funzione sizeof(). +Links: +- http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm +- https://www.arduino.cc/en/Reference/Sizeof +*/ + + diff --git a/oggi/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino b/oggi/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino new file mode 100644 index 0000000..3b5ddef --- /dev/null +++ b/oggi/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino @@ -0,0 +1,63 @@ +/* + For Loop Iteration + + Demonstrates the use of a for() loop. + Lights multiple LEDs in sequence, then in reverse. + + The circuit: + * LEDs from pins 2 through 9 to ground + + Schemi: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + + http://www.arduino.cc/en/Tutorial/ForLoop + */ + +byte ledPins[8] = { // Domanda: cosa succede se uso int? + 2,3,4,5,6,7,8,9} +; //Array +int timer = 100; // Pausa per far brillare i LED + +void setup() { + Serial.begin(9600); + // use a for loop to initialize each pin as an output: + for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) { + pinMode(ledPins[thisPin], OUTPUT); + Serial.print("Inizializzato pin n. "); + Serial.println( thisPin); + } + + Serial.print("Dimesione array: "); + Serial.println(sizeof(ledPins)); +} + +void loop() { + // loop from the lowest pin to the highest: + for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) { + Serial.print("Accensione pin n. "); + Serial.println(thisPin); + // turn the pin on: + digitalWrite(ledPins[thisPin], HIGH); + delay(timer); + // turn the pin off: + digitalWrite(ledPins[thisPin], LOW); + // Debug + + } + + // loop from the highest pin to the lowest: + for (int thisPin = sizeof(ledPins) -1 ; thisPin > 0; thisPin--) { + Serial.print("Accensione pin n. "); // Gli array sono indicizzati da 0 + Serial.println(thisPin); + // >< milliseconds: - delay(sensorValue); - // turn the ledPin off: - digitalWrite(ledPin, LOW); - - // stop the program for for milliseconds: - // print the results to the serial monitor: - Serial.print("sensor = " ); - Serial.print(sensorValue); - Serial.print("\t delay = "); - Serial.println(sensorValue); - delay(sensorValue); -} - -/* domande: - 1. qual'e' il valore minimo rilevato? - 2. quale il massimo? - 3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore? - 4. A cosa serve la resistenza da 10 - 5k ohms ? - - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . -Risposte: - -3. Vedi esercizio suciessivo -4. Serve come pull down per il pin che altrimenti sarebbe un sensore -ad alta impendenza flottante e come divisore di voltaggio. - - */ - - diff --git a/oggi/photo_4_calibrated/photo_4_calibrated.ino b/oggi/photo_4_calibrated/photo_4_calibrated.ino deleted file mode 100644 index 913f136..0000000 --- a/oggi/photo_4_calibrated/photo_4_calibrated.ino +++ /dev/null @@ -1,61 +0,0 @@ -/* - Photoresistor - - Utilizzare una fotoresistenza come analog input. - Il comportamento della foto resistenza e' simile - a un potenziometro: varia la resistenza in base alla - quantita' di luce. - - Per ottenere valori significativi utilizzare unaresistenza - da ~5k - 10k ohms in serie con il sensore. - - Questo sketch modifica l'intervallo di intermittenza di un led - in base alla luminosita' rilevata. - - Schema: http://lab.piffa.net/schemi/photoresistor_led.png - */ - -int sensorPin = A0; // select the input pin for the potentiometer -int ledPin = 13; // select the pin for the LED -int sensorValue = 0; // variable to store the value coming from the sensor - -int min = 60; // valore minimo rilevato dal sensore -int max = 600; // valore massimo rilevato dal sensore - -void setup() { - // declare the ledPin as an OUTPUT: - pinMode(ledPin, OUTPUT); - // initialize serial communications at 9600 bps: - Serial.begin(9600); -} - -void loop() { - // read the value from the sensor: - sensorValue = analogRead(sensorPin); - int calValue = map(sensorValue,min,max,0,1000) ; - // Max pausa = 1000 ms - - // Manage those blinks - digitalWrite(ledPin, HIGH); - // stop the program for milliseconds: - delay(calValue); - // turn the ledPin off: - digitalWrite(ledPin, LOW); - // stop the program for for milliseconds: - - // print the results to the serial monitor: - Serial.print("sensor = " ); - Serial.print(sensorValue); - Serial.print("\t cal delay = "); - Serial.println(calValue); - delay(sensorValue); -} - -/* -Domande: -1. Modificare lo sketch in modo che modifichi la luminosita' di un led -via PWM tramite il valore letto dal sensore. -2. Come fare per costringere la variabile dentro un intervallo stabilito? -3. Come si potrebbe eseguire la calibrazione automaticamente? -*/ - diff --git a/oggi/photo_5_calibration/photo_5_calibration.ino b/oggi/photo_5_calibration/photo_5_calibration.ino deleted file mode 100644 index 1d9b4d7..0000000 --- a/oggi/photo_5_calibration/photo_5_calibration.ino +++ /dev/null @@ -1,83 +0,0 @@ -/* - Calibration - - Demonstrates one technique for calibrating sensor input. The - sensor readings during the first five seconds of the sketch - execution define the minimum and maximum of expected values - attached to the sensor pin. - - The sensor minimum and maximum initial values may seem backwards. - Initially, you set the minimum high and listen for anything - lower, saving it as the new minimum. Likewise, you set the - maximum low and listen for anything higher as the new maximum. - - The circuit: - * Analog sensor (potentiometer will do) attached to analog input 0 - * LED attached from digital pin 9 to ground - - created 29 Oct 2008 - By David A Mellis - modified 30 Aug 2011 - By Tom Igoe - - http://arduino.cc/en/Tutorial/Calibration - - This example code is in the public domain. - - */ - -// These constants won't change: -const int sensorPin = A0; // pin that the sensor is attached to -const int ledPin = 9; // pin that the LED is attached to - -// variables: -int sensorValue = 0; // the sensor value -int sensorMin = 1023; // minimum sensor value -int sensorMax = 0; // maximum sensor value - - -void setup() { - // turn on LED to signal the start of the calibration period: - pinMode(13, OUTPUT); - digitalWrite(13, HIGH); - - // calibrate during the first five seconds - while (millis() < 5000) { - sensorValue = analogRead(sensorPin); - - // record the maximum sensor value - if (sensorValue > sensorMax) { - sensorMax = sensorValue; - } - - // record the minimum sensor value - if (sensorValue < sensorMin) { - sensorMin = sensorValue; - } - delay(5); // Let the sensor rest a bit and stabilize - } - - // signal the end of the calibration period - digitalWrite(13, LOW); -} - -void loop() { - // read the sensor: - sensorValue = analogRead(sensorPin); - - // apply the calibration to the sensor reading - sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255); - - // in case the sensor value is outside the range seen during calibration - sensorValue = constrain(sensorValue, 0, 255); - - // fade the LED using the calibrated value: - analogWrite(ledPin, sensorValue); -} -/* -Domande: -1. Modificare lo sketch in modo che modifichi la luminosita' di un led -via PWM tramite il valore letto dal sensore. -2. Come fare per costringere la variabile dentro un intervallo stabilito? -*/ - diff --git a/oggi/photo_6_smooth/photo_6_smooth.ino b/oggi/photo_6_smooth/photo_6_smooth.ino deleted file mode 100644 index 9d1aa9c..0000000 --- a/oggi/photo_6_smooth/photo_6_smooth.ino +++ /dev/null @@ -1,68 +0,0 @@ -/* - Smoothing - - Legge 10 valori dal sensore e ritorna il valore medio tra questi. - - - */ - -// These constants won't change: -const int sensorPin = A0; // pin that the sensor is attached to -const int ledPin = 9; // pin that the LED is attached to - -// variables: -int sensorValue = 0; // the sensor value -int sensorMin = 1023; // minimum sensor value -int sensorMax = 0; // maximum sensor value - - -void setup() { - // turn on LED to signal the start of the calibration period: - pinMode(13, OUTPUT); - digitalWrite(13, HIGH); - - // calibrate during the first five seconds - while (millis() < 5000) { - sensorValue = analogRead(sensorPin); - - // record the maximum sensor value - if (sensorValue > sensorMax) { - sensorMax = sensorValue; - } - - // record the minimum sensor value - if (sensorValue < sensorMin) { - sensorMin = sensorValue; - } - } - - // signal the end of the calibration period - digitalWrite(13, LOW); -} - -void loop() { - // read the sensor: - sensorValue = smoothRead(sensorPin); - - // apply the calibration to the sensor reading - sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255); - - // in case the sensor value is outside the range seen during calibration - sensorValue = constrain(sensorValue, 0, 255); - - // fade the LED using the calibrated value: - analogWrite(ledPin, sensorValue); -} -// Funzioni - -int smoothRead(int sensorPin) { -// Legge 10 valori dal sensore e ritorna il valore medio tra questi. - int total = 0; - for (int i = 0; i < 10; i++) { - total = total + analogRead(sensorPin); - delay(2); // Pausa per assestare il senstore - } - return(total / 10); -} - - diff --git a/oggi/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino b/oggi/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino deleted file mode 100644 index dcfc5b7..0000000 --- a/oggi/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino +++ /dev/null @@ -1,77 +0,0 @@ -/* - Pitch follower - - Plays a pitch that changes based on a changing analog input - - circuit: - * 8-ohm speaker on digital pin 8 - * photoresistor on analog 0 to 5V - * 4.7K resistor on analog 0 to ground - - created 21 Jan 2010 - modified 31 May 2012 - by Tom Igoe, with suggestion from Michael Flynn - -This example code is in the public domain. - - http://arduino.cc/en/Tutorial/Tone2 - - */ - -// These constants won't change: -const int sensorPin = A0; // pin that the sensor is attached to -const int ledPin = 9; // pin that the LED is attached to - -// variables: -int sensorValue = 0; // the sensor value -int sensorMin = 1023; // minimum sensor value -int sensorMax = 0; // maximum sensor value - -void setup() { - // initialize serial communications (for debugging only): - Serial.begin(9600); - pinMode(13, OUTPUT); - digitalWrite(13, HIGH); - - // calibrate during the first five seconds - while (millis() < 5000) { - sensorValue = analogRead(sensorPin); - - // record the maximum sensor value - if (sensorValue > sensorMax) { - sensorMax = sensorValue; - } - - // record the minimum sensor value - if (sensorValue < sensorMin) { - sensorMin = sensorValue; - } - } - - // signal the end of the calibration period - digitalWrite(13, LOW); -} - -void loop() { - // read the sensor: - int sensorReading = analogRead(sensorPin); - // print the sensor reading so you know its range - Serial.println(sensorReading); - // map the analog input range (in this case, 400 - 1000 from the photoresistor) - // to the output pitch range (120 - 1500Hz) - // change the minimum and maximum input numbers below - // depending on the range your sensor's giving: - int thisPitch = map(sensorReading, sensorMin, sensorMax, 220, 3500); - - // play the pitch: - if (sensorReading < sensorMax -50) { - tone(ledPin, thisPitch, 10); - } - delay(1); // delay in between reads for stability -} - - - - - - diff --git a/oggi/pwm_4_analog_input/pwm_4_analog_input.ino b/oggi/pwm_4_analog_input/pwm_4_analog_input.ino deleted file mode 100644 index e31396e..0000000 --- a/oggi/pwm_4_analog_input/pwm_4_analog_input.ino +++ /dev/null @@ -1,29 +0,0 @@ -/* - Analog PWM - - Impostare la frequenza del PWM tramite un input analogico. - - */ - -int inputPin = A0; // set input pin for the potentiometer -int inputValue = 0; // potentiometer input variable -int ledPin = 3; // output pin, deve avere il PWM - -void setup() { - // declare the ledPin as an OUTPUT: - pinMode(ledPin, OUTPUT); -} - -void loop() { - // read the value from the potentiometer: - inputValue = analogRead(inputPin); - - // send the square wave signal to the LED: - analogWrite(ledPin, inputValue/4); - // la lettura analogica e' a 10 bit (0-1024) - // Il PWM invece e' a 8 bit (0-255) - // Circa 1024 / 4 ~= 255 - - // Domanda: dovrebbe esserci un delay()? -} - diff --git a/programming/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino b/programming/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino index 3b5ddef..7591641 100644 --- a/programming/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino +++ b/programming/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino @@ -50,14 +50,9 @@ void loop() { for (int thisPin = sizeof(ledPins) -1 ; thisPin > 0; thisPin--) { Serial.print("Accensione pin n. "); // Gli array sono indicizzati da 0 Serial.println(thisPin); - // ><