-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoven.ino
344 lines (269 loc) · 6.94 KB
/
oven.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* Matthew's Microwave Oven
*/
#include <Wire.h> // Include the Arduino SPI library
#include "pitches.h"
#include "S7S.h"
#include "RotaryEncoder.h"
#include "RGB.h"
// Pin number the push button is connected to
const int buttonPin = 13;
// The push button states
int buttonState = 0;
int lastButtonState = 0;
boolean wasLongPress = false;
// This is the mode the program is currently in
int mode = 0;
// Number to display on the LED S7S
int displayValue = 0;
int lastDisplayValue = 0;
// Pin numbers for the RGB LEB
const int ledRedPin = 6;
const int ledGreenPin = 9;
const int ledBluePin = 10;
RGB rgb(ledRedPin, ledGreenPin, ledBluePin, false);
// I2C address of our S7S
const byte s7sAddress = 0x71;
S7S s7s(s7sAddress);
// Will be used with sprintf to create strings
char tempString[10];
// Piezo buzzer pin number
int buzzerPin = 7;
// Place to store current time
unsigned long now;
long offset = 0;
// The time (in ms) when the button was pressed down
unsigned long buttonDownTime;
boolean cycleColors = true;
// Amount of time (in ms) to hold the button down to set the clock
const int buttonSetClockDuration = 8000; // 8 seconds
const int MODE_CLOCK = 0;
const int MODE_SET_COOK_TIME = 1;
const int MODE_COOK = 2;
const int MODE_SET_CLOCK = 3;
void setup() {
Serial.begin(115200);
// Attach interrupts for rotary encoder
re_setup();
// Enable push button in LOW state
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW);
// Set the RGB LEB pins as output
rgb.setup();
// Initialize hardware I2C pins for S7S
Wire.begin();
// Clear display, reset cursor
s7s.clearDisplay();
// Set baud rate higher
s7s.setBaudRate(6);
// Set brightness
s7s.setBrightness(255);
// Welcome
s7s.sendString("-HI-");
Serial.println("Hi");
delay(2000);
// Clear the display before jumping into loop
s7s.clearDisplay();
}
void loop() {
doButtonCheck();
switch (mode) {
case MODE_CLOCK:
displayClock();
break;
case MODE_SET_COOK_TIME:
adjustCountdownTimer();
break;
case MODE_COOK:
cooking();
break;
case MODE_SET_CLOCK:
setClock();
break;
}
}
void doButtonCheck() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
if (lastButtonState == HIGH) {
Serial.println("Button Pushed");
switchMode();
}
} else {
if (lastButtonState == LOW) {
// Record time button was pushed
buttonDownTime = millis();
}
// Check for long press
if (millis() - buttonDownTime >= buttonSetClockDuration) {
wasLongPress = true;
// Set time setup goes here
rgb.setColor(0, 128, 255);
cycleColors = false;
//offset = (pulses / 4) * 1000 * 60;
long pulses = offset / 60 / 1000 * 4;
re_setPulses(pulses);
}
}
// Save the previous state for the next loop
lastButtonState = buttonState;
}
void switchMode() {
if (wasLongPress) {
mode = MODE_SET_CLOCK;
wasLongPress = false;
} else {
// Normal mode switch
mode++;
mode = mode % 3;
Serial.print("Now in mode: ");
Serial.println(mode);
// Reset pulses for normal mode switch
re_resetPulses();
cycleColors = true;
}
// Always clear display before switching modes
s7s.clearDisplay();
}
void displayClock() {
now = millis() + offset + 86400000;
int seconds = (now / 1000) % 60;
int minutes = (now / 1000 / 60) % 60;
int hours24 = (now / 1000 / 60 / 60) % 24;
int hours = hours24 % 12;
int cycleSpeed = 500;
if (hours == 0) {
hours = 12;
}
sprintf(tempString, "%2d%02d", hours, minutes);
s7s.sendString(tempString);
if (mode == MODE_SET_CLOCK) {
// Decimals
if (hours24 >= 12) {
s7s.setDecimals(0b00011000);
} else {
s7s.setDecimals(0b00010000);
}
} else {
// Decimals
if (seconds % 2 == 0) {
if (hours24 >= 12) {
s7s.setDecimals(0b00011000);
} else {
s7s.setDecimals(0b00010000);
}
} else {
if (hours24 >= 12) {
s7s.setDecimals(0b00001000);
} else {
s7s.setDecimals(0b00000000);
}
}
// Brightness
int s7sBrightness = 255;
int buttonBrightness = 255;
if (hours24 < 7 || hours24 >= 20) {
s7sBrightness = 32;
buttonBrightness = 32;
}
s7s.setBrightness(s7sBrightness);
if (minutes == 0) {
cycleColors = false;
} else {
cycleColors = true;
}
// Cycle colors
if (cycleColors) {
int v = (now / cycleSpeed) % 360;
rgb.cycleColor(v, buttonBrightness);
} else {
rgb.setColor(174, 255, 255);
}
}
}
void setClock() {
rgb.setColor(0, 0, 255);
long pulses = (long) re_getPulses();
offset = (pulses / 4) * 1000 * 60;
displayClock();
}
void adjustCountdownTimer() {
rgb.setColor(0, 255, 0);
s7s.setBrightness(255);
int pulses = re_getPulses();
if (pulses < 0) {
re_resetPulses();
}
displayValue = pulses / 4;
updateDisplay();
}
void cooking() {
rgb.setColor(255, 0, 0);
s7s.setBrightness(255);
displayValue--;
updateDisplay();
if (displayValue <= 0) {
soupsReady();
} else {
for (int i = 0; i < 5; i++) {
tone(buzzerPin, NOTE_C1, 100);
delay(100);
tone(buzzerPin, NOTE_C2, 100);
delay(100);
}
noTone(buzzerPin);
}
}
void soupsReady() {
// Switch to clock mode after song and dance
mode = MODE_CLOCK;
displayValue = 0;
re_resetPulses();
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4 };
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8, 8, 4, 4, 4, 2 };
int colors[][3] = {
{255, 255, 0},
{255, 255, 255}, // was 128 Blue (pin 10)
{255, 255, 0},
{255, 128, 0},
{255, 0, 0},
{255, 0, 255}
};
char* display[] = {
" 0",
" 0",
" 00",
" 000",
"0000",
"0 0"
};
int numberOfNotes = sizeof(melody) / sizeof(int);
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < numberOfNotes; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1800/noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
// Change LED color
rgb.setColor(colors[thisNote][0], colors[thisNote][1], colors[thisNote][2]);
s7s.sendString(display[thisNote]);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(buzzerPin);
}
}
void updateDisplay() {
// Magical sprintf creates a string for us to send to the s7s.
// The %4d option creates a 4-digit integer.
sprintf(tempString, "%4d", displayValue);
// This will output the tempString to the S7S
s7s.sendString(tempString);
}