-
Notifications
You must be signed in to change notification settings - Fork 1
/
drumbo.ino
244 lines (194 loc) · 5.14 KB
/
drumbo.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
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
void setup() {
// initialize the serial communication:
Serial.begin(9600);
EsploraTFT.begin();
EsploraTFT.background(255, 255, 255);
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.fill(255, 255, 255);
EsploraTFT.text("Time", 1, 1);
EsploraTFT.line(0, 63, 159, 63);
EsploraTFT.text("Score", 1, 65);
EsploraTFT.setTextSize(5);
enterRunning();
}
int MODE_READY = 1;
int MODE_SETTING = 2;
int MODE_RUNNING = 3;
int MODE_FINISHED = 4;
boolean hit = false;
boolean isDepressed = false;
unsigned long depressedSince;
int hitCount;
int mode = MODE_RUNNING;
int minDuration = 1;
int maxDuration = 300;
unsigned long duration = 60;
int elapsed = 0;
unsigned long timeStarted;
char scoreChars[5];
char timeChars[5];
void displayTime() {
EsploraTFT.stroke(255, 255, 255);
EsploraTFT.text(timeChars, 1, 18);
}
void displayTime(unsigned long value) {
displayTime();
String(value).toCharArray(timeChars, 5);
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.text(timeChars, 1, 18);
}
void displayScore() {
EsploraTFT.stroke(255, 255, 255);
EsploraTFT.text(scoreChars, 1, 82);
}
void displayScore(int value) {
displayScore();
String(value).toCharArray(scoreChars, 5);
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.text(scoreChars, 1, 82);
}
void enterSetting() {
isDepressed = false;
displayTime(duration);
displayScore(0);
mode = MODE_SETTING;
}
unsigned int oldTimeBlink;
void doSetting() {
// handle done / exit setting mode
if (Esplora.readButton(SWITCH_DOWN) == LOW) {
enterRunning();
}
unsigned long mills = millis();
// blink LED green
if (mills / 500 % 2 == 0) {
Esplora.writeRGB(0,255,0);
} else {
Esplora.writeRGB(0,0,0);
}
boolean isLeftPressed = (Esplora.readButton(SWITCH_LEFT) == LOW);
boolean isRightPressed = (Esplora.readButton(SWITCH_RIGHT) == LOW);
if (isDepressed && !isLeftPressed && !isRightPressed) {
isDepressed = false;
} else if (!isDepressed && (isLeftPressed || isRightPressed)) {
isDepressed = true;
depressedSince = mills;
}
if ((isLeftPressed && (duration > minDuration)) || (isRightPressed && (duration < maxDuration))) {
// receiving input
int adjustment = isLeftPressed ? -1 : 1;
unsigned long millisElapsed = mills - depressedSince;
int sub = 1000 / sq(millisElapsed / 1000 + 1);
if ((millisElapsed > 4000) || (millisElapsed % 1000) % sub == 0) {
duration += adjustment;
displayTime(duration);
}
} else {
// waiting for input
// blink current setting while standing by
int timeBlink = mills / 500 % 2; // result is either 0 or 1
if (timeBlink && !oldTimeBlink) {
displayTime();
} else if (!timeBlink && oldTimeBlink) {
displayTime(duration);
}
oldTimeBlink = timeBlink;
}
}
void enterRunning() {
isDepressed = false;
hitCount = 0;
displayScore(0);
displayTime(duration);
delay(10);
mode = MODE_RUNNING;
}
unsigned int oldScoreBlink;
unsigned int oldSecondsRemaining;
void doRunning() {
// handle escape / go to time setting mode
if (Esplora.readButton(SWITCH_UP) == LOW) {
enterSetting();
} else if (Esplora.readButton(SWITCH_LEFT) == LOW) {
// reset / restart
enterRunning();
}
unsigned long mills = millis();
unsigned long millisElapsed;
unsigned int secondsRemaining;
if (hitCount == 0) {
// standby (no hits counted yet)
millisElapsed = 0;
secondsRemaining = duration;
// blink zero score while standing by
int scoreBlink = mills / 500 % 2; // result is either 0 or 1
if (scoreBlink && !oldScoreBlink) {
displayScore(0);
Esplora.writeRGB(0,0,255);
} else if (!scoreBlink && oldScoreBlink) {
displayScore();
Esplora.writeRGB(0,0,0);
}
oldScoreBlink = scoreBlink;
} else {
// timer is running
millisElapsed = mills - timeStarted;
secondsRemaining = duration - millisElapsed / 1000;
// update time display if needed
if (secondsRemaining != oldSecondsRemaining) {
displayTime(secondsRemaining);
}
oldSecondsRemaining = secondsRemaining;
}
if (hitCount && !secondsRemaining) {
// done: timer just ran out
mode = MODE_FINISHED;
} else {
// check for a hit and update counter / display if so.
boolean newHit = false;
unsigned int thresh = Esplora.readSlider();
unsigned int level = Esplora.readMicrophone();
if (level > thresh) {
if (!hit) {
if (!hitCount) {
Esplora.writeRGB(0,0,255);
timeStarted = millis();
}
newHit = true;
hit = true;
hitCount++;
}
} else if (hit && (level < (thresh / 2))) {
hit = false;
displayScore(hitCount);
}
}
}
void doFinished() {
// hit left button to run again
if (Esplora.readButton(SWITCH_LEFT) == LOW) {
enterRunning();
}
// blink LED red
if (millis() / 1000 % 2 == 0) {
Esplora.writeRGB(255,0,0);
} else {
Esplora.writeRGB(0,0,0);
}
}
void loop() {
switch (mode) {
case 4:
doFinished();
break;
case 2:
doSetting();
break;
case 3:
default:
doRunning();
}
}