-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIkeaMicrowave.ino
More file actions
197 lines (157 loc) · 3.59 KB
/
IkeaMicrowave.ino
File metadata and controls
197 lines (157 loc) · 3.59 KB
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
#include <avr/sleep.h>
#include <TM1637Display.h>
#include <ClickEncoder.h>
#include <TimerOne.h>
#define SLEEP_INTERRUPT_1 2
#define SLEEP_INTERRUPT_2 3
#define SPEAKER 5
#define LIGHT 6
#define ENCODER_BUTTON 2
#define ENCODER_A 3
#define ENCODER_B 4
#define MAX_SECONDS 10 * 60
#define SLEEP_TIMEOUT 60 * 1000;
#define CLK 12
#define DIO 11
ClickEncoder *encoder;
TM1637Display display(CLK, DIO);
uint8_t data[] = {0, 0, 0, 0};
int16_t last, left, milli, sleepTimer;
bool running, showEnd, lightOn;
void timerIsr() {
encoder->service();
if(running) {
milli -= 1;
}
sleepTimer -= 1;
}
void feedSleepTimer() {
sleepTimer = SLEEP_TIMEOUT;
}
void goToSleep() {
displayOff();
Timer1.detachInterrupt();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(digitalPinToInterrupt(SLEEP_INTERRUPT_1), wakeUp, CHANGE);
attachInterrupt(digitalPinToInterrupt(SLEEP_INTERRUPT_2), wakeUp, CHANGE);
sleep_mode();
// Zzzzz. When we wake, we resume here...
sleep_disable();
detachInterrupt(digitalPinToInterrupt(SLEEP_INTERRUPT_2));
detachInterrupt(digitalPinToInterrupt(SLEEP_INTERRUPT_1));
// Run setup again, so everything is back to the initial state
setup();
}
void wakeUp() {
// This happens after we wake up.
feedSleepTimer();
}
void displayOff() {
// We may need to turn off the display using hardware...
display.setBrightness(0, false);
display.setSegments(data);
}
void displayOn() {
display.setBrightness(7, true);
display.setSegments(data);
}
void displayEnd() {
//
// A
// ---
// F | | B
// -G-
// E | | C
// ---
// D
// XGFEDCBA
data[0] = 0;
data[1] = 0b01111001;
data[2] = 0b01010100;
data[3] = 0b01011110;
}
void displayTime(void) {
int8_t minutes = left / 60;
int8_t seconds = left % 60;
uint8_t dot = (!running || milli < 500) ? 0x80 : 0x0;
data[0] = display.encodeDigit(minutes / 10);
data[1] = display.encodeDigit(minutes % 10) | dot;
data[2] = display.encodeDigit(seconds / 10);
data[3] = display.encodeDigit(seconds % 10);
}
void updateTime(void) {
if(showEnd) {
displayEnd();
} else {
displayTime();
}
display.setSegments(data);
}
void setup() {
encoder = new ClickEncoder(ENCODER_A, ENCODER_B, ENCODER_BUTTON, 4, HIGH);
encoder->setAccelerationEnabled(true);
Timer1.initialize(1000);
Timer1.attachInterrupt(timerIsr);
pinMode(LIGHT, OUTPUT);
running = false;
showEnd = false;
lightOn = false;
left = 0;
last = 0;
milli = 1000;
feedSleepTimer();
displayOn();
updateTime();
}
void checkButton() {
ClickEncoder::Button b = encoder->getButton();
if(b == ClickEncoder::Released) {
running = !running;
}
}
void checkLight() {
if(running && !lightOn) {
digitalWrite(LIGHT, HIGH);
lightOn = true;
} else if(!running && lightOn) {
digitalWrite(LIGHT, LOW);
lightOn = false;
}
}
void done() {
showEnd = true;
tone(SPEAKER, 1500, 2000);
}
void loop() {
checkButton();
left += encoder->getValue();
if(left > MAX_SECONDS) {
left = MAX_SECONDS;
}
if(milli <= 0) {
left--;
milli = 1000;
}
if(left < 0) {
left = 0;
}
// If the timer is running, the display needs to be updated every 500ms, otherwise it only needs updating if the encode value changes.
if ((running && milli / 500 % 2 == 0) || left != last) {
if(left != last) {
last = left;
}
if(running && left == 0) {
running = false;
done();
} else {
showEnd = false;
}
updateTime();
feedSleepTimer();
}
checkLight();
if(sleepTimer == 0) {
goToSleep();
}
}