From: eaman Date: Wed, 7 Dec 2016 23:35:45 +0000 (+0100) Subject: Calibrazioni X-Git-Url: http://git.piffa.net/web?a=commitdiff_plain;h=6dc7b8f2ea8b8f73f4f98571028291d7469ef3ab;p=sketchbook_andrea Calibrazioni --- diff --git a/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino b/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino index 3701f07..313598d 100644 --- a/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino +++ b/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino @@ -32,21 +32,21 @@ void setup() { void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); - int calValue = map(sensorValue,min,max,0,1024) ; - // Max pausa = 1024 + int calValue = map(sensorValue,min,max,0,1000) ; + // Max pausa = 1sec // turn the ledPin on 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: + // Debug, per disabilitarlo togliere l'inizializzazione della seriale + // dal setup() Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t cal delay = "); Serial.println(calValue); - delay(sensorValue); + delay(calValue); } /* diff --git a/basic/analog_input/photo_6_smooth/photo_6_smooth.ino b/basic/analog_input/photo_6_smooth/photo_6_smooth.ino index 9d1aa9c..1aaa668 100644 --- a/basic/analog_input/photo_6_smooth/photo_6_smooth.ino +++ b/basic/analog_input/photo_6_smooth/photo_6_smooth.ino @@ -62,7 +62,7 @@ int smoothRead(int sensorPin) { total = total + analogRead(sensorPin); delay(2); // Pausa per assestare il senstore } - return(total / 10); + return(total / 10); // Valore medio } diff --git a/basic/analog_input/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino b/basic/analog_input/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino index dcfc5b7..5b08cae 100644 --- a/basic/analog_input/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino +++ b/basic/analog_input/photo_7_tonePitchFollower/photo_7_tonePitchFollower.ino @@ -12,6 +12,8 @@ modified 31 May 2012 by Tom Igoe, with suggestion from Michael Flynn + Modified by A.Manni + This example code is in the public domain. http://arduino.cc/en/Tutorial/Tone2 @@ -20,17 +22,19 @@ 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 +const int piezoPin = 9; // pin that the piezo is attached to // variables: int sensorValue = 0; // the sensor value -int sensorMin = 1023; // minimum sensor value -int sensorMax = 0; // maximum sensor value +int sensorMin = 1023; // minimum sensor value +int sensorMax = 0; // maximum sensor value +int pitch = 0; // pithc value for the piezo void setup() { // initialize serial communications (for debugging only): Serial.begin(9600); pinMode(13, OUTPUT); + pinMode(piezoPin, OUTPUT); digitalWrite(13, HIGH); // calibrate during the first five seconds @@ -54,24 +58,28 @@ void setup() { void loop() { // read the sensor: - int sensorReading = analogRead(sensorPin); + int sensorValue = 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) + Serial.println(sensorValue); + // map the analog input range // 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); + pitch = map(sensorValue, sensorMin, sensorMax, 220, 3500); // play the pitch: - if (sensorReading < sensorMax -50) { - tone(ledPin, thisPitch, 10); + if (sensorValue < sensorMax -50) { // Offset to prevent the piezo to ring + // all the time, check sensor polarity + tone(piezoPin, pitch, 10); } delay(1); // delay in between reads for stability } +/* Esercizi: + 1. Implementare constrain e smoothing in questo sketch + */ diff --git a/libraries/common/common.cpp b/libraries/common/common.cpp index 2de42e9..b2564e7 100644 --- a/libraries/common/common.cpp +++ b/libraries/common/common.cpp @@ -155,6 +155,7 @@ void Pwm::Up(long speed) { if ((millis() - previousMillis) > speed / 256) { brightness++; // Incrementiamo la luminosita' previousMillis = millis(); + Serial.println(brightness); }; } @@ -169,6 +170,7 @@ void Pwm::Down(long speed ) { if ((millis() - previousMillis) > speed / 256) { brightness--; // Incrementiamo la luminosita' previousMillis = millis(); + Serial.println(brightness); }; } @@ -177,11 +179,12 @@ void Pwm::UD(long speed ) { if ((millis() - previousMillis) > speed / 512) { brightness = brightness + increment; // Incrementiamo la luminosita' previousMillis = millis(); + Serial.println(brightness); + analogWrite(ledPin, brightness); if (brightness == 0 || brightness == 255) { // Reverse direction increment = -increment ; }; }; - analogWrite(ledPin, brightness); } diff --git a/libraries/common/common.h b/libraries/common/common.h index c1cfc39..9e721b8 100644 --- a/libraries/common/common.h +++ b/libraries/common/common.h @@ -64,7 +64,6 @@ class Pwm { // Variabili int ledPin ; // il numero del LED pin int speed ; // velocita' del ciclo in ms - byte brightness ; // luminostia' iniziale long previousMillis ; //precedente cambio di stato byte increment ; // aumenta brighteness nel loop UD @@ -74,6 +73,7 @@ public: void Up(long speed); void Down(long speed); void UD(long speed); + byte brightness ; // luminostia' iniziale }; ////////////////////// 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 fa03276..0000000 --- a/oggi/analogInput_2_serial/analogInput_2_serial.ino +++ /dev/null @@ -1,57 +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 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); - // 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); -} 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/pwm_0_stati/pwm_0_stati.ino b/oggi/pwm_0_stati/pwm_0_stati.ino deleted file mode 100644 index 3863c4a..0000000 --- a/oggi/pwm_0_stati/pwm_0_stati.ino +++ /dev/null @@ -1,37 +0,0 @@ -/* - PWM Stati - - PWM per un LED: impostare i valori di luminosita' di un LED. - 4 stati di luminosita' per un LED collegato a un PIN PWM - - */ - -const int led = 9 ; // Il pin ~9 e' abilitato al PWM -byte brightness = 255; // Valore iniziale per il PWM del LED -const int pausa = 2000; // Pausa tra uno stato e l'altro - -void setup() { - pinMode(led, OUTPUT); // Il PIN nove va dichiarato come un OUTPUT -} - -void loop() { - // OUTPUT al 100% : 255 - analogWrite(led, brightness) ; - delay(pausa); - - // OUTPUT al 75% : 191 - analogWrite(led, brightness * 0.75) ; - delay(pausa); - - // OUTPUT al 50% : 127 - analogWrite(led, brightness * 0.5) ; - delay(pausa); - - // OUTPUT al 25%: 63 - analogWrite(led, brightness * 0.25 ) ; - delay(pausa); - - // OUTPUT al 0% - analogWrite(led, brightness * 0) ; - delay(pausa); -} diff --git a/oggi/pwm_1_soluzione_doppio_while_byte/pwm_1_soluzione_doppio_while_byte.ino b/oggi/pwm_1_soluzione_doppio_while_byte/pwm_1_soluzione_doppio_while_byte.ino deleted file mode 100644 index 19f8d07..0000000 --- a/oggi/pwm_1_soluzione_doppio_while_byte/pwm_1_soluzione_doppio_while_byte.ino +++ /dev/null @@ -1,42 +0,0 @@ -/* - Fade sali e scendi - - PWM per un LED: aumentare progressivamente la luminosita'. - Aumenta e diminuisce la luminostia' usando un ciclo while - - Schema: http://lab.piffa.net/schemi/led_single_bb.png - Usare un PIN abilitato per il PWM. - */ - -byte led = 9 ; // Il pin ~9 e' abilitato al PWM -byte brightness = 0; // Valore iniziale per il PWM del LED - -// the setup routine runs once when you press reset: -void setup() { - pinMode(led, OUTPUT); // Il PIN nove va dichiarato come un OUTPUT -} - -void loop() { - while (brightness < 255) { - analogWrite(led, brightness);// La funziona analogWrite utilizza il PWM - // a 8 bit integrato nel MCU: simula un serie di valori intermedi - // nell'intervallo discreto con minimo 0 (spento) e massimo 255 (acceso). - delay(10); - brightness = brightness + 1; // Incrementiamo la luminosita' - } - - while (brightness > 0) { - analogWrite(led, brightness);// La funziona analogWrite utilizza il PWM - delay(10); - brightness = brightness - 1; // Decrementiamo la luminosita' - } - - -} -/* Note: - -- basic/pwm/pwm_3_fade_reverser/pwm_3_fade_reverser.ino - - E molto piu' snello utilizzando il solo ciclo loop come iteratore - e una condizione per cambiare l'incremento. -*/ diff --git a/oggi/pwm_1_while_byte/pwm_1_while_byte.ino b/oggi/pwm_1_while_byte/pwm_1_while_byte.ino deleted file mode 100644 index dc09234..0000000 --- a/oggi/pwm_1_while_byte/pwm_1_while_byte.ino +++ /dev/null @@ -1,33 +0,0 @@ -/* - Fade - - PWM per un LED: aumentare progressivamente la luminosita' - utilizzando un ciclo while e l'overflow di un byte. - - Schema: http://lab.piffa.net/schemi/led_single_bb.png - (Cambiare PIN) - */ - -const byte led = 9 ; // Il pin ~9 e' abilitato al PWM -byte brightness = 0; // Valore iniziale per il PWM del LED - // Perche' brightness non e' una costante? - -// the setup routine runs once when you press reset: -void setup() { - pinMode(led, OUTPUT); // Il PIN nove va dichiarato come un OUTPUT -} - -void loop() { - analogWrite(led, brightness); // La funziona analogWrite utilizza il PWM - // a 8 bit integrato nel MCU: simula un serie di valori intermedi - // nell'intervallo discreto con minimo 0 (spento) e massimo 255 (acceso). - delay(10); - brightness = brightness + 1; // Incrementiamo la luminosita' -} - -/* Domande: - -1. Come fare a invertire la dissolvenza diminuendo la luminosita'? -2. Provare a far salire e poi scendere la luminosita' - -*/ diff --git a/oggi/pwm_2_for_loop/pwm_2_for_loop.ino b/oggi/pwm_2_for_loop/pwm_2_for_loop.ino deleted file mode 100644 index 610d302..0000000 --- a/oggi/pwm_2_for_loop/pwm_2_for_loop.ino +++ /dev/null @@ -1,66 +0,0 @@ -/* - LED for PWM - - PWM per un LED: aumentare progressivamente la luminosita'. - Utilizzo di un ciclo iterativo: for loop - - */ - -int led = 9; // Pin per il PWM -int pausa = 5; - -void setup() -{ - pinMode(led, OUTPUT); -} - -void loop() -{ - for (int i = 0; i < 255 ; i++) { // Operatore ternario, 3 argomenti: - /* 1. definizione iteratore - 2. limite iteratore - 3. incremento operatore - */ - analogWrite(led, i) ; - delay(pausa); - } - // Ora l'inverso - for (int c = 255; c > 0 ; c--) { // Domanda: 1. avrei potuto usare come - // variabile di nuovo i ? - analogWrite(led, c) ; - delay(pausa); - } -} - -/* Domande: - 2. I due loop sembrano molto simili: e' possibile accorparli? - - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - . - - Risposte: - 1. Si, le variabili i e c esistono solo nello scopo degli iteratori - in cui sono dichiarate. - 2. Vedi es. suciessivo. - */ - - - - 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 5e9f83d..0000000 --- a/oggi/pwm_4_analog_input/pwm_4_analog_input.ino +++ /dev/null @@ -1,31 +0,0 @@ -/* - Analog PWM - - Impostare la frequenza del PWM tramite un input analogico. - -- Schema: http://lab.piffa.net/schemi/arduino-pwm-diagram.png - - */ - -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()? -} -