]> git.piffa.net Git - sketchbook_andrea/blob - basic/analog_input/photo_3_serial/photo_3_serial.ino
photosensor
[sketchbook_andrea] / basic / analog_input / photo_3_serial / photo_3_serial.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 void setup() {
21   // declare the ledPin as an OUTPUT:
22   pinMode(ledPin, OUTPUT);  
23     // initialize serial communications at 9600 bps:
24   Serial.begin(9600); 
25 }
26
27 void loop() {
28   // read the value from the sensor:
29   sensorValue = analogRead(sensorPin);    
30   // turn the ledPin on
31   digitalWrite(ledPin, HIGH);  
32   // stop the program for <sensorValue> milliseconds:
33   delay(sensorValue);          
34   // turn the ledPin off:        
35   digitalWrite(ledPin, LOW);   
36   // stop the program for for <sensorValue> milliseconds:
37     // print the results to the serial monitor:
38   Serial.print("sensor = " );                       
39   Serial.print(sensorValue);      
40   Serial.print("\t delay = ");      
41   Serial.println(sensorValue);
42   delay(sensorValue);                  
43 }
44
45 /* domande:
46 1. qual'e' il valore minimo rilevato?
47 2. quale il massimo?
48 3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore?
49 */
50