From: eaman Date: Wed, 12 Oct 2016 11:02:42 +0000 (+0200) Subject: Analog X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=commitdiff_plain;h=8358fa5a102089baa511a66a7b56fd863ba652b0 Analog --- diff --git a/basic/blinks/blink_7.1_diode/blink_7.1_diode.ino b/basic/blinks/blink_7.1_diode/blink_7.1_diode.ino deleted file mode 100644 index 963233d..0000000 --- a/basic/blinks/blink_7.1_diode/blink_7.1_diode.ino +++ /dev/null @@ -1,107 +0,0 @@ -/* - Blink v7: diodi - - Accensione e spegnimanto di 2 LED invertendo il verso di percorrenza - della corrente elettrica con un solo PIN di OUTPUT. - - -Schema: http://www.pighixxx.com/test/portfolio-items/light-two-leds/?portfolioID=610 - - Ricordarsi di usare una resistenza da ~320ohms per i LED. - Resistenza = (Voltaggio_Arduino - Forward_voltage_LED) / (ampere utilizzati) - R = (5v-1.8v) / 0.010a - R =320ohms - - This example code is in the public domain. - */ - - -int led = 2; // Pin per i LED -int pause = 200; // Variabile richiambile nel corso dell'esecuzione - - -void setup() { - pinMode(led, OUTPUT); // Abilitaiamo entrambi i LED, questo comporta - // collegarli dalla resistenza interna! -} - - -void loop() { - digitalWrite(led, HIGH); // turn the 1st LED on (HIGH is the voltage level) - delay(pause); // wait for a second - - digitalWrite(led, LOW); // turn the 2nd LED on by making the voltage LOW - delay(pause); // wait for a second -} - - -/* Domande - * - 1. Quanti stati sono disponibili per i LED ? - 2. Sarebbe possibile spegnere conemporaneamente entrambi i LED? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Risposte: - 1. Be' un digital out puo' essere a 0 oppure 5v. - - - - - - - - - - - - - - - - - - 2. Si: trasformando il PIN da OUTPUT a INPUT questo diventerebbe - ad alta impendenza impedendo anche il DRAIN di corrente. - Da un punto di vista fisico si potrebbe lavorare sul tempo di attivazione - del LED: facendo oscillare il pin a una frequenza superiore al periodo - necessario di attivazione del LED si potrebbe impedire l'accensione - anche come UOTPUT. - -*/ diff --git a/basic/blinks/blink_7_1_diode/blink_7_1_diode.ino b/basic/blinks/blink_7_1_diode/blink_7_1_diode.ino new file mode 100644 index 0000000..963233d --- /dev/null +++ b/basic/blinks/blink_7_1_diode/blink_7_1_diode.ino @@ -0,0 +1,107 @@ +/* + Blink v7: diodi + + Accensione e spegnimanto di 2 LED invertendo il verso di percorrenza + della corrente elettrica con un solo PIN di OUTPUT. + + +Schema: http://www.pighixxx.com/test/portfolio-items/light-two-leds/?portfolioID=610 + + Ricordarsi di usare una resistenza da ~320ohms per i LED. + Resistenza = (Voltaggio_Arduino - Forward_voltage_LED) / (ampere utilizzati) + R = (5v-1.8v) / 0.010a + R =320ohms + + This example code is in the public domain. + */ + + +int led = 2; // Pin per i LED +int pause = 200; // Variabile richiambile nel corso dell'esecuzione + + +void setup() { + pinMode(led, OUTPUT); // Abilitaiamo entrambi i LED, questo comporta + // collegarli dalla resistenza interna! +} + + +void loop() { + digitalWrite(led, HIGH); // turn the 1st LED on (HIGH is the voltage level) + delay(pause); // wait for a second + + digitalWrite(led, LOW); // turn the 2nd LED on by making the voltage LOW + delay(pause); // wait for a second +} + + +/* Domande + * + 1. Quanti stati sono disponibili per i LED ? + 2. Sarebbe possibile spegnere conemporaneamente entrambi i LED? + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Risposte: + 1. Be' un digital out puo' essere a 0 oppure 5v. + + + + + + + + + + + + + + + + + + 2. Si: trasformando il PIN da OUTPUT a INPUT questo diventerebbe + ad alta impendenza impedendo anche il DRAIN di corrente. + Da un punto di vista fisico si potrebbe lavorare sul tempo di attivazione + del LED: facendo oscillare il pin a una frequenza superiore al periodo + necessario di attivazione del LED si potrebbe impedire l'accensione + anche come UOTPUT. + +*/ diff --git a/oggi/analogInput_1/analogInput_1.ino b/oggi/analogInput_1/analogInput_1.ino new file mode 100644 index 0000000..231a9ab --- /dev/null +++ b/oggi/analogInput_1/analogInput_1.ino @@ -0,0 +1,54 @@ +/* + 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 new file mode 100644 index 0000000..2cce5e2 --- /dev/null +++ b/oggi/analogInput_2_serial/analogInput_2_serial.ino @@ -0,0 +1,58 @@ +/* + 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_1/button_1.ino b/oggi/button_1/button_1.ino deleted file mode 100644 index 6ea60b2..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 e for? - 4. Domanda: cosa succede se il jumper input non e' collegato ne al +5 ne al ground? - */ diff --git a/oggi/button_2_serial_debug/button_2_serial_debug.ino b/oggi/button_2_serial_debug/button_2_serial_debug.ino deleted file mode 100644 index 9a5057c..0000000 --- a/oggi/button_2_serial_debug/button_2_serial_debug.ino +++ /dev/null @@ -1,50 +0,0 @@ -/* - Input serial debug - - - Accensione e spegnimanto di un LED utilizzando un pin come input. - Utilizzare un bottone momentaneo per attivare il LED. - - Schemi del circuito per bottone in pull down: - - http://lab.piffa.net/schemi/button_1_bb.png - - http://lab.piffa.net/schemi/button_1_schem.png - -Tutorial: -- - */ - -int led = 12; -int input = 2; - -// the setup routine runs once when you press reset: -void setup() { - pinMode(led, OUTPUT); // Il PIN e' attivato come output - pinMode(input, INPUT); // Il PIN e' attivato come output - - Serial.begin(9600); // Attivazione seriale -} - -// 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); - Serial.println("Bottone premuto: circuito chiuso"); // Debug seriale - delay(200); - } - else { // Alterativa: se non e' +5v - digitalWrite(led, LOW); - Serial.println("Bottone libero: circuito aperto"); // Debug seriale - delay(200); - } -} - -/* Domande: - 1. invertire il programma facendo in modo che il led si spenga - quando il bottone e' premuto. Consoderare 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 e for? - 4. Domanda: cosa succede se il jumper input non e' collegato ne al +5 ne al gound? - */ diff --git a/oggi/operator_1_basic/operator_1_basic.ino b/oggi/operator_1_basic/operator_1_basic.ino deleted file mode 100644 index 1a8bd6b..0000000 --- a/oggi/operator_1_basic/operator_1_basic.ino +++ /dev/null @@ -1,63 +0,0 @@ -/* - Operatori base - - */ - -int a = 5; -int b = 10; -int c = 20; - -void setup() // run once, when the sketch starts -{ - Serial.begin(9600); // set up Serial library at 9600 bps - - Serial.println("Here is some math: "); - - Serial.print("a = "); - Serial.println(a); - Serial.print("b = "); - Serial.println(b); - Serial.print("c = "); - Serial.println(c); - - Serial.print("a + b = "); // add - Serial.println(a + b); - - Serial.print("a * c = "); // multiply - Serial.println(a * c); - - Serial.print("c / b = "); // divide - Serial.println(c / b); - - Serial.print("b - c = "); // subtract - Serial.println(b - c); - - Serial.print("b modulo a = "); // Calculates the remainder when one integer is divided by another. - Serial.println(b % a); - - Serial.print("8 modulo 3 = "); // Calculates the remainder when one integer is divided by another. - Serial.println(8 % 3); - - Serial.print("a incrementato ++ = "); // Increments - Serial.println(a++); // Increments AFTER the call - Serial.print("valore attuale di a: "); - Serial.println(a); - Serial.print("++a incrementato = "); - Serial.println(++a); // Increments BEFORE the call - - - Serial.print("b decrementato -- = "); // Decrement - Serial.println(b--); - Serial.print("valore attuale di b: "); - Serial.println(b); - Serial.print("--b decrementato = "); - Serial.println(--b); - -} - -void loop() // we need this to be here even though its empty -{ -} - - - diff --git a/oggi/operator_2_comparison/operator_2_comparison.ino b/oggi/operator_2_comparison/operator_2_comparison.ino deleted file mode 100644 index 7900d9e..0000000 --- a/oggi/operator_2_comparison/operator_2_comparison.ino +++ /dev/null @@ -1,47 +0,0 @@ -/* - Operatori comparativi binari - Comparison operators, binary - - */ - -int a = 5; -int b = 10; -int c = 20; - -void setup() // run once, when the sketch starts -{ - Serial.begin(9600); // set up Serial library at 9600 bps - - Serial.println("Here is some math: "); - - Serial.print("a = "); - Serial.println(a); - Serial.print("b = "); - Serial.println(b); - Serial.print("c = "); - Serial.println(c); - - Serial.print("a > b = "); // maggiore - Serial.println(a > b); - - Serial.print("a < b = "); // minore - Serial.println(a < b); - - Serial.print("a == b -> "); // stesso valore - Serial.println(a == b); - - Serial.print("a != b -> "); // valore diverso - Serial.println(a != b); - - Serial.print("a <= b ->"); // minore uguale - Serial.println(a <= b); - - Serial.print("a >= b -> "); // maggiore uguale - Serial.println(a >= b); -} - -void loop() // we need this to be here even though its empty -{ -} - - diff --git a/oggi/photo_3_serial/photo_3_serial.ino b/oggi/photo_3_serial/photo_3_serial.ino new file mode 100644 index 0000000..b2319e1 --- /dev/null +++ b/oggi/photo_3_serial/photo_3_serial.ino @@ -0,0 +1,86 @@ +/* + 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 ohms 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 + +Links: + +- http://www.pighixxx.com/test/portfolio-items/connect-a-photoresistor/ + +Divisori di voltaggio: +- https://en.wikipedia.org/wiki/Electrical_resistivity_and_conductivity +- https://learn.sparkfun.com/tutorials/voltage-dividers +- http://www.pighixxx.com/test/wp-content/uploads/2014/10/126.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 + +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 = "); + 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 new file mode 100644 index 0000000..913f136 --- /dev/null +++ b/oggi/photo_4_calibrated/photo_4_calibrated.ino @@ -0,0 +1,61 @@ +/* + 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 new file mode 100644 index 0000000..1d9b4d7 --- /dev/null +++ b/oggi/photo_5_calibration/photo_5_calibration.ino @@ -0,0 +1,83 @@ +/* + 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 new file mode 100644 index 0000000..9d1aa9c --- /dev/null +++ b/oggi/photo_6_smooth/photo_6_smooth.ino @@ -0,0 +1,68 @@ +/* + 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 new file mode 100644 index 0000000..dcfc5b7 --- /dev/null +++ b/oggi/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino @@ -0,0 +1,77 @@ +/* + 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 new file mode 100644 index 0000000..e31396e --- /dev/null +++ b/oggi/pwm_4_analog_input/pwm_4_analog_input.ino @@ -0,0 +1,29 @@ +/* + 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/oggi/serial_hello_world/serial_hello_world.ino b/oggi/serial_hello_world/serial_hello_world.ino deleted file mode 100644 index 7f8e814..0000000 --- a/oggi/serial_hello_world/serial_hello_world.ino +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Hello World! - * - * This is the Hello World! for Arduino. - * It shows how to send data to the computer trough the serial connection - */ - -void setup() -{ - Serial.begin(9600); // Inposta la seriale a 9600 bps - // Se Arduino manda 9600 bit per secondo e devi manandare 12 Bytes di dati - // quanto tempo serve per mandare i dati? - - // Try to change bond rate in the monitor - - Serial.println("Hello World!"); // scrive hello world e ritorna a capo - - -// Serial.print("Il mio nome e': "); // Scrive senza ritorno a capo -// Serial.println("Andrea"); // Scrive con ritorno a capo -// Serial.flush(); // Assicura che tutto il testo venga scritto - -} - -void loop() -{ - // Provate a mettere i Serial.printl() qui, magari con un delay() -} - - - - diff --git a/oggi/switch_test_serial/switch_test_serial.ino b/oggi/switch_test_serial/switch_test_serial.ino deleted file mode 100644 index 9fa2b7d..0000000 --- a/oggi/switch_test_serial/switch_test_serial.ino +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Switch test program - */ - -int switchPin = 2; // Switch connected to digital pin 2 - -void setup() // run once, when the sketch starts -{ - Serial.begin(9600); // set up Serial library at 9600 bps - pinMode(switchPin, INPUT); // sets the digital pin as input to read switch -} - - -void loop() // run over and over again -{ - Serial.print("Read switch input: "); - Serial.println(digitalRead(switchPin)); // Read the pin and display the value - delay(200); -}