-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson_04_project.ino
42 lines (40 loc) · 1016 Bytes
/
lesson_04_project.ino
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
const int greenLEDPin = 9;
const int blueLEDPin = 10;
const int redLEDPin = 11;
const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup()
{
Serial.begin(9600);
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}
void loop()
{
//Calculate temperature from the sensor value 0-1023 range
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue / 1024.0 * 5.0;
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
//Choose LED color for different temperatures from 0-255 range
if(temperature < baselineTemp)
{
analogWrite(redLEDPin, 0);
analogWrite(greenLEDPin, 0);
analogWrite(blueLEDPin, 50);
}
if(temperature >= baselineTemp)
{
analogWrite(redLEDPin, 0);
analogWrite(greenLEDPin, 150);
analogWrite(blueLEDPin, 0);
}
if(temperature >= baselineTemp + 2)
{
analogWrite(redLEDPin, 250);
analogWrite(greenLEDPin, 0);
analogWrite(blueLEDPin, 0);
}
delay(5000);
}