-
Notifications
You must be signed in to change notification settings - Fork 0
/
04b_night_light.cpp
56 lines (45 loc) · 1.56 KB
/
04b_night_light.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Setup constants for baud rate, and pins we are using as inputs and outputs.
const int baudRate = 9600;
const int sensorPin = A2;
const int greenLEDPin = 9;
const int redLEDPin = 10;
const int blueLEDPin = 11;
// Max value constant that controls the how the LED brightens.
const int maxVal = 50;
// Initialize variables to store sensor reading as well as light level of LED.
int maxLEDValue = 0;
int sensorValue = 0;
void setup() {
// Setup serial port.
Serial.begin(baudRate);
// Set the direction of the digital pins.
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}
void loop() {
// Read value of light sensor. Use a small delay to allow ADC to complete read.
sensorValue = analogRead(sensorPin);
delay(5);
// Report the sensor readings to the computer.
Serial.print("Raw Sensor Value: ");
Serial.println(sensorValue);
// Hacky way to convert the sensor reading to a LED value.
// We want the LED brightness to increase as the sensorValue diminishes to zero.
// Calibrate by setting the maxLEDValue constant.
LEDValue = sensorValue;
LEDValue = maxLEDValue - LEDValue;
if (LEDValue < 0) {
LEDValue = 0;
} else if (LEDValue > maxLEDValue) {
LEDValue = maxLEDValue;
}
// Report the calculated LED light level values.
Serial.print("Mapped Sensor Value: ");
Serial.println(LEDValue);
// Set the LED light levels.
// All of the pins are mapped to the same light sensor.
analogWrite(redLEDPin, LEDValue);
analogWrite(greenLEDPin, LEDValue);
analogWrite(blueLEDPin, LEDValue);
}