From db26862cf5427076e1b6c05b5624f3e72b307f41 Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Thu, 9 Apr 2015 17:55:20 +0200 Subject: [PATCH] photosensor --- .../photo_3_serial/photo_3_serial.ino | 50 +++++++++++++++++ .../photo_4_calibrated/photo_4_calibrated.ino | 55 +++++++++++++++++++ .../loop_3_multi_led/loop_3_multi_led.ino | 4 +- 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 basic/analog_input/photo_3_serial/photo_3_serial.ino create mode 100644 basic/analog_input/photo_4_calibrated/photo_4_calibrated.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 new file mode 100644 index 0000000..2266237 --- /dev/null +++ b/basic/analog_input/photo_3_serial/photo_3_serial.ino @@ -0,0 +1,50 @@ +/* + 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 in serie con il sensore. + + Questo sketch modifica l'intervallo di intermittenza di un led + in base alla luminosita' rilevata. + */ + +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? +*/ + diff --git a/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino b/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino new file mode 100644 index 0000000..b432528 --- /dev/null +++ b/basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino @@ -0,0 +1,55 @@ +/* + 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 in serie con il sensore. + + Questo sketch modifica l'intervallo di intermittenza di un led + in base alla luminosita' rilevata. + */ + +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 + +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,1024) ; + // Max pausa = 1024 + // 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: + Serial.print("sensor = " ); + Serial.print(sensorValue); + Serial.print("\t 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? + */ + diff --git a/programming/loops/loop_3_multi_led/loop_3_multi_led.ino b/programming/loops/loop_3_multi_led/loop_3_multi_led.ino index e736eb0..bb7e7ce 100644 --- a/programming/loops/loop_3_multi_led/loop_3_multi_led.ino +++ b/programming/loops/loop_3_multi_led/loop_3_multi_led.ino @@ -26,7 +26,7 @@ void setup() { void loop() { // loop from the lowest pin to the highest: - for (int thisPin = 2; thisPin <= 9; thisPin++) { + for (int thisPin = 2; thisPin < 9; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); @@ -35,7 +35,7 @@ void loop() { } // loop from the highest pin to the lowest: - for (int thisPin = 9; thisPin >= 2; thisPin--) { + for (int thisPin = 9; thisPin > 2; thisPin--) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); -- 2.39.2