]> git.piffa.net Git - sketchbook_andrea/blob - basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino
photosensor
[sketchbook_andrea] / basic / analog_input / photo_4_calibrated / photo_4_calibrated.ino
1 /*
2   Photoresistor
3  
4  Utilizzare una fotoresistenza come analog input.
5  Il comportamento della foto resistenza e' simile
6  a un potenziometro: varia la resistenza in base alla 
7  quantita' di luce.
8  
9  Per ottenere valori significativi utilizzare unaresistenza
10  da ~5k ohms in serie con il sensore.
11  
12  Questo sketch modifica l'intervallo di intermittenza di un led
13  in base alla luminosita' rilevata.
14  */
15
16 int sensorPin = A0;    // select the input pin for the potentiometer
17 int ledPin = 13;      // select the pin for the LED
18 int sensorValue = 0;  // variable to store the value coming from the sensor
19
20 int min = 240;        // valore minimo rilevato dal sensore
21 int max = 380;        // valore massimo rilevato dal sensore
22
23 void setup() {
24   // declare the ledPin as an OUTPUT:
25   pinMode(ledPin, OUTPUT);  
26   // initialize serial communications at 9600 bps:
27   Serial.begin(9600); 
28 }
29
30 void loop() {
31   // read the value from the sensor:
32   sensorValue = analogRead(sensorPin);   
33   int calValue = map(sensorValue,min,max,0,1024) ; 
34   // Max pausa = 1024 
35       // turn the ledPin on
36     digitalWrite(ledPin, HIGH);  
37   // stop the program for <sensorValue> milliseconds:
38   delay(calValue);          
39   // turn the ledPin off:        
40   digitalWrite(ledPin, LOW);   
41   // stop the program for for <sensorValue> milliseconds:
42   // print the results to the serial monitor:
43   Serial.print("sensor = " );                       
44   Serial.print(sensorValue);      
45   Serial.print("\t delay = ");      
46   Serial.println(calValue);
47   delay(sensorValue);                  
48 }
49
50 /* domande:
51  1. qual'e' il valore minimo rilevato?
52  2. quale il massimo?
53  3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore?
54  */
55