-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab02.ino
124 lines (104 loc) · 2.83 KB
/
Lab02.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const int greenLedPin = 2;
const int redLedPin = 3;
const int yellowLedPin = 4;
const int tempSensor = A0;
const int lightSensor = A1;
const int angleSensor = A3;
int tempRead = 0;
int lightRead = 0;
int angleRead = 0;
int lastAngleRead = -1;
int lightReadMinValue = 1023; //Start on max
int lightReadMaxValue = 0; //Start on min
float temperature = 0;
int light = 0;
int angle = 0;
boolean isAngleOn = false;
int intervalTime = 0;
unsigned long previousTime = 0, currentTime = 0;
void setup() {
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
// Calibrate Light Sensor
// Start Calibration
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, HIGH);
// Calibration During the First Five Seconds
while (millis() < 3000) {
lightRead = analogRead(lightSensor);
// Record the Maximum Value Read
if (lightRead > lightReadMaxValue) {
lightReadMaxValue = lightRead;
}
// Record the Minimum Value Read
if (lightRead < lightReadMinValue) {
lightReadMinValue = lightRead;
}
}
// End Calibration
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
}
void loop() {
// Temperature
tempRead = analogRead(tempSensor); // Read Temperature
temperature = (((tempRead / 1024.0) * 5.0 ) - 0.5) * 100;
if(temperature > 30) {
digitalWrite(greenLedPin, HIGH);
} else {
digitalWrite(greenLedPin, LOW);
}
// Light
lightRead = analogRead(lightSensor); // Read Light
light = map(lightRead, lightReadMinValue, lightReadMaxValue, 255, 0); // Convert Analog Value
light = constrain(light, 0, 255);
// Noise Reduction
if(light < 20) {
light = 0;
} else if (light < 40) {
light = 30;
} else if (light < 60) {
light = 50;
} else if (light < 80) {
light = 70;
} else if (light < 100) {
light = 90;
} else if (light < 120) {
light = 110;
} else if (light < 140) {
light = 130;
} else if (light < 160) {
light = 150;
} else if (light < 180) {
light = 170;
} else if (light < 200) {
light = 190;
} else if (light < 220) {
light = 210;
} else if (light < 240) {
light = 230;
} else {
light = 255;
}
analogWrite(redLedPin, light);
// Potentiometer
angleRead = analogRead(angleSensor); // Read Angle
if (lastAngleRead != angleRead) {
angle = map(angleRead, 0, 1023, 0, 180); // Map Read Value to Angle
intervalTime = map(angle, 0, 180, 200, 2000); // Map Angle to Miliseconds
lastAngleRead = angleRead;
}
currentTime = millis();
if (currentTime - previousTime >= intervalTime) {
isAngleOn = !isAngleOn;
if(isAngleOn) {
digitalWrite(yellowLedPin, HIGH);
} else {
digitalWrite(yellowLedPin, LOW);
}
previousTime = currentTime;
}
}