-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi-stomptroller.ino
383 lines (297 loc) · 7.06 KB
/
midi-stomptroller.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <OneButton.h>
#include <MIDI.h>
#include <Adafruit_NeoPixel.h>
#include <NoDelay.h>
void setBlink(int n, int repeat=1);
void updateBlink();
void updateBPM();
/** Button Config
---------
| 1 2 |
| |
| |
| 3 4 |
---------
*/
// configure button pins
OneButton button1(4, true, true);
OneButton button2(5, true, true);
OneButton button3(7, true, true);
OneButton button4(9, true, true);
// Create and bind the MIDI interface to the default hardware Serial port
MIDI_CREATE_DEFAULT_INSTANCE();
const int MIDI_Base = 1;
// configure Neopixel LED's
const int PIN = 10;
const int NUMPIXELS = 4;
const int neoBrightness = 15;
/** Mapping for NeoPixels to button
Mapping below matches the included schematic
<button number> = <NeoPixel address>;
*/
const int neoButton1 = 2;
const int neoButton2 = 1;
const int neoButton3 = 3;
const int neoButton4 = 4;
//Create pixles object
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//Var to handle page state
int page = 0;
//Define colours
int page0 [3] = {100, 0, 100};
int page1 [3] = {0, 100, 100};
int blinkColour[3] = {255, 255, 255};
int bpmColour[3] = {100, 0, 0};
//Timimng interval vars
int blinkState[4] = {0, 0, 0, 0};
noDelay blinkTime(250, updateBlink, false);
//BPM interval vars
bool blinkBPM_State = true;
int bpmLED = neoButton4;
long lastPress = millis();
noDelay bpmTime(500, updateBPM, false);
void setup() {
Serial.begin(9600);
// button 1
button1.attachClick(button1Press);
button1.attachLongPressStart(button1LongPressStart);
// button 2
button2.attachClick(button2Press);
button2.attachLongPressStart(button2LongPressStart);
// button 3
button3.attachClick(button3Press);
button3.attachLongPressStart(button3LongPressStart);
// button 4
button4.attachClick(button4Press);
button4.attachLongPressStart(button4LongPressStart);
// MIDI setup
MIDI.begin();
// Pixel Setup
pixels.begin();
pixels.setBrightness(neoBrightness);
colourPage();
}
void loop() {
button1.tick();
button2.tick();
button3.tick();
button4.tick();
blinkTime.update();
bpmTime.update();
}
//BUTTON EVENT CONFIGS
// button 1 Events
void button1Press() {
switch (page) {
//Page 0
case 0:
Serial.write("Button 1 Page 0 \n");
// Snapshot 1
MIDI.sendControlChange(69, 0, MIDI_Base);
break;
//Page 1
case 1:
Serial.write("Button 1 Page 1 \n");
//Note D2 ON
MIDI.sendNoteOn(38, 127, MIDI_Base);
break;
}
setBlink(neoButton1);
}
void button1LongPressStart() {
Serial.write("Button 1 Long \n");
// Switch Page
if (page == 0) {
stopBPM();
page = 1;
} else {
page = 0;
}
setBlink(neoButton1, 2);
}
// button 2 Events
void button2Press() {
switch (page) {
//Page 0
case 0:
Serial.write("Button 2 Page 0 \n");
// Snapshot 2
MIDI.sendControlChange(69, 1, MIDI_Base);
break;
//Page 1
case 1:
Serial.write("Button 2 Page 1 \n");
//Note D#2 ON
MIDI.sendNoteOn(39, 127, MIDI_Base);
break;
}
setBlink(neoButton2);
}
void button2LongPressStart() {
switch (page) {
//Page 0
case 0:
Serial.write("Button 2 Page 0 Long \n");
// FS5
MIDI.sendControlChange(72, 127, MIDI_Base);
break;
//Page 1
case 1:
Serial.write("Button 2 Page 1 Long \n");
// FS5
MIDI.sendControlChange(72, 127, MIDI_Base);
break;
}
setBlink(neoButton2,2);
}
// button 3 Events
void button3Press() {
switch (page) {
//Page 0
case 0:
Serial.write("Button 3 Page 0 \n");
// Snapshot 3
MIDI.sendControlChange(69, 2, MIDI_Base);
break;
//Page 1
case 1:
Serial.write("Button 3 Page 1 \n");
//Note E2 ON
MIDI.sendNoteOn(40, 127, MIDI_Base);
break;
}
setBlink(neoButton3);
}
void button3LongPressStart() {
switch (page) {
//Page 0
case 0:
Serial.write("Button 3 Page 0 Long\n");
// Next Footswitch Mode
MIDI.sendControlChange(71, 4, MIDI_Base);
break;
//Page 1
case 1:
Serial.write("Button 3 Page 1 Long\n");
//Note C3 ON
MIDI.sendNoteOn(48, 127, MIDI_Base);
break;
}
setBlink(neoButton3,2);
}
// button 4 Events
void button4Press() {
switch (page) {
//Page 0
case 0:
Serial.write("Button 4 Page 0 \n");
// Tap tempo
MIDI.sendControlChange(64, 127, MIDI_Base);
pressBPM();
break;
//Page 1
case 1:
Serial.write("Button 4 Page 1 \n");
//Note C#3 ON
MIDI.sendNoteOn(49, 127, MIDI_Base);
setBlink(neoButton4);
break;
}
}
void button4LongPressStart() {
switch (page) {
//Page 0
case 0:
Serial.write("Button 4 Page 0 Long \n");
stopBPM();
// show tuner
MIDI.sendControlChange(68, 0, MIDI_Base);
break;
//Page 1
case 1:
Serial.write("Button 4 Page 0 Long \n");
// show tuner
MIDI.sendControlChange(68, 0, MIDI_Base);
break;
}
setBlink(neoButton4,2);
}
//FUNCTIONS FOR INTERNALS
//Set colour for page
void colourPage() {
Serial.write("Colour Page \n");
uint32_t colour;
if (page == 1) {
colour = pixels.Color(page1[0], page1[1], page1[2]);
pixels.fill(colour);
} else {
colour = pixels.Color(page0[0], page0[1], page0[2]);
pixels.fill(colour);
}
pixels.show();
}
//Set blink value in blinkState array
void setBlink(int n, int repeat) {
blinkState[n-1] = repeat * 2;
updateBlink();
}
//Use blinkState to update page
void updateBlink() {
bool blinkActive = false;
for (int i = 0; i < (sizeof(blinkState) / sizeof(blinkState[0])); i++){
//If off state
if (blinkState[i] == 0) {
continue;
}
//On State
else if (blinkState[i] % 2 == 0) {
blinkActive = true;
pixels.setPixelColor(i, pixels.Color(blinkColour[0], blinkColour[1], blinkColour[2]));
pixels.show();
}
//Blink is off but more to come (repeated)
else {
blinkActive = true;
colourPage();
}
//Decrease count
blinkState[i] = blinkState[i] -1;
}
//Restart timer if a blink left
if (blinkActive == true) {
blinkTime.start();
}
else {
blinkTime.stop();
}
}
//Flash BPM Led based on state
void updateBPM() {
//On page 0 only
if (page == 0){
if (blinkBPM_State == true) {
blinkBPM_State = false;
pixels.setPixelColor(bpmLED - 1, page0[0], page0[1], page0[2]);
}
else if (blinkBPM_State == false) {
blinkBPM_State = true;
pixels.setPixelColor(bpmLED - 1, bpmColour[0], bpmColour[1], bpmColour[2]);
}
pixels.show();
}
}
//Handle press of bpm switch
void pressBPM() {
long now = millis();
long diff = now - lastPress;
if (diff < 3000) {
bpmTime.setdelay(diff/2);
bpmTime.start();
}
lastPress = now;
}
//End bpm timer and update page
void stopBPM(){
bpmTime.stop();
colourPage();
}