]> git.piffa.net Git - sketchbook_andrea/blob - basic/analog_input/photo_3_serial/photo_3_serial.ino
analog cleanup
[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  Guida:  https://learn.adafruit.com/photocells/using-a-photocell
17  */
18
19 int sensorPin = A0;   // select the input pin for the potentiometer
20 int ledPin = 3;      // select the pin for the LED
21 int sensorValue = 0;  // variable to store the value coming from the sensor
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   // turn the ledPin on
34   digitalWrite(ledPin, HIGH);  
35   // stop the program for <sensorValue> milliseconds:
36   delay(sensorValue);          
37   // turn the ledPin off:        
38   digitalWrite(ledPin, LOW);   
39   // stop the program for for <sensorValue> milliseconds:
40   // print the results to the serial monitor:
41   Serial.print("sensor = " );                       
42   Serial.print(sensorValue);      
43   Serial.print("\t delay = ");      
44   Serial.println(sensorValue);
45   delay(sensorValue);                  
46 }
47
48 /* domande:
49  1. qual'e' il valore minimo rilevato?
50  2. quale il massimo?
51  3. Modificare lo sketch in modo che modifichi la luminosita' di un LED 
52 via PWM tramite il valore letto dal sensore. (vedi esercizio sucessivo)
53  */
54
55