]> git.piffa.net Git - sketchbook_andrea/blob - oggi/photo_3_serial/photo_3_serial.ino
Analog
[sketchbook_andrea] / oggi / 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 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 Links:
18
19 - http://www.pighixxx.com/test/portfolio-items/connect-a-photoresistor/
20
21 Divisori di voltaggio:
22 - https://en.wikipedia.org/wiki/Electrical_resistivity_and_conductivity
23 - https://learn.sparkfun.com/tutorials/voltage-dividers
24 - http://www.pighixxx.com/test/wp-content/uploads/2014/10/126.png
25  */
26
27 int sensorPin = A0;    // select the input pin for the potentiometer
28 int ledPin = 13;      // select the pin for the LED
29 int sensorValue = 0;  // variable to store the value coming from the sensor
30
31 void setup() {
32   // declare the ledPin as an OUTPUT:
33   pinMode(ledPin, OUTPUT);  
34   // initialize serial communications at 9600 bps:
35   Serial.begin(9600); 
36 }
37
38 void loop() {
39   // read the value from the sensor:
40   sensorValue = analogRead(sensorPin);    
41   // turn the ledPin on
42   digitalWrite(ledPin, HIGH);  
43   // stop the program for <sensorValue> milliseconds:
44   delay(sensorValue);          
45   // turn the ledPin off:        
46   digitalWrite(ledPin, LOW);   
47
48   // stop the program for for <sensorValue> milliseconds:
49   // print the results to the serial monitor:
50   Serial.print("sensor = " );                       
51   Serial.print(sensorValue);      
52   Serial.print("\t delay = ");      
53   Serial.println(sensorValue);
54   delay(sensorValue);                  
55 }
56
57 /* domande:
58  1. qual'e' il valore minimo rilevato?
59  2. quale il massimo?
60  3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore?
61  4. A cosa serve la resistenza da 10 - 5k ohms ?
62
63  .
64  .
65  .
66  .
67  .
68  .
69  .
70  .
71  .
72  .
73  .
74  .
75  .
76  .
77  .
78 Risposte:
79
80 3. Vedi esercizio suciessivo
81 4. Serve come pull down per il pin che altrimenti sarebbe un sensore
82 ad alta impendenza flottante e come divisore di voltaggio.
83
84  */
85
86