-
Notifications
You must be signed in to change notification settings - Fork 5
/
arkade_warrior_jun05c.ino
539 lines (476 loc) · 13.2 KB
/
arkade_warrior_jun05c.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//teensy++ code for arcade warrior
//tomash ghz
//www.tomashg.com
#include <Bounce.h>
//debugging will print out in serial instead of midi
boolean debugging=false;
boolean traktorrr=false;
//channel number for midi messages
int midiChannel=3;
//pin numbers for elements
int leds[3]= {
20, 21, 22
};
int buttons[16]= {
5, 9, 13, 17, //first row
4, 8, 12, 16,
1, 7, 11, 15,
0, 6, 10, 14
};
int knobs[4]= {
A2, A3, A0, A1
};
int faders[3]= {
A7, A6, A5
};
//bank
int bank=0;
// pins for joystick buttons
int joystic[4]= {
24, 26, 25, 23
};
// variables to store values
Bounce *buttonState[16];
int knobVal[4];
int faderVal[3];
boolean joysticState[4];
int joysticVal[4]; // save the joystic cc values
boolean ledState[3];
Bounce jUp = Bounce( joystic[0], 10 );
Bounce jDown = Bounce( joystic[1], 10 );
Bounce jLeft = Bounce( joystic[2], 10 );
Bounce jRight = Bounce( joystic[3], 10 );
int comboState=0; //state of the current combo
int temp;
//initialize values and set modes
void setup() {
//DEBUG
if (debugging) {
Serial.begin(9600);//open serail port
}
else {
Serial.begin(31250);//open midi
usbMIDI.setHandleNoteOn(myNoteOn);
usbMIDI.setHandleNoteOff(myNoteOff);
}
//arcade buttons
for (int i=0;i<16;i++) {
pinMode(buttons[i], INPUT_PULLUP);
buttonState[i]= new Bounce(buttons[i], 5);
}
pinMode(6, OUTPUT); //hope this doesnt fry the led
//joystick buttons
for (int i=0;i<4;i++) {
pinMode(joystic[i], INPUT_PULLUP);
joysticVal[i]=0;
}
// LEDS
for (int i=0;i<3;i++) {
pinMode(leds[i], OUTPUT);
digitalWrite(leds[i], HIGH);
delay(50);
digitalWrite(leds[i], LOW);
}
//switch to traktor mode
// to switch hold the lower left and right buttons while the device is booting
for (int i=0;i<10;i++) {
digitalWrite(leds[0], HIGH);
delay(100);
digitalWrite(leds[0], LOW);
delay(100);
if ((buttonState[0]->read()==LOW)&&(buttonState[3]->read()==LOW)) {
traktorrr=true;
//flash leds to indicate the mode change
for (int i=0;i<10;i++) {
digitalWrite(leds[0], HIGH);
digitalWrite(leds[1], HIGH);
digitalWrite(leds[2], HIGH);
delay(100);
digitalWrite(leds[0], LOW);
digitalWrite(leds[1], LOW);
digitalWrite(leds[2], LOW);
delay(100);
}
break;
}
}
}
void loop() {
//read buttons
for (int i=0;i<16;i++) {
if (buttonState[i]->update()) {//state changed
if (buttonState[i]->read()==LOW) {//is pressed
midiNoteOnOff(true, i+36+bank*16);
}
else {
midiNoteOnOff(false, i+36+bank*16);
}
}
}//end loop
//read knobs and faders
for (int i=0;i<4;i++) {
temp=map(analogRead(knobs[i]), 0, 1023, 0, 127);
if (temp!=knobVal[i]) { //value changed
midiCC(temp, knobVal[i], i*2+16+bank*17);
}
knobVal[i]=temp;
}// end loop
for (int i=0;i<3;i++) {
temp=map(analogRead(faders[i]), 0, 1023, 0, 127);
if (temp!=faderVal[i]) { //value changed
midiCC(temp, faderVal[i], i*2+24+bank*17);
}
faderVal[i]=temp;
}// end loop
// read joystic guestures
readJoystic();
// update leds depending on bank
switch(bank) {
case 0:
ledState[0]=false;
ledState[1]=false;
ledState[2]=false;
break;
case 1:
ledState[0]=true;
ledState[1]=false;
ledState[2]=false;
break;
case 2:
ledState[0]=true;
ledState[1]=true;
ledState[2]=false;
break;
case 3:
ledState[0]=true;
ledState[1]=true;
ledState[2]=true;
break;
}
// update leds
for (int i=0;i<3;i++) {
digitalWrite(leds[i], ledState[i]);
}
//recieve MIDI messages
if (!debugging) {
usbMIDI.read();
}
}
// function to handle noteon outgoing messages
void midiNoteOnOff(boolean s, int n) {
//check if the key pressed is part of any secret combo
if (traktorrr) {
checkCombo(s);
}
if (s) {
if (debugging) {//debbuging enabled
Serial.print("Button ");
Serial.print(n);
Serial.println(" pressed.");
}
else {
usbMIDI.sendNoteOn(n, 127, midiChannel);
}
}
else {
if (debugging) {//debbuging enabled
Serial.print("Button ");
Serial.print(n);
Serial.println(" released.");
}
else {
usbMIDI.sendNoteOff(n, 0, midiChannel);
}
}
}
//function to check for secret combos
void checkCombo(boolean s) {
// from MIDI Fighter Pro documentation:
// COMBOS
//
// A B C D E
// +--------+ +--------+ +--------+ +--------+ +--------+
// | | | | | | | | | |
// | | | | | 3 4 | | | |u |
// | | |n n n n | | 1 2 | |a b c d | |l r A B |
// |1 2 3 4 | | | | | | | |d |
// +--------+ +--------+ +--------+ +--------+ +--------+
// a-b-c-c-d uuddlrlrBA
//
// Combos retain a NoteOn while the final key is depressed and emit a NoteUp
// when it is released.
// Combo A G#-2
// Combo B A-2
// Combo C A#-2
// Combo D B-2
// Combo E C-1
// lets simplify things a bit :)
// A B C D E
// +--------+ +--------+ +--------+ +--------+ +--------+
// | | | | | | | | | |
// | | | | | x x | | | |x |
// | | |x x x x | | x x | | x x x | | x x |
// |x x x x | | | | | | | |x |
// +--------+ +--------+ +--------+ +--------+ +--------+
//no combo active and a button is pressed
if ((comboState==0)&&(s)) {
// combo A
if ((buttonState[0]->read()==LOW)
&&(buttonState[1]->read()==LOW)
&&(buttonState[2]->read()==LOW)
&&(buttonState[3]->read()==LOW)) {
comboState=1;
usbMIDI.sendNoteOn(8, 127, midiChannel);
}
else
//combo B
if ((buttonState[4]->read()==LOW)
&&(buttonState[5]->read()==LOW)
&&(buttonState[6]->read()==LOW)
&&(buttonState[7]->read()==LOW)) {
comboState=2;
usbMIDI.sendNoteOn(9, 127, midiChannel);
}
else
//combo C
if ((buttonState[5]->read()==LOW)
&&(buttonState[6]->read()==LOW)
&&(buttonState[9]->read()==LOW)
&&(buttonState[10]->read()==LOW)) {
comboState=3;
usbMIDI.sendNoteOn(10, 127, midiChannel);
}
else
//combo D
if ((buttonState[5]->read()==LOW)
&&(buttonState[6]->read()==LOW)
&&(buttonState[7]->read()==LOW)) {
comboState=4;
usbMIDI.sendNoteOn(11, 127, midiChannel);
}
else
//combo E
if ((buttonState[0]->read()==LOW)
&&(buttonState[8]->read()==LOW)
&&(buttonState[6]->read()==LOW)
&&(buttonState[7]->read()==LOW)) {
comboState=5;
usbMIDI.sendNoteOn(12, 127, midiChannel);
}
}
else if ((comboState!=0)&&(!s)) {//combo was active and released
switch(comboState) {
case 1:
usbMIDI.sendNoteOff(8, 0, midiChannel);
break;
case 2:
usbMIDI.sendNoteOff(9, 0, midiChannel);
break;
case 3:
usbMIDI.sendNoteOff(10, 0, midiChannel);
break;
case 4:
usbMIDI.sendNoteOff(11, 0, midiChannel);
break;
case 5:
usbMIDI.sendNoteOff(12, 0, midiChannel);
break;
}
comboState=0;
}
}
// function to handle cc outgoing messages
void midiCC(int v, int oldv, int n) {
if (debugging) {//debbuging enabled
Serial.print("Potentiometer ");
Serial.print(n);
Serial.print(" changed value to ");
Serial.println(v);
}
else {
usbMIDI.sendControlChange(n, v, midiChannel);
//send the advanced traktor midi messages
// 0 3 64 124 127
// |--|-------------|-------------|--| - full range
//
// |0=======================127| - CC A
// |0=========105| - CC B
//
// |__|on____________________________| - note A
// |off___________________________|on| - note B
// 3 124
if (traktorrr) {
if ((v>3)&&(oldv<=3)) { // send the 0 value note on
usbMIDI.sendNoteOn(n+84, 127, midiChannel);
}
else if ((v<=3)&&(oldv>3)) {
usbMIDI.sendNoteOff(n+84, 127, midiChannel);
}
if ((v>124)&&(oldv<=124)) { // send the 127 value note on
usbMIDI.sendNoteOn(n+85, 127, midiChannel);
}
else if ((v<=124)&&(oldv>124)) {
usbMIDI.sendNoteOff(n+85, 127, midiChannel);
}
if (v<=64) //send secondary cc
usbMIDI.sendControlChange(n+1, map(v, 0, 64, 0, 105), midiChannel);
}
}
}
void readJoystic() {
//up joystick
if ( jUp.update() ) {//state changed
if ( jUp.read() != HIGH) {// it is pressed
if ( joysticState[0] == LOW ) {//last state was low
joysticState[0] = HIGH;
midiNoteOnOff(true, 4);
}
}
else {// it was released
if ( joysticState[0] == HIGH ) {//last state was low
joysticState[0] = LOW;
joysticVal[0]=0;
if (debugging) {
Serial.println(joysticVal[0]);
}
else {
usbMIDI.sendControlChange(30, joysticVal[0], midiChannel);
}
//midiNoteOnOff(true,4);
midiNoteOnOff(false, 4);
}
}
}
else {//state didnot change
if ( jUp.read() != HIGH) {//is held on
if (jUp.duration()>500) {// was held longer than half sec
if (jUp.duration()%100>90) {//increment the value
joysticVal[0]++;
joysticVal[0]=constrain(joysticVal[0], 0, 127);
if (debugging) {
Serial.println(joysticVal[0]);
}
else {
usbMIDI.sendControlChange(30, joysticVal[0], midiChannel);
}
}
}
}
}
//down joystick
if ( jDown.update() ) {//state changed
if ( jDown.read() != HIGH) {// it is pressed
if ( joysticState[1] == LOW ) {//last state was low
joysticState[1] = HIGH;
midiNoteOnOff(true, 5);
}
}
else {// it was released
if ( joysticState[1] == HIGH ) {//last state was low
joysticState[1] = LOW;
joysticVal[1]=0;
if (debugging) {
Serial.println(joysticVal[1]);
}
else {
usbMIDI.sendControlChange(31, joysticVal[1], midiChannel);
}
//midiNoteOnOff(true,5);
midiNoteOnOff(false, 5);
}
}
}
else {//state didnot change
if ( jDown.read() != HIGH) {//is held on
if (jDown.duration()>500) {// was held longer than half sec
if (jDown.duration()%100>95) {//increment the value
joysticVal[1]++;
joysticVal[1]=constrain(joysticVal[1], 0, 127);
if (debugging) {
Serial.println(joysticVal[1]);
}
else {
usbMIDI.sendControlChange(31, joysticVal[1], midiChannel);
}
}
}
}
}
//left joystick
if ( !jLeft.update() ) {//state didnot change
if ( jLeft.read() != HIGH) {//is held on
if (traktorrr) {
if (jLeft.duration()%100>85)//increment the value
usbMIDI.sendControlChange(32, 63, midiChannel); //act as rotary encoder
}
if ( joysticState[2] == LOW ) {//last state was low
joysticState[2] = HIGH;
if (!traktorrr) { // decrease the bank number
if (bank==0)
bank=3;
else
bank=bank-1;
}
else
usbMIDI.sendNoteOn(0, 127, midiChannel);
}
}
else {// it was released
if ( joysticState[2] == HIGH ) {//last state was low
joysticState[2] = LOW;
usbMIDI.sendNoteOff(0, 127, midiChannel);
}
}
}
//right joystick
if ( !jRight.update() ) {//state didnot change
if ( jRight.read() != HIGH) {//is held on
if (traktorrr) {
if (jRight.duration()%100>85)//increment the value
usbMIDI.sendControlChange(32, 65, midiChannel); //act as rotary encoder
}
if ( joysticState[3] == LOW ) {//last state was low
joysticState[3] = HIGH;
if (!traktorrr) // increase the bank number
bank=(bank+1)%4;
else
usbMIDI.sendNoteOn(0, 127, midiChannel);
}
}
else {// it was released
if ( joysticState[3] == HIGH ) {//last state was low
joysticState[3] = LOW;
usbMIDI.sendNoteOff(0, 127, midiChannel);
}
}
}
}
//event handlers for recieved note ons
void myNoteOn(byte channel, byte note, byte velocity) {
if (channel==midiChannel) { //determine which led to turn on
if (note==(byte)0)
ledState[0]=true;
if (note==(byte)1)
ledState[1]=true;
if (note==(byte)2)
ledState[2]=true;
}
if ((channel==midiChannel)&&(velocity==0)) { //traktor note off
if (note==(byte)0)
ledState[0]=false;
if (note==(byte)1)
ledState[1]=false;
if (note==(byte)2)
ledState[2]=false;
}
}
void myNoteOff(byte channel, byte note, byte velocity) {
if (channel==midiChannel) {
if (note==(byte)0)
ledState[0]=false;
if (note==(byte)1)
ledState[1]=false;
if (note==(byte)2)
ledState[2]=false;
}
}