1 | /*
|
---|
2 | Esplora Led calibration
|
---|
3 |
|
---|
4 | This sketch shows you how to read and calibrate the light sensor.
|
---|
5 | Because light levels vary from one location to another, you need to calibrate the
|
---|
6 | sensor for each location. To do this, you read the sensor for a few seconds,
|
---|
7 | and save the highest and lowest readings as maximum and minimum.
|
---|
8 | Then, when you're using the sensor's reading (for example, to set the brightness
|
---|
9 | of the LED), you map the sensor's reading to a range between the minimum
|
---|
10 | and the maximum.
|
---|
11 |
|
---|
12 | Created on 22 Dec 2012
|
---|
13 | by Tom Igoe
|
---|
14 |
|
---|
15 | This example is in the public domain.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <Esplora.h>
|
---|
19 |
|
---|
20 | // variables:
|
---|
21 | int lightMin = 1023; // minimum sensor value
|
---|
22 | int lightMax = 0; // maximum sensor value
|
---|
23 | boolean calibrated = false; // whether the sensor's been calibrated yet
|
---|
24 |
|
---|
25 | void setup() {
|
---|
26 | // initialize the serial communication:
|
---|
27 | Serial.begin(9600);
|
---|
28 |
|
---|
29 | // print an intial message
|
---|
30 | Serial.println("To calibrate the light sensor, press and hold Switch 1");
|
---|
31 | }
|
---|
32 |
|
---|
33 | void loop() {
|
---|
34 | // if switch 1 is pressed, go to the calibration function again:
|
---|
35 | if (Esplora.readButton(1) == LOW) {
|
---|
36 | calibrate();
|
---|
37 | }
|
---|
38 | // read the sensor into a variable:
|
---|
39 | int light = Esplora.readLightSensor();
|
---|
40 |
|
---|
41 | // map the light level to a brightness level for the LED
|
---|
42 | // using the calibration min and max:
|
---|
43 | int brightness = map(light, lightMin, lightMax, 0, 255);
|
---|
44 | // limit the brightness to a range from 0 to 255:
|
---|
45 | brightness = constrain(brightness, 0, 255);
|
---|
46 | // write the brightness to the blue LED.
|
---|
47 | Esplora.writeBlue(brightness);
|
---|
48 |
|
---|
49 | // if the calibration's been done, show the sensor and brightness
|
---|
50 | // levels in the serial monitor:
|
---|
51 | if (calibrated == true) {
|
---|
52 | // print the light sensor levels and the LED levels (to see what's going on):
|
---|
53 | Serial.print("light sensor level: ");
|
---|
54 | Serial.print(light);
|
---|
55 | Serial.print(" blue brightness: ");
|
---|
56 | Serial.println(brightness);
|
---|
57 | }
|
---|
58 | // add a delay to keep the LED from flickering:
|
---|
59 | delay(10);
|
---|
60 | }
|
---|
61 |
|
---|
62 | void calibrate() {
|
---|
63 | // tell the user what do to using the serial monitor:
|
---|
64 | Serial.println("While holding switch 1, shine a light on the light sensor, then cover it.");
|
---|
65 |
|
---|
66 | // calibrate while switch 1 is pressed:
|
---|
67 | while (Esplora.readButton(1) == LOW) {
|
---|
68 | // read the sensor value:
|
---|
69 | int light = Esplora.readLightSensor();
|
---|
70 |
|
---|
71 | // record the maximum sensor value:
|
---|
72 | if (light > lightMax) {
|
---|
73 | lightMax = light;
|
---|
74 | }
|
---|
75 |
|
---|
76 | // record the minimum sensor value:
|
---|
77 | if (light < lightMin) {
|
---|
78 | lightMin = light;
|
---|
79 | }
|
---|
80 | // note that you're calibrated, for future reference:
|
---|
81 | calibrated = true;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|