-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoaster.ino
293 lines (256 loc) · 8.57 KB
/
coaster.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <EEPROM.h>
#define AREF 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
//Vout TMP36 > A1
//3.3V > V+ TMP36 > AREF
//GND TMP36 > GND
//D6 > Piezo > GND
//D7 > Blue Led (+), Blue Led (-) > 60 Ohm > GND
//D8 > Green Led (+), Green Led (-) > 560 Ohm > GND
//D9 > Red Led (+), Red Led (-) > 640 Ohm > GND
int iPinTemperature = 1;
int iTemperatureReading;
int iPinLedRed = 9;
int iPinLedGreen = 8;
int iPinLedBlue = 7;
int iPinBuzzer = 6;
String strSerialInput = "";
bool bSerialInputComplete = false;
float fTopTemperature = 100;
float fBottomTemperature = 0;
int iEEPROMTopTemperature = 0;
int iEEPROMBottomTemperature = 4;
int iEEPROMDisplayCelcius = 8;
int iEEPROMPlayTopTemperatureSound = 9;
int iEEPROMPlayBottomTemperatureSound = 10;
bool bPlayTopTemperatureSound = true;
bool bPlayBottomTemperatureSound = true;
bool bTempDisplaysCelcius = true;
float fPreviousTemperature = 0;
// Number of average temperatures in celcius
#define AVGC 13
#define POSCOUNT 10
float fArrTemps[AVGC]; // Array used to calculate the average temperature
float fArrTempsD[AVGC]; // Array used to calculate the average delta temperature
int iLEDConfigCurrent = 0; // 1 = Red, 0 = Green, -1 = Blue
int iLEDConfigPrevious = 0; // 1 = Red, 0 = Green, -1 = Blue
bool bLEDsActive = false;
bool bLEDsBlink = false;
float fAverageTemp;
float fAverageTempD;
int iDeltaPositiveCount;
bool bInitAverage = true;
int iAvgIndex;
long lMilliSecondsSinceBeep = -1;
long iMilliSecondsSinceBeepCount = 50000;
long iMilliSecondsSinceLastSwitchCount = 900000;
long lMilliSecondsSinceLastSwitch = iMilliSecondsSinceLastSwitchCount;
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
Serial.println("Setup");
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
pinMode(iPinLedRed, OUTPUT);
pinMode(iPinLedGreen, OUTPUT);
pinMode(iPinLedBlue, OUTPUT);
strSerialInput.reserve(20);
long lTop = EEPROMReadlong(iEEPROMTopTemperature);
if (lTop < 0 || lTop > 10000) {
lTop = 2500;
}
long lBottom = EEPROMReadlong(iEEPROMBottomTemperature);
if (lBottom < 0 || lBottom > 10000) {
lBottom = 2100;
}
if (lTop < lBottom) {
lTop = 2500;
lBottom = 2100;
}
fTopTemperature = (float)lTop / (float)100;
fBottomTemperature = (float)lBottom / (float)100;
bTempDisplaysCelcius = EEPROM.read(iEEPROMDisplayCelcius) == 1;
bPlayTopTemperatureSound = EEPROM.read(iEEPROMPlayTopTemperatureSound) == 1;
bPlayBottomTemperatureSound = EEPROM.read(iEEPROMPlayBottomTemperatureSound) == 1;
}
void loop(void) {
if (bSerialInputComplete) {
Serial.println(strSerialInput);
strSerialInput = "";
bSerialInputComplete = false;
}
iTemperatureReading = analogRead(iPinTemperature);
// Converting temperature voltage to temperature using AREF
float fTemperatureVoltage = iTemperatureReading * AREF;
fTemperatureVoltage /= 1024.0;
float fTemperatureC = (fTemperatureVoltage - 0.5) * 100; //converting from 10 mv per degree with 500 mV offset to degrees ((fTemperatureVoltage - 500mV) times 100
// Initialize average arrays etc
if (bInitAverage) {
for (int j = 0; j < AVGC; j++) {
fArrTemps[j] = fTemperatureC;
fArrTempsD[j] = (float)-0.01;
}
bInitAverage = false;
iAvgIndex = 0;
fAverageTemp = fTemperatureC;
fPreviousTemperature = fTemperatureC;
bLEDsActive = true;
lMilliSecondsSinceBeep = millis();
iLEDConfigPrevious = -1;
}
// Point to next average in the arrays
iAvgIndex++;
if (iAvgIndex > AVGC - 1) {
iAvgIndex = 0;
}
fArrTemps[iAvgIndex] = fTemperatureC;
// Calculate averages
fAverageTemp = 0;
int iPosCount = 0;
for (int j = 0; j < AVGC; j++) {
if (fArrTempsD[j] > (float)0.0) {
iPosCount++;
}
fAverageTemp += fArrTemps[j];
fAverageTempD += fArrTempsD[j];
}
fAverageTemp = fAverageTemp / (float)AVGC;
fAverageTempD = fAverageTempD / (float)AVGC;
Serial.print("[it:");
Serial.print(fTemperatureC);
Serial.print(",tt:");
Serial.print(fTopTemperature);
Serial.print(",bt:");
Serial.print(fBottomTemperature);
Serial.print(",dc:");
Serial.print(bTempDisplaysCelcius);
Serial.print(",at:");
Serial.print(fAverageTemp);
Serial.print(",pt:");
Serial.print(bPlayTopTemperatureSound);
Serial.print(",pb:");
Serial.print(bPlayBottomTemperatureSound);
Serial.println("]");
if (fAverageTemp >= fTopTemperature) {
iLEDConfigCurrent = 1; // Red
}
if (fAverageTemp <= fBottomTemperature) {
iLEDConfigCurrent = -1; // Blue
}
if (fAverageTemp > fBottomTemperature && fAverageTemp < fTopTemperature) {
iLEDConfigCurrent = 0; // Green
}
if (iLEDConfigCurrent >= 0 || (iLEDConfigCurrent == -1 && fAverageTempD > 0 && iPosCount >= POSCOUNT)) {
lMilliSecondsSinceLastSwitch = millis();
}
bLEDsActive = ((millis() - lMilliSecondsSinceLastSwitch) < iMilliSecondsSinceLastSwitchCount);
if (!bLEDsActive || (fAverageTempD > 0 && iPosCount >= POSCOUNT && !bLEDsBlink)) {
digitalWrite(iPinLedRed, LOW);
digitalWrite(iPinLedGreen, LOW);
digitalWrite(iPinLedBlue, LOW);
} else {
if (iLEDConfigCurrent == 1) {
digitalWrite(iPinLedRed, HIGH);
digitalWrite(iPinLedGreen, LOW);
digitalWrite(iPinLedBlue, LOW);
}
if (iLEDConfigCurrent == 0) {
digitalWrite(iPinLedRed, LOW);
digitalWrite(iPinLedGreen, HIGH);
digitalWrite(iPinLedBlue, LOW);
}
if (iLEDConfigCurrent == -1) {
digitalWrite(iPinLedRed, LOW);
digitalWrite(iPinLedGreen, LOW);
digitalWrite(iPinLedBlue, HIGH);
}
}
if (iLEDConfigPrevious == 1 && iLEDConfigCurrent == 0) {
PlayUp();
}
if (iLEDConfigPrevious == 0 && iLEDConfigCurrent == -1) {
PlayDown();
}
fArrTempsD[iAvgIndex] = fAverageTemp - fPreviousTemperature;
fPreviousTemperature = fAverageTemp;
iLEDConfigPrevious = iLEDConfigCurrent;
bLEDsBlink = !bLEDsBlink;
delay(450);
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the strSerialInput:
strSerialInput += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
bSerialInputComplete = true;
if (strSerialInput.startsWith("tt:")) {
strSerialInput = strSerialInput.substring(3);
fTopTemperature = strSerialInput.toFloat();
EEPROMWritelong(iEEPROMTopTemperature, (long)(fTopTemperature * (float)100));
}
if (strSerialInput.startsWith("bt:")) {
strSerialInput = strSerialInput.substring(3);
fBottomTemperature = strSerialInput.toFloat();
EEPROMWritelong(iEEPROMBottomTemperature, (long)(fBottomTemperature * (float)100));
}
bTempDisplaysCelcius = ParseSerialOfBoolIfMatchSetEEPROMAndReturnBool("dc:", bTempDisplaysCelcius, iEEPROMDisplayCelcius);
bPlayTopTemperatureSound = ParseSerialOfBoolIfMatchSetEEPROMAndReturnBool("pt:", bPlayTopTemperatureSound, iEEPROMPlayTopTemperatureSound);
bPlayBottomTemperatureSound = ParseSerialOfBoolIfMatchSetEEPROMAndReturnBool("pb:", bPlayBottomTemperatureSound, iEEPROMPlayBottomTemperatureSound);
}
}
}
bool ParseSerialOfBoolIfMatchSetEEPROMAndReturnBool(String ip_strCommand, bool ip_bOriginalValue, int ip_iEEPROM_Location) {
bool _bResult = ip_bOriginalValue;
if (strSerialInput.startsWith(ip_strCommand)) {
strSerialInput = strSerialInput.substring(3);
_bResult = (strSerialInput == "1\
");
EEPROM.write(ip_iEEPROM_Location, _bResult);
}
return _bResult;
}
void EEPROMWritelong(int address, long value) {
byte by;
for (int i = 0; i < 4; i++) {
by = (value >> ((3 - i) * 8)) & 0x000000ff;
EEPROM.write(address + i, by);
}
}
long EEPROMReadlong(long address) {
long lo = 0;
for (int i = 0; i < 3; i++) {
lo += EEPROM.read(address + i);
lo = lo << 8;
}
lo += EEPROM.read(address + 3);
return lo;
}
void PlayUp() {
Serial.print("Perfect temp");
//if (bPlayTopTemperatureSound && (millis() - lMilliSecondsSinceBeep > iMilliSecondsSinceBeepCount))
{
tone(iPinBuzzer, 440, 100);
delay(100);
tone(iPinBuzzer, 880, 100);
delay(100);
tone(iPinBuzzer, 1760, 100);
lMilliSecondsSinceBeep = millis();
delay(30000);
}
}
void PlayDown() {
Serial.print("Too cold");
//if (bPlayBottomTemperatureSound && (millis() - lMilliSecondsSinceBeep > iMilliSecondsSinceBeepCount))
{
tone(iPinBuzzer, 1760, 100);
delay(100);
tone(iPinBuzzer, 880, 100);
delay(100);
tone(iPinBuzzer, 440, 100);
lMilliSecondsSinceBeep = millis();
delay(30000);
}
}