-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_color_mixing_lamp.cpp
60 lines (48 loc) · 1.75 KB
/
04_color_mixing_lamp.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
57
58
59
// Initialize constants.
const int baudRate = 9600; // Sets the baud rate for the serial port.
const int sensorPin = A0; // Sets the temperature sensor pin.
const float baselineTemp = 20.0; // Sets the baseline temperature for branching.
void setup() {
// Open a serial port with the computer.
Serial.begin (baudRate);
// Initialize the digital pin directions and turn off.
for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop() {
// Read the temperature sensor.
int sensorVal = analogRead(sensorPin);
// Convert the ADC reading to voltage.
float voltage = (sensorVal/1024.0) * 5.0;
// Convert the voltage to temperature in degrees C.
float temperature = (voltage - 0.5) * 100;
// Send the raw sensor value, voltage, and temperature to the computer.
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
Serial.print(", Volts: ");
Serial.print(sensorVal);
Serial.print(", degrees C: ");
Serial.println(temperature);
// Turn off LED's for a low temperature.
if (temperature < baselineTemp + 2) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if ((temperature >= baselineTemp + 2) && (temperature < baselineTemp+4)) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if ((temperature >= baselineTemp + 4) && (temperature < baselineTemp+6)) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} else if (temperature >= baselineTemp + 6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
// Add a small delay as reading values too quickly will make values appear erratic.
delay(1);
}