-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIDIsonar.ino
1351 lines (1168 loc) · 60.6 KB
/
MIDIsonar.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* MIDIsonar */
/* ---------------------------------------------------------------------------- */
/* Author: Paul Goes */
/* Version: 3.1 */
/* Date: 19-03-2024 */
/* ---------------------------------------------------------------------------- */
/* Revision history: */
/* */
/* v0.1 : Initial version. Only contains simple LCD functionality that */
/* enables to scroll through the different modes: STANDBY, SETUP and */
/* PLAY. */
/* */
/* v0.2 : Added data array that holds the setup values. Initialized the */
/* data array with the default values. Implemented temporary PLAY */
/* mode functionality that shows the contents of the array on the */
/* LCD display. */
/* */
/* v0.3 : Added function that converts integer value to string in order to */
/* display meaningful values on the LCD. */
/* */
/* v0.4 : Added setup mode. Navigation through the six setup screens. Setup */
/* screens display the initalizaton data from the data array. */
/* Changing the data values is not yet implemented. */
/* */
/* v0.5 : Added the functionality to change values in setup mode. Added */
/* extensive in-code comments to document the logic. */
/* */
/* v0.6 : Solved bug #1 'LDST and HDST displays wrong values when LDST>HDST */
/* and HDST<LDST' and solved bug #2 'Jitter at certain positions of */
/* the value dial'. */
/* */
/* v0.7 : Implemented play mode. Implemented is only sonar controller A. */
/* For this controller the complete functionality is implemented. */
/* Both controller types (MIDI notes and MIDI CC) are implemented. */
/* */
/* v0.8 : Solved bug #3 'Alternating adjacent notes when playing notes' and */
/* solved bug 'No NoteOff Midi message sent when hand is out of */
/* range'. */
/* */
/* v0.9 : Implemented play mode for sonar controller B. */
/* */
/* v1.0 : Implemented setting values by two new buttons for INC and DEC. */
/* The value potentiometer is removed and no longer functional. */
/* */
/* v1.1 : Switched pins for sonar controller A and sonar controller B to */
/* fix the build error in which the controllers are build into the */
/* wrong physical slots. */
/* */
/* v2.0 : Changed implementation of the MIDI notes functionality. It is now */
/* possible to select a scale to use when playing the notes. The */
/* scale is played starting from a specified root note which */
/* determines the key that the scale is played in. */
/* */
/* v2.1 : Added implementation of the MIDI chords functionality. It is now */
/* possible to select a progression to use when playing the chords. */
/* The progression is played starting from a specified root note */
/* which determines the key the progression is played in. */
/* */
/* v2.2 : Added implementation of additional MIDI chord progressions. */
/* */
/* v2.3 : Added implementation of the MIDI pitchbend functionality. It is */
/* possible to select a whether bending up only, bending down only */
/* or bending up and down. In all three settings there is a neutral */
/* zone of configurable size that corresponds to the central pitch. */
/* */
/* v2.4 : Enhanced menu handling. When returning to setup it returns to the */
/* previously selected menu item. When selecting NEXT at the last */
/* menu item it selects the first menu item and when selecting PREV */
/* at the first menu item it selects the last menu item. */
/* */
/* v2.5 : Solved issue with the MIDI chord progressions. The new chord was */
/* played with the wrong chord type and the note off was played with */
/* the correct chord type resulting in notes sustaining. */
/* */
/* v3.0 : Added MIDI reset when leaving play mode. Notes that are sounding */
/* are released and the pitch bend is reset to neutral position. */
/* */
/* v3.1 : Enhanced playing of note scales and chord progressions. In */
/* previous versions it played one note/chord outside of the range. */
/* With a range of 1 it played 2 notes/chords, with the range set */
/* to maximum it tried to play a note outside of the arrays. */
/* With range of a single note it is now possible to trigger single */
/* notes/chords with more precision. */
/* ---------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------- */
/* Arduino Pin connections: */
/* TXD - MIDI OUT Serial D10 - Sonar B Led */
/* D2 - LCD Bit 7 (pin 14) D11 - LCD Clock (pin 6) */
/* D3 - LCD Bit 6 (pin 13) D12 - LCD Reg Select (pin 4) */
/* D4 - LCD Bit 5 (pin 12) D13 - Sonar A Led */
/* D5 - LCD Bit 4 (pin 11) A0 - Potmeter Value (deprecated) */
/* D8 - Sonar A Trigger A1 - Button MODE SELECT */
/* D9 - Sonar A Echo A2 - Button MENU PREV */
/* D6 - Sonar B Trigger A3 - Button MENU NEXT */
/* D7 - Sonar B Echo A4 - Button VALUE INC */
/* A5 - Button VALUE DEC */
/* ---------------------------------------------------------------------------- */
/* include the library code */
#include <LiquidCrystal.h>
#include <NewPing.h>
/* initialize the library with the numbers of the interface pins */
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
/* initialize the push buttons */
const int buttonPin1 = 15; /* button 1 connected to pin 15 (analog A1) MODE SELECT */
const int buttonPin2 = 16; /* button 2 connected to pin 16 (analog A2) MENU PREV */
const int buttonPin3 = 17; /* button 3 connected to pin 17 (analog A3) MENU NEXT */
const int buttonPin4 = 18; /* button 4 connected to pin 18 (analog A4) VALUE INC */
const int buttonPin5 = 19; /* button 5 connected to pin 19 (analog A5) VALUE DEC */
/* initialize the LEDs */
const int ledPinA = 13; /* activity LED used for controller A connected to pin D13 */
const int ledPinB = 10; /* activity LED used for controller B connected to pin D10 */
/* initialize the potentiometer */
const int potPin1 = 0; /* potentiometer 1 connected to pin 1 (analog A0) VALUE */
int buttonState = 0;
/* initialize the start menu position */
int menu_id = 0; /* menu id of the selected menu item */
int menu_id_old = 3; /* menu id of the previous menu item */
/* initialize the Sonars */
#define triggerPinA 8 /* trigger pin sonar sensor A connected to pin D8 */
#define echoPinA 9 /* echo pin sonar sensor A connected to pin D9 */
#define triggerPinB 6 /* trigger pin sonar sensor B connected to pin D6 */
#define echoPinB 7 /* echo pin sonar sensor B connected to pin D7 */
NewPing sonarA(triggerPinA, echoPinA, 100); /* NewPing setup of sonar A pins and maximum distance */
NewPing sonarB(triggerPinB, echoPinB, 100); /* NewPing setup of sonar A pins and maximum distance */
/* initialize the data array with default values */
int value[2][18] = {
{ 1, 1, 1, 5, 50, 1, 7, 0, 127, 1, 60, 24, 1, 60, 14, 3, 3, 1 }, /* controller A: Active | CC 7 Volume over full range 0-127 */
{ 1, 2, 1, 5, 50, 1, 7, 0, 127, 1, 60, 24, 1, 60, 14, 3, 3, 1 } /* controller B: Active | NOTE Chromatic scale with root C3 with 24 steps */
};
/* initialize the data array with valuetypes */
int type[18] = { 2, 3, 1, 1, 1, 4, 1, 1, 1, 5, 6, 1, 7, 6, 1, 8, 9, 10 };
/* initialize the string array with menu names */
char* menu[] = { "ACTV", "TYPE", "CHNL", "LDST", "HDST", "POLR", "CNTR", "LVAL", "HVAL", "SCAL", "ROOT", "RANG", "PROG", "ROOT", "RANG", "TYPE", "NTRL", "PBFX" };
/* initialize the data array with ping medians */
int pingmedian[4] = { 2, 3, 3, 2 };
/* initalize the data array with wiggle room */
int wiggleroom[4] = { 10, 10, 10, 0 };
/* initialize the data array with scales */
int scales[12][24] = {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, /* Chromatic */
{ 0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23, 24, 26, 28, 29, 31, 33, 35, 36, 38, 40 }, /* Major */
{ 0, 2, 3, 5, 7, 8, 10, 12, 14, 15, 17, 19, 20, 22, 24, 26, 27, 29, 31, 32, 34, 36, 38, 39 }, /* Minor */
{ 0, 2, 3, 5, 7, 9, 11, 12, 14, 15, 17, 19, 21, 23, 24, 26, 27, 29, 31, 33, 35, 36, 38, 39 }, /* Melodic Minor */
{ 0, 2, 3, 5, 7, 8, 11, 12, 14, 15, 17, 19, 20, 23, 24, 26, 27, 29, 31, 32, 35, 36, 38, 39 }, /* Harmonic Minor */
{ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46 }, /* Whole Tone */
{ 0, 2, 3, 4, 7, 9, 12, 14, 15, 16, 19, 21, 24, 26, 27, 28, 31, 33, 36, 38, 39, 40, 43, 45 }, /* Major Blues */
{ 0, 3, 5, 6, 7, 10, 12, 15, 17, 18, 19, 22, 24, 27, 29, 30, 31, 34, 36, 39, 41, 42, 43, 46 }, /* Minor Blues */
{ 0, 2, 4, 7, 9, 12, 14, 16, 19, 21, 24, 26, 28, 31, 33, 36, 38, 40, 43, 45, 48, 50, 52, 55 }, /* Major Pentatonic */
{ 0, 3, 5, 7, 10, 12, 15, 17, 19, 22, 24, 27, 29, 31, 34, 36, 39, 41, 43, 46, 48, 51, 53, 55 }, /* Minor Pentatonic */
{ 0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, 23, 24, 26, 27, 29, 30, 32, 33, 35 }, /* Octatonic Whole Half */
{ 0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34 } /* Octatonic Half Whole */
};
/* initialize the data array with progressions */
int progs[9][14][2] = {
{ {0,2} , {2,3} , {4,3} , {5,2} , {7,2} , {9,3} , {11,5} , {12,2} , {14,3} , {16,3} , {17,2} , {19,2} , {21,3} , {23,5} }, /* Major */
{ {0,3} , {2,5} , {3,2} , {5,3} , {7,3} , {8,2} , {10,2} , {12,3} , {14,5} , {15,2} , {17,3} , {19,3} , {20,2} , {22,2} }, /* Minor */
{ {0,8} , {5,8} , {7,8} , {12,8} , {17,8} , {19,8} , {24,8} , {29,8} , {31,8} , {36,8} , {41,8} , {43,8} , {48,8} , {53,8} }, /* Major Blues 7th */
{ {0,10} , {5,10} , {7,10} , {12,10} , {17,10} , {19,10} , {24,10} , {29,10} , {31,10} , {36,10} , {41,10} , {43,10} , {48,10} , {53,10} } , /* Minor Blues 7th */
{ {0,2} , {5,2} , {7,2} , {9,3} , {12,2} , {17,2} , {19,2} , {21,3} , {24,2} , {29,2} , {31,2} , {33,3} , {36,2} , {41,2} } , /* Pop */
{ {0,9} , {2,10} , {7,8} , {12,9} , {14,10} , {19,8} , {24,9} , {26,10} , {31,8} , {36,9} , {38,10} , {43,8} , {48,9} , {50,10} } , /* Jazz */
{ {0,2} , {5,2} , {10,2} , {12,2} , {17,2} , {22,2} , {24,2} , {29,2} , {34,2} , {36,2} , {41,2} , {46,2} , {48,2} , {53,2} } , /* Modal */
{ {0,2} , {2,3} , {4,3} , {5,2} , {7,2} , {9,3} , {11,5} , {12,2} , {14,3} , {16,3} , {17,2} , {19,2} , {21,3} , {23,5} } , /* Gospel */
{ {0,2} , {4,3} , {5,3} , {7,3} , {9,2} , {12,2} , {16,3} , {17,3} , {19,3} , {21,2} , {24,2} , {28,3} , {29,3} , {31,3} } /* Pachelbel */
};
/* ---------------------------------------------------------------------------- */
/* Setup portion of the Arduino program. */
/* ---------------------------------------------------------------------------- */
void setup()
{
/* initialize serial MIDI OUT interface with the correct baud rate */
Serial.begin(31250);
/* set up the LCD's number of columns and rows */
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
/* set up the LCD screen as 2 lines of 16 characters */
lcd.begin(16, 2);
/* clear the LCD display */
lcd.clear();
/* display productname and version */
lcd.setCursor(0, 0);
lcd.print("MIDIsonar v3.1");
delay(1000);
/* display the welcome message */
for(int counter=0; counter<3; counter++)
{
lcd.setCursor(0,1); lcd.print("by Paul Goes");
delay(750);
lcd.setCursor(0,1); lcd.print(" ");
delay(750);
}
lcd.clear();
}
/* ---------------------------------------------------------------------------- */
/* Loop portion of the Arduino program. */
/* ---------------------------------------------------------------------------- */
void loop()
{
/* cycle through the three modes */
MODEstandby();
MODEsetup();
MODEplay();
}
/* ---------------------------------------------------------------------------- */
/* MODEstandby(): */
/* Function: Implements the standby mode. */
/* Arguments: None. */
/* ---------------------------------------------------------------------------- */
void MODEstandby()
{
/* display the mode message */
lcd.clear();
lcd.setCursor(0,0); lcd.print("MIDIsonar");
lcd.setCursor(0,1); lcd.print("Standby");
delay(1000);
/* check if MODE SELECT button is pressed */
do
{
if(digitalRead(buttonPin1) == HIGH)
{
/* wait until MODE SELECT button is released */
do
delay(20);
while(digitalRead(buttonPin1) == HIGH);
return;
}
delay(20);
} while (true);
} /* End of MODEstandby */
/* ---------------------------------------------------------------------------- */
/* MODEsetup(): */
/* Function: Implements the setup mode. */
/* Arguments: None. */
/* ---------------------------------------------------------------------------- */
void MODEsetup()
{
/* display the mode message */
lcd.clear();
lcd.setCursor(0,0); lcd.print("MIDIsonar");
lcd.setCursor(0,1); lcd.print("Setup");
delay(1000);
lcd.clear();
/* initialize variables */
int screen_id; /* screen id of the selected menu item */
int screen_id_old; /* screen id of the previous menu item */
int controller_id; /* controller id of the selected menu item */
int array_pos; /* position of the selected menu item in the value array */
int init_potval; /* intial value read from the potentiometer at the selection of a new menu item [0-1023] */
int read_potval; /* current value read from the potentiometer [0-1023] */
int prev_potval; /* previous value read from the potentiometer [0-1023] */
int prev_val; /* previous value of the selected menu item as stored in the value array [type specific] */
int temp_val; /* temporary value for the selected menu item [type specific] */
int treshold; /* indicates whether value knob is turned past treshold (0=FALSE, 1=TRUE) */
bool buttonhold; /* indicates whether value button is hold down */
char* valstr = " "; /* temporary string used to construct formatted string values */
do
{
/* determine screen id's of the selected menu item and the previous menu item */
screen_id = menu_id/3;
screen_id_old = menu_id_old/3;
/* determine controller id and array position of the selected menu item */
controller_id = menu_id/9;
array_pos = menu_id%9;
/* array positions of menu-items [6-8] depend on selected TYPE [if TYPE=1 increase three positions] */
if(array_pos>5) array_pos=array_pos+3*(value[controller_id][1]-1);
/* redraw the complete screen if the selected menu item is on a new setup screen */
/*if(screen_id != screen_id_old)*/
if(0==0)
{
lcd.noCursor();
lcd.clear();
lcd.setCursor(0,0);
if(controller_id==0) lcd.print("A:"); else lcd.print("B:");
for(int counter=0; counter<3; counter++)
{
lcd.setCursor(2+counter*5,0); lcd.print(menu[(array_pos/3)*3+counter]);
value2string(value[controller_id][(array_pos/3)*3+counter], valstr, type[(array_pos/3)*3+counter]);
lcd.setCursor(3+counter*5,1); lcd.print(valstr);
}
}
/* draw the cursor at the place of the selected menu item */
lcd.noCursor();
lcd.setCursor(5+(menu_id%3)*5,1); lcd.cursor();
/* read the initial value for the selected menu item */
init_potval = analogRead(potPin1);
prev_potval = init_potval;
/* read the stored value for the selected menu item from the value array */
prev_val = value[controller_id][array_pos];
/* reset the treshold */
treshold = 0;
/* check for setup actions */
do
{
/* check if MODE SELECT button is pressed */
if(digitalRead(buttonPin1) == HIGH)
{
/* wait until MODE SELECT button is released */
do
delay(20);
while(digitalRead(buttonPin1) == HIGH);
lcd.noCursor();
return;
}
/* check if MENU PREV button is pressed */
if(digitalRead(buttonPin2) == HIGH)
{
/* decrease the menu_id */
menu_id_old=menu_id;
menu_id--;
if(menu_id==-1) menu_id=17;
/* wait until MENU PREV button is released */
do
delay(20);
while(digitalRead(buttonPin2) == HIGH);
break;
}
/* check if MENU NEXT button is pressed */
if(digitalRead(buttonPin3) == HIGH)
{
/* increase the menu_id */
menu_id_old=menu_id;
menu_id++;
if(menu_id==18) menu_id=0;
/* wait until MENU NEXT button is released */
do
delay(20);
while(digitalRead(buttonPin3) == HIGH);
break;
}
/* check if VALUE INC button is pressed */
if(digitalRead(buttonPin4) == HIGH)
{
/* reset the button hold indicator */
buttonhold=false;
/* wait a little to prevent jitter */
delay(20);
/* check if VALUE INC button is still pressed */
while(digitalRead(buttonPin4) == HIGH)
{
/* get value from the value array */
temp_val = value[controller_id][array_pos];
/* increase the value value */
switch(array_pos)
{
/* ACTV */
case 0: temp_val = temp_val + 1;
if(temp_val == 2) temp_val = 0; /* wrap around */
break;
/* TYPE */
case 1: temp_val = temp_val + 1;
if(temp_val == 5) temp_val = 1; /* wrap around */
break;
/* CHNL */
case 2: temp_val = temp_val + 1;
if(temp_val == 17) temp_val = 1; /* wrap around */
break;
/* LDST */
case 3: temp_val = temp_val + 5;
if(temp_val==value[controller_id][array_pos+1]) temp_val=value[controller_id][array_pos+1]-5; /* value of LDST must be lower then value of HDST */
break;
/* HDST */
case 4: temp_val = temp_val + 5;
if(temp_val == 85) temp_val = 80; /* no wrap around */
break;
/* POLR */
case 5: temp_val = temp_val + 1;
if(temp_val == 2) temp_val = 0; /* wrap around */
break;
/* CTRL NUMBER */
case 6: temp_val = temp_val + 1;
if(temp_val == 128) temp_val = 0; /* wrap around */
break;
/* CTRL LVAL */
case 7: temp_val = temp_val + 1;
if(temp_val==value[controller_id][array_pos+1]) temp_val=value[controller_id][array_pos+1]-1; /* value of LVAL must be lower then value of HVAL */
break;
/* CTRL HVAL */
case 8: temp_val = temp_val + 1;
if(temp_val == 128) temp_val = 127; /* no wrap around */
break;
/* NOTE SCALE */
case 9: temp_val = temp_val + 1;
if(temp_val == 13) temp_val = 1; /* wrap around */
break;
/* NOTE ROOT */
case 10: temp_val = temp_val + 1;
if(temp_val == 120) temp_val = 119; /* no wrap around */
break;
/* NOTE RANG */
case 11: temp_val = temp_val + 1;
if(temp_val == 25) temp_val = 24; /* no wrap around */
break;
/* CHORD PROGRESSION */
case 12: temp_val = temp_val + 1;
if(temp_val == 10) temp_val = 1; /* wrap around */
break;
/* CHORD ROOT */
case 13: temp_val = temp_val + 1;
if(temp_val == 120) temp_val = 119; /* no wrap around */
break;
/* NOTE RANG */
case 14: temp_val = temp_val + 1;
if(temp_val == 15) temp_val = 14; /* no wrap around */
break;
/* PBND TYPE */
case 15: temp_val = temp_val + 1;
if(temp_val == 4) temp_val = 1; /* wrap around */
break;
/* PBND NTRL */
case 16: temp_val = temp_val + 1;
if(temp_val == 4) temp_val = 1; /* wrap around */
break;
/* PBND PBFX */
case 17: temp_val = temp_val + 1;
if(temp_val == 3) temp_val = 1; /* wrap around */
break;
}
/* store the changed value in the value array */
value[controller_id][array_pos] = temp_val;
/* display the changed value and redraw cursor */
value2string(value[controller_id][array_pos], valstr, type[array_pos]);
lcd.setCursor(3+(menu_id%3)*5,1); lcd.print(valstr);
lcd.setCursor(5+(menu_id%3)*5,1); lcd.cursor();
/* check buttonhold and wait appropriate time for next button check */
if(buttonhold == false)
{
delay(300);
buttonhold=true;
}
else
{
delay(70);
}
}
}
/* check if VALUE DEC button is pressed */
if(digitalRead(buttonPin5) == HIGH)
{
/* reset the button hold indicator */
buttonhold=false;
/* wait a little to prevent jitter */
delay(20);
/* check if VALUE DEC button is still pressed */
while(digitalRead(buttonPin5) == HIGH)
{
/* get value from the value array */
temp_val = value[controller_id][array_pos];
/* increase the value value */
switch(array_pos)
{
/* ACTV */
case 0: temp_val = temp_val - 1;
if(temp_val == -1) temp_val = 1; /* wrap around */
break;
/* TYPE */
case 1: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 4; /* wrap around */
break;
/* CHNL */
case 2: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 16; /* wrap around */
break;
/* LDST */
case 3: temp_val = temp_val - 5;
if(temp_val == 0) temp_val = 5; /* no wrap around */
break;
/* HDST */
case 4: temp_val = temp_val - 5;
if(temp_val==value[controller_id][array_pos-1]) temp_val=value[controller_id][array_pos-1]+5; /* value of HDST must be higher then value of LDST */
break;
/* POLR */
case 5: temp_val = temp_val - 1;
if(temp_val == -1) temp_val = 1; /* wrap around */
break;
/* CTRL NUMBER */
case 6: temp_val = temp_val - 1;
if(temp_val == -1) temp_val = 127; /* wrap around */
break;
/* CTRL LVAL */
case 7: temp_val = temp_val - 1;
if(temp_val == -1) temp_val = 0; /* no wrap around */
break;
/* CTRL HVAL */
case 8: temp_val = temp_val - 1;
if(temp_val==value[controller_id][array_pos-1]) temp_val=value[controller_id][array_pos-1]+1; /* value of HVAL must be higher then value of LVAL */
break;
/* NOTE SCALE */
case 9: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 12; /* wrap around */
break;
/* NOTE ROOT */
case 10: temp_val = temp_val - 1;
if(temp_val == 11) temp_val = 12; /* no wrap around */
break;
/* NOTE RANG */
case 11: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 1; /* no wrap around */
break;
/* CHORD PROGRESSION */
case 12: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 9; /* wrap around */
break;
/* CHORD ROOT */
case 13: temp_val = temp_val - 1;
if(temp_val == 11) temp_val = 12; /* no wrap around */
break;
/* CHORD RANG */
case 14: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 1; /* no wrap around */
break;
/* PBND TYPE */
case 15: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 3; /* wrap around */
break;
/* PBND NTRL */
case 16: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 3; /* wrap around */
break;
/* PBND PBFX */
case 17: temp_val = temp_val - 1;
if(temp_val == 0) temp_val = 2; /* wrap around */
break;
}
/* store the changed value in the value array */
value[controller_id][array_pos] = temp_val;
/* display the changed value and redraw cursor */
value2string(value[controller_id][array_pos], valstr, type[array_pos]);
lcd.setCursor(3+(menu_id%3)*5,1); lcd.print(valstr);
lcd.setCursor(5+(menu_id%3)*5,1); lcd.cursor();
/* check buttonhold and wait appropriate time for next button check */
if(buttonhold == false)
{
delay(300);
buttonhold=true;
}
else
{
delay(70);
}
}
}
} while (true);
} while (true);
} /* End of MODEsetup */
/* ---------------------------------------------------------------------------- */
/* MODEplay(): */
/* Function: Implements the play mode. */
/* Arguments: None. */
/* ---------------------------------------------------------------------------- */
void MODEplay()
{
/* display the mode message */
lcd.clear();
lcd.setCursor(0,0); lcd.print("MIDIsonar");
lcd.setCursor(0,1); lcd.print("Play");
delay(1000);
lcd.clear();
/* initialize variables */
int pingTimeOldA = 0; /* previous ping time for controller A */
int lowRangeA; /* low range ping controller A in microseconds */
int highRangeA; /* high range ping controller A in microseconds */
int rangePolA; /* polarity controller A */
int lowValA; /* low value for controller A */
int highValA; /* high value for controller A */
int newValA = 0; /* current value measured for controller A */
int oldValA = 999; /* previous value measured for controller A */
int noteStateA = 0; /* state of the notes when type=NOTE: 0=off, 1=on */
int newNoteA = 0; /* current note for controller A */
int oldNoteA = 0; /* previous note for controller A */
int newChordA = 0; /* current chord for controller A */
int oldChordA = 0; /* previous chord for controller A */
int pbValA = 0; /* calculated pitchbend value for controller A */
int pbRangeA = 0; /* calculated pitchbend range for controller A */
int pbDeltaA = 0; /* calculated pitchbend delta for controller A */
int pbDirA = 0; /* calculated pitchbend direction for controller A */
int pbStateA = 0; /* state of the pitchbend when type=PBND: 0=neutral, 1=bend */
int pingTimeOldB = 0; /* previous ping time for controller B */
int lowRangeB; /* low range ping controller B in microseconds */
int highRangeB; /* high range ping controller B in microseconds */
int rangePolB; /* polarity controller B */
int lowValB; /* low value for controller B */
int highValB; /* high value for controller B */
int newValB = 0; /* current value measured for controller B */
int oldValB = 999; /* previous value measured for controller B */
int noteStateB = 0; /* State of the notes when type=NOTE: 0=off, 1=on */
int newNoteB = 0; /* current note for controller B */
int oldNoteB = 0; /* previous note for controller B */
int newChordB = 0; /* current chord for controller B */
int oldChordB = 0; /* previous chord for controller B */
int pbValB = 0; /* calculated pitchbend value for controller B */
int pbRangeB = 0; /* calculated pitchbend range for controller B */
int pbDeltaB = 0; /* calculated pitchbend delta for controller B */
int pbDirB = 0; /* calculated pitchbend direction for controller B */
int pbStateB = 0; /* state of the pitchbend when type=PBND: 0=neutral, 1=bend */
int pingTime; /* measured ping time between trigger and echo */
int displayTime = 0; /* display update timer to avoid value flickering */
char* valstr = " "; /* temporary string used to construct formatted string values */
char* pbstr = " "; /* temporary string used to construct formatted string values */
/* display the static part of the mode play screen and switch on LED's */
for(int counter=0; counter<2; counter++)
{
/* display the label for the controller */
if(counter==0) {lcd.setCursor(0,counter); lcd.print("A:");} else {lcd.setCursor(0,counter); lcd.print("B:");}
/* determine whether the controller is active */
if(value[counter][0]==0)
{
/* controller is not active */
/* display OFF */
lcd.setCursor(2,counter); lcd.print("OFF");
}
else
{
/* controller is active */
/* switch on the controller activity LED */
if(counter==0) digitalWrite(ledPinA, HIGH); else digitalWrite(ledPinB, HIGH);
/* display the controller TYPE */
value2string(value[counter][1], valstr, type[1]);
lcd.setCursor(2,counter); lcd.print(valstr);
/* display the controller CTRL NUMBER, NOTE SCALE OR CHORD PROGRESSION */
if(value[counter][1]==1) value2string(value[counter][6], valstr, type[6]);
if(value[counter][1]==2) value2string(value[counter][9], valstr, type[9]);
if(value[counter][1]==3) value2string(value[counter][12], valstr, type[12]);
if(value[counter][1]==4) value2string(value[counter][15], valstr, type[15]);
lcd.setCursor(6,counter); lcd.print(valstr);
/* display the controller CHNL */
value2string(value[counter][2], valstr, type[2]);
lcd.setCursor(9,counter); lcd.print(valstr);
/* display three dashes as placeholder for the controller value */
lcd.setCursor(13, counter); lcd.print("---");
}
}
/* determine lowrange and highrange as pingtimes in microseconds */
lowRangeA = value[0][3]*58; highRangeA = value[0][4]*58;
lowRangeB = value[1][3]*58; highRangeB = value[1][4]*58;
/* determine the polarity */
rangePolA = value[0][5];
rangePolB = value[1][5];
/* determine lowval and highval depending on TYPE */
if(value[0][1]==1) {lowValA=value[0][7]; highValA=value[0][8];} /* controller A - CTRL */
if(value[0][1]==2) {lowValA=0; highValA=value[0][11];} /* controller A - NOTE */
if(value[0][1]==3) {lowValA=0; highValA=value[0][14];} /* controller A - CHRD */
if(value[0][1]==4 && value[0][15]==1) {lowValA=0; highValA=10000+value[0][16]*819;} /* controller A - PBND UP */
if(value[0][1]==4 && value[0][15]==2) {lowValA=0; highValA=10000+value[0][16]*819;} /* controller A - PBND DWN */
if(value[0][1]==4 && value[0][15]==3) {lowValA=0; highValA=20000+value[0][16]*819;} /* controller A - PBND UP&DWN */
if(value[1][1]==1) {lowValB=value[1][7]; highValB=value[1][8];} /* controller B - CTRL */
if(value[1][1]==2) {lowValB=0; highValB=value[1][11];} /* controller B - NOTE */
if(value[1][1]==3) {lowValB=0; highValB=value[1][14];} /* controller B - CTRL */
if(value[1][1]==4 && value[1][15]==1) {lowValB=0; highValB=10000+value[0][16]*819;} /* controller B - PBND UP */
if(value[1][1]==4 && value[1][15]==2) {lowValB=0; highValB=10000+value[0][16]*819;} /* controller B - PBND DWN */
if(value[1][1]==4 && value[1][15]==3) {lowValB=0; highValB=20000+value[0][16]*819;} /* controller B - PBND UP&DWN */
do
{
/* if active, process controller A */
if(value[0][0]==1)
{
/* send ping and get ping time in microseconds based on configured average depending on the TYPE */
pingTime = sonarA.ping_median(pingmedian[value[0][1]-1]);
/* process the results when pingTime is within range */
if (pingTime>lowRangeA && pingTime<highRangeA)
{
/* determine new value */
if(value[0][1]==4) /* TYPE = PBND */
{
newValA = int ( (pingTime-lowRangeA) * ( (highValA-lowValA+1) / (highRangeA-lowRangeA) ) );
}
else /* TYPE = CNTRL or NOTE or CHORD */
{
newValA = int ( (pingTime-lowRangeA) / ( (highRangeA-lowRangeA) / (highValA-lowValA+1) ) );
}
/* adjust value for polarity */
if (rangePolA == 1) newValA = lowValA + newValA; else newValA = highValA - newValA;
/* keep value within range in case of rounding errors */
if (newValA<lowValA) newValA=lowValA; if (newValA>highValA) newValA=highValA;
/* adjust value to array index */
if((value[0][1]==2) || (value[0][1]==3)) if(newValA==highValA) newValA--;
/* send MIDI controller message if value has changed */
if ( (newValA!=oldValA) && (abs(pingTime-pingTimeOldA) > wiggleroom[value[0][1]-1] ) )
{
/* set previous value of the pingTime */
pingTimeOldA = pingTime;
/* send CC message if TYPE is CTRL */
if(value[0][1]==1)
{
/* CC: statusbyte=144+channel, databyte1=controller, databyte2=value */
MIDImessage(176+value[0][2]-1, value[0][6], newValA);
/* display value on the LCD screen */
if(displayTime==0)
{
value2string(newValA, valstr, 1);
lcd.setCursor(13,0); lcd.print(valstr);
}
}
/* send Note messages if TYPE is NOTE */
if(value[0][1]==2)
{
/* Send noteOff message for the previous note */
if(oldValA != 999)
{
oldNoteA=value[0][10]+scales[value[0][9]-1][oldValA];
MIDIchord(value[0][2], 0, oldNoteA, 1);
}
/* Send noteOn messages for the new chord */
newNoteA=value[0][10]+scales[value[0][9]-1][newValA];
MIDIchord(value[0][2], 1, newNoteA, 1);
/* Switch noteState to On: notes are sounding */
noteStateA = 1;
/* display value on the LCD screen */
value2string(newNoteA, valstr, 6);
lcd.setCursor(13,0); lcd.print(valstr);
}
/* send Note messages if TYPE is CHORD */
if(value[0][1]==3)
{
if(oldValA != 999)
{
/* Send noteOff message for the previous chord */
oldNoteA=value[0][13]+progs[value[0][12]-1][oldValA][0];
oldChordA=progs[value[0][12]-1][oldValA][1];
MIDIchord(value[0][2], 0, oldNoteA, oldChordA);
}
/* Send noteOn messages for the new chord */
newNoteA=value[0][13]+progs[value[0][12]-1][newValA][0];
newChordA=progs[value[0][12]-1][newValA][1];
MIDIchord(value[0][2], 1, newNoteA, newChordA);
/* Switch noteState to On: notes are sounding */
noteStateA = 1;
/* display value on the LCD screen */
value2string(newNoteA, valstr, 6);
lcd.setCursor(13,0); lcd.print(valstr);
}
/* send Pitch Bend messages if TYPE is PBND */
if(value[0][1]==4)
{
/* adjust for neutral zones */
if(value[0][15]==1) /* pitchbend UP */
{
pbRangeA = 819 * value[0][16]; pbDirA = 1;
if(newValA <= pbRangeA) pbDeltaA = 0; else pbDeltaA = newValA - pbRangeA;
}
if(value[0][15]==2) /* pitchbend DOWN */
{
pbRangeA = 819 * value[0][16]; pbDirA = -1;
if(newValA <= pbRangeA) pbDeltaA = 0; else pbDeltaA = newValA - pbRangeA;
}
if(value[0][15]==3) /* pitchbend UP&DOWN */
{
pbRangeA = 819 * value[0][16];
if((newValA >= 10900-pbRangeA) && (newValA <= 10900+pbRangeA)) {pbDeltaA = 0; pbDirA=1;}
if(newValA < 10900-pbRangeA) {pbDeltaA = 10900-pbRangeA-newValA; pbDirA=-1;}
if(newValA > 10900+pbRangeA) {pbDeltaA = newValA-pbRangeA-10900; pbDirA=1;}
}
/* process pitchbend effects */
if(value[0][17] == 2) /* pitchbend in discrete steps */
{
pbDeltaA = (pbDeltaA / 256) * 256;
}
/* finalize pitchbend value */
pbValA = 8192 + pbDirA * pbDeltaA;
if(pbValA < 0) pbValA = 0;
if(pbValA > 16383) pbValA = 16383;
/* Send PB message */
MIDIpitchbend(value[0][2], pbValA);
/* Set pbState to 0 if neutral and 1 if bend */
if(pbValA == 8192) pbStateA=0; else pbStateA=1;
/* display value on the LCD screen */
if(pbValA<2730) pbstr = "---";
else if(pbValA<5460) pbstr = " --";
else if(pbValA<8192) pbstr = " -";
else if(pbValA>13650) pbstr = "+++";
else if(pbValA>10920) pbstr = "++ ";
else if(pbValA>8192) pbstr = "+ ";
else pbstr = "000";
lcd.setCursor(13,0); lcd.print(pbstr);
}
}
/* remember the current value for next cycle */
oldValA = newValA;
}
/* process the results when pingTime is out of range */
else
{
/* send Note Off message if TYPE is NOTE and notes are sounding */
if( (value[0][1]==2) && (noteStateA==1))
{
/* Send noteOff messages for the previous note */
oldNoteA=value[0][10]+scales[value[0][9]-1][oldValA];
MIDIchord(value[0][2], 0, oldNoteA, 1);
/* Switch noteState to Off and reset old values: notes are no longer sounding */
noteStateA = 0; oldValA = 999; pingTimeOldA = pingTime;
/* display value on the LCD screen */
lcd.setCursor(13, 0);
lcd.print("---");
}
/* send Note Off message if TYPE is CHORD and notes are sounding */
if((value[0][1]==3) && (noteStateA==1))
{
/* Send noteOff messages for the previous chord */
oldNoteA=value[0][13]+progs[value[0][12]-1][oldValA][0];
oldChordA=progs[value[0][12]-1][oldValA][1];
MIDIchord(value[0][2], 0, oldNoteA, oldChordA);
/* Switch noteState to Off and reset old values: notes are no longer sounding */
noteStateA = 0; oldValA = 999; pingTimeOldA = pingTime;
/* display value on the LCD screen */
lcd.setCursor(13, 0);
lcd.print("---");
}
}
}
/* if active, process controller B */
if(value[1][0]==1)
{
/* send ping and get ping time in microseconds based on configured average depending on the TYPE */
pingTime = sonarB.ping_median(pingmedian[value[1][1]-1]);
/* process the results when pingTime is within range */
if (pingTime>lowRangeB && pingTime<highRangeB)
{
/* determine new value */
if(value[1][1]==4) /* TYPE = PBND */
{
newValB = int ( (pingTime-lowRangeB) * ( (highValB-lowValB+1) / (highRangeB-lowRangeB) ) );
}
else /* TYPE = CNTRL or NOTE or CHORD */
{
newValB = int ( (pingTime-lowRangeB) / ( (highRangeB-lowRangeB) / (highValB-lowValB+1) ) );
}
/* adjust value for polarity */
if (rangePolB == 1) newValB = lowValB + newValB; else newValB = highValB - newValB;
/* keep value within range in case of rounding errors */
if (newValB<lowValB) newValB=lowValB; if (newValB>highValB) newValB=highValB;
/* adjust value to array index */
if((value[1][1]==2) || (value[1][1]==3)) if(newValB==highValB) newValB--;
/* send MIDI controller message if value has changed */
if ( (newValB!=oldValB) && (abs(pingTime-pingTimeOldB) > wiggleroom[value[1][1]-1] ) )
{
/* set previous value of the pingTime */
pingTimeOldB = pingTime;
/* send CC message if TYPE is CTRL */
if(value[1][1]==1)
{
/* CC: statusbyte=144+channel, databyte1=controller, databyte2=value */
MIDImessage(176+value[1][2]-1, value[1][6], newValB);
/* display value on the LCD screen */
if(displayTime==0)
{
value2string(newValB, valstr, 1);
lcd.setCursor(13,1); lcd.print(valstr);
}
}
/* send Note messages if TYPE is NOTE */
if(value[1][1]==2)
{
if(oldValB != 999)
{
/* Send noteOff messages for the previous note */
oldNoteB=value[1][10]+scales[value[1][9]-1][oldValB];
MIDIchord(value[1][2], 0, oldNoteB, 1);
}
/* Send noteOn messages for the new note */
newNoteB=value[1][10]+scales[value[1][9]-1][newValB];
MIDIchord(value[1][2], 1, newNoteB, 1);
/* Switch noteState to On: notes are sounding */
noteStateB = 1;
/* display value on the LCD screen */
value2string(newNoteB, valstr, 6);
lcd.setCursor(13,1); lcd.print(valstr);
}
/* send Note messages if TYPE is CHORD */
if(value[1][1]==3)
{
/* Send noteOff message for the previous chord */