-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource.c
244 lines (194 loc) · 5.31 KB
/
source.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <avr/sleep.h>
//LEDs
int rLed = 0;
int bLed = 4;
//current program and program count
int curProgram = 0;
int progCount = 8;
//button stuff
int buttonPin = 2;
int buttonRead = false;
//Brightness variables for smooth light switching
int rBrightness = 255;
int bBrightness = 0;
int curBrightness = 0;
int fadeAmt = 5;
int rLedOn = true;
//temperature sensor related stuff
int sensorPin = 3;
float temp;
long measureInterval = 3000;
//blink without delay
long previousMillis = 0;
unsigned long currentMillis;
long interval = 1000;
//sleep
int sleepStatus = 0;
void setup()
{
TCCR1 |= _BV (CS10);
GTCCR |= _BV (PWM1B) | _BV(COM1B1); //Enable PWM 1B
DDRB = 0x3f; //outputs
pinMode(sensorPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
readButton();
switch(curProgram) {
case 0:
//go into power saving mode
OCR1B = 0;
digitalWrite(bLed, LOW);
digitalWrite(rLed, LOW);
ADCSRA = ADCSRA & 0b01111111; /* Disable ADC, then sleep! */
sleepNow();
ADCSRA = ADCSRA | 0b10000000;
break;
case 1:
//measure temperature
currentMillis = millis();
if(currentMillis - previousMillis > measureInterval) {
previousMillis = currentMillis;
temp = getTemperature();
rBrightness = (int) ((temp + 10) * (255 / 20));
bBrightness = (int) ((255 - rBrightness) * 127 / 255);
}
if(temp < -10) {
lightBlues(255);
} else if(temp > 10) {
lightReds(255);
} else {
lightReds(rBrightness);
delay(8);
lightBlues(bBrightness);
delay(8);
}
break;
case 2:
// police
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
rLedOn = !rLedOn;
}
if(rLedOn) {
lightReds(255);
} else {
lightBlues(255);
}
break;
case 3:
//fading police
curBrightness = curBrightness + fadeAmt;
curBrightness = (curBrightness >= 255) ? 255 : curBrightness;
curBrightness = (curBrightness <= 0) ? 0 : curBrightness;
if(curBrightness == 0) {
rLedOn = !rLedOn;
}
if (curBrightness <= 0 || curBrightness >= 255) {
fadeAmt = -fadeAmt;
}
if(rLedOn) {
lightReds(curBrightness);
} else {
lightBlues(curBrightness);
}
delay(20);
break;
case 4:
//rapid police
lightReds(255);
delay(50);
lightBlues(255);
delay(50);
break;
case 5:
//blink together
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (curBrightness == 0)
curBrightness = 255;
else
curBrightness = 0;
}
lightReds(curBrightness);
lightBlues(curBrightness);
break;
case 6:
//light only reds
lightReds(255);
lightBlues(0);
break;
case 7:
//light only blues
lightReds(0);
delay(8);
lightBlues(127);
delay(8);
break;
case 8:
//fade together
curBrightness = curBrightness + fadeAmt;
curBrightness = (curBrightness >= 255) ? 255 : curBrightness;
curBrightness = (curBrightness <= 0) ? 0 : curBrightness;
if (curBrightness <= 0 || curBrightness >= 255) {
fadeAmt = -fadeAmt;
}
lightReds(curBrightness);
delay(8);
bBrightness = (curBrightness > 127) ? 127 : curBrightness;
lightBlues(bBrightness);
delay(8);
break;
}
}
void lightReds(int val) {
OCR1B = 0;
digitalWrite(bLed, LOW);
analogWrite(rLed, val);
}
void lightBlues(int val) {
digitalWrite(rLed, LOW);
digitalWrite(bLed, HIGH);
OCR1B = val;
}
float getTemperature() {
lightBlues(127);
GTCCR = GTCCR & 0b11011111; /* Clear COM1B1 */
delay(2);
float voltage = analogRead(sensorPin) * 3.3 / 1024.0;
GTCCR = GTCCR | 0b00100000; /* set bit COM1B1 */
float temperatureC = (voltage - 0.5) * 100;
return temperatureC;
}
void readButton() {
int buttonState = digitalRead(buttonPin);
if(buttonState == LOW) {
if(!buttonRead)
curProgram = (curProgram < progCount) ? curProgram + 1 : 0;
buttonRead = true;
} else {
buttonRead = false;
}
}
void sleepNow()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin
attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
// wakeUpNow when pin 2 gets LOW
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep:
// disable sleep...
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
// wakeUpNow code will not be executed
// during normal running time.
}
void wakeUpNow() // here the interrupt is handled after wakeup
{
lightReds(255);
}