From ca9bc9014acdfae3ee07d19650c547f02b796994 Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Mon, 13 Apr 2015 18:31:59 +0200 Subject: [PATCH] light and sound --- .../photo_3_serial/photo_3_serial.ino | 17 ++-- .../photo_4_calibrated/photo_4_calibrated.ino | 20 +++-- .../photo_5_calibration.ino | 80 +++++++++++++++++++ .../photo_6_smooth/photo_6_smooth.ino | 68 ++++++++++++++++ .../keyboard_three_pullup_buttons.ino | 2 +- piezo/piezo_mario_tune/piezo_mario_tune.ino | 2 +- 6 files changed, 172 insertions(+), 17 deletions(-) create mode 100644 basic/analog_input/photo_5_calibration/photo_5_calibration.ino create mode 100644 basic/analog_input/photo_6_smooth/photo_6_smooth.ino diff --git a/basic/analog_input/photo_3_serial/photo_3_serial.ino b/basic/analog_input/photo_3_serial/photo_3_serial.ino index 2266237..3b0a96d 100644 --- a/basic/analog_input/photo_3_serial/photo_3_serial.ino +++ b/basic/analog_input/photo_3_serial/photo_3_serial.ino @@ -1,6 +1,6 @@ /* Photoresistor - + Utilizzare una fotoresistenza come analog input. Il comportamento della foto resistenza e' simile a un potenziometro: varia la resistenza in base alla @@ -11,6 +11,8 @@ 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 @@ -20,7 +22,7 @@ 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: + // initialize serial communications at 9600 bps: Serial.begin(9600); } @@ -34,7 +36,7 @@ void loop() { // turn the ledPin off: digitalWrite(ledPin, LOW); // stop the program for for milliseconds: - // print the results to the serial monitor: + // print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t delay = "); @@ -43,8 +45,9 @@ void loop() { } /* domande: -1. qual'e' il valore minimo rilevato? -2. quale il massimo? -3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore? -*/ + 1. qual'e' il valore minimo rilevato? + 2. quale il massimo? + 3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore? + */ + 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 b432528..2708689 100644 --- a/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino +++ b/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino @@ -11,14 +11,16 @@ 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 = 240; // valore minimo rilevato dal sensore -int max = 380; // valore massimo rilevato dal sensore +int min = 60; // valore minimo rilevato dal sensore +int max = 600; // valore massimo rilevato dal sensore void setup() { // declare the ledPin as an OUTPUT: @@ -42,14 +44,16 @@ void loop() { // print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); - Serial.print("\t delay = "); + Serial.print("\t cal delay = "); Serial.println(calValue); 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? - */ +/* +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/basic/analog_input/photo_5_calibration/photo_5_calibration.ino b/basic/analog_input/photo_5_calibration/photo_5_calibration.ino new file mode 100644 index 0000000..db0d2b0 --- /dev/null +++ b/basic/analog_input/photo_5_calibration/photo_5_calibration.ino @@ -0,0 +1,80 @@ +/* + 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; + } + } + + // 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/basic/analog_input/photo_6_smooth/photo_6_smooth.ino b/basic/analog_input/photo_6_smooth/photo_6_smooth.ino new file mode 100644 index 0000000..9d1aa9c --- /dev/null +++ b/basic/analog_input/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/piezo/keyboard_three_pullup_buttons/keyboard_three_pullup_buttons.ino b/piezo/keyboard_three_pullup_buttons/keyboard_three_pullup_buttons.ino index 1bd4e47..13fc24c 100644 --- a/piezo/keyboard_three_pullup_buttons/keyboard_three_pullup_buttons.ino +++ b/piezo/keyboard_three_pullup_buttons/keyboard_three_pullup_buttons.ino @@ -15,7 +15,7 @@ */ // Please take care of the include path -#include "/root/arduino/sketchbook-lessons/piezo_mario_tune/pitches.h"; +#include "/home/utente/sketchbook-andrea/piezo/piezo_mario_tune/pitches.h"; int input[]= {1,2,3}; int notes[] = { NOTE_A4, NOTE_C4,NOTE_E3 }; diff --git a/piezo/piezo_mario_tune/piezo_mario_tune.ino b/piezo/piezo_mario_tune/piezo_mario_tune.ino index 26c171a..12dc602 100644 --- a/piezo/piezo_mario_tune/piezo_mario_tune.ino +++ b/piezo/piezo_mario_tune/piezo_mario_tune.ino @@ -5,7 +5,7 @@ last updated: 31/3/13 */ #include - +//#include "/home/utente/sketchbook-andrea/piezo/piezo_mario_tune/pitches.h"; #define melodyPin 3 //Mario main theme melody int melody[] = { -- 2.39.2