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