]> git.piffa.net Git - sketchbook_andrea/blob - basic/analog_input/photo_5_calibration/photo_5_calibration.ino
Errata calibrazione
[sketchbook_andrea] / basic / analog_input / photo_5_calibration / photo_5_calibration.ino
1 /*
2   Calibration
3  
4  Demonstrates one technique for calibrating sensor input.  The
5  sensor readings during the first five seconds of the sketch
6  execution define the minimum and maximum of expected values
7  attached to the sensor pin.
8  
9  The sensor minimum and maximum initial values may seem backwards.
10  Initially, you set the minimum high and listen for anything 
11  lower, saving it as the new minimum. Likewise, you set the
12  maximum low and listen for anything higher as the new maximum.
13  
14  The circuit:
15  * Analog sensor (potentiometer will do) attached to analog input 0
16  * LED attached from digital pin 9 to ground
17  
18  created 29 Oct 2008
19  By David A Mellis
20  modified 30 Aug 2011
21  By Tom Igoe
22  
23  http://arduino.cc/en/Tutorial/Calibration
24  
25  This example code is in the public domain.
26  Schema:  Schema: https://learn.adafruit.com/assets/460
27  
28  */
29
30 // These constants won't change:
31 const int sensorPin = A0;    // pin that the sensor is attached to
32 const int ledPin = 11;        // pin that the LED is attached to
33
34 // variables:
35 int sensorValue = 0;         // the sensor value
36 int sensorMin = 1023;        // minimum sensor value
37 int sensorMax = 0;           // maximum sensor value
38
39
40 void setup() {
41   // turn on LED to signal the start of the calibration period:
42   pinMode(13, OUTPUT);
43   pinMode(ledPin, OUTPUT);
44   digitalWrite(13, HIGH);
45
46   // calibrate during the first five seconds 
47   while (millis() < 5000) {
48     sensorValue = analogRead(sensorPin);
49
50     // record the maximum sensor value
51     if (sensorValue > sensorMax) {
52       sensorMax = sensorValue;
53     }
54
55     // record the minimum sensor value
56     if (sensorValue < sensorMin) {
57       sensorMin = sensorValue;
58     }
59     delay(5); // Let the sensor rest a bit and stabilize
60   }
61
62   // signal the end of the calibration period
63   digitalWrite(13, LOW);
64 }
65
66 void loop() {
67   // read the sensor:
68   sensorValue = analogRead(sensorPin);
69   // in case the sensor value is outside the range seen during calibration
70   sensorValue = constrain(sensorValue, sensorMin, sensorMax);
71   
72   // apply the calibration to the sensor reading
73   sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
74
75
76
77   // fade the LED using the calibrated value:
78   analogWrite(ledPin, sensorValue);
79 }
80 /*
81 Domande:
82 1. Modificare lo sketch in modo che modifichi la luminosita' di un led 
83 via PWM tramite il valore letto dal sensore.
84 2. Come fare per costringere la variabile dentro un intervallo stabilito?
85 */
86