-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpedals.ino
136 lines (117 loc) · 3.23 KB
/
pedals.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
#define DEBOUNCE 1500
struct key{
int pin;
int midiKey;
int debounce;
int keySent;
int value;
};
struct key keys[] = {
{ 19, 36, 0, 0, 0 }, // high C
{ 2, 24, 0, 0, 0 }, // C
{ 3, 25, 0, 0, 0 }, // Db
{ 4, 26, 0, 0, 0 }, // D
{ 5, 27, 0, 0, 0 }, // Eb
{ 6, 28, 0, 0, 0 }, // E
{ 7, 29, 0, 0, 0 }, // F
{ 8, 30, 0, 0, 0 }, // Gb
{ 9, 31, 0, 0, 0 }, // G
{ 10, 32, 0, 0, 0 }, // Ab
{ 11, 33, 0, 0, 0 }, // A
{ 12, 34, 0, 0, 0 }, // Bb
{ 13, 35, 0, 0, 0 }, // B
{ 0, 0, 0, 0, 0 } // end of list marker
};
struct octaveSwitch{
int pin;
int debounce;
int value;
char direction[4];
};
struct octaveSwitch octaveSwitches[] = {
{ 27, 0, 0, 'up' }, // up
{ 26, 0, 0, 'down' }, // down
{ 0, 0, 0, 'end' } // end of list marker
};
int octave = 0;
int keyOffset = 0;
int keyVelocity = 100;
int noteON = 144;
int noteOFF = 128;
void setup(){
//set octave switch pins
for(int i = 0; octaveSwitches[i].pin != 0; ++i){
pinMode(octaveSwitches[i].pin, INPUT_PULLUP);
}
//set pedal pins
for(int i = 0; keys[i].pin != 0; ++i){
pinMode(keys[i].pin, INPUT_PULLUP);
}
//start serial with midi baudrate 31250
Serial.begin(31250);
}
void sendMidi(byte cmd, byte data1, byte data2){
Serial.write(cmd);
Serial.write(data1);
Serial.write(data2);
}
void noteOn(int midiKey){
sendMidi(noteON, midiKey, keyVelocity);
}
void noteOff(int midiKey){
sendMidi(noteOFF, midiKey, 0);
}
//void getMidiKey(int key){
// return key + keyOffset + (octave * 12);
//}
void handleOctaveSwitchEvents(){
for(int i = 0; octaveSwitches[i].pin != 0; ++i){ //for all octave switches
octaveSwitches[i].value = digitalRead(octaveSwitches[i].pin);
if(octaveSwitches[i].debounce == 0){ //switch has been off
if(octaveSwitches[i].value == LOW){ //switch is on now
octaveSwitches[i].debounce = DEBOUNCE; //set debounce value
if(octaveSwitches[i].direction == 'up'){
++octave;
}
if(octaveSwitches[i].direction == 'down'){
--octave;
}
}
else{ //switch has been on
if(octaveSwitches[i].value == HIGH){ //key is off now
--octaveSwitches[i].debounce; //decrease the debounce
}
else{ //switch has not gone off
octaveSwitches[i].debounce = DEBOUNCE; //reset debounce
}
}
}
}
}
void handleKeyEvents(){
for(int i = 0; keys[i].pin != 0; ++i){ //for all keys
keys[i].value = digitalRead(keys[i].pin);
if(keys[i].debounce == 0){ //key has been off
if(keys[i].value == LOW){ //key is on now
int key = keys[i].midiKey + keyOffset + (octave * 12);
noteOn(key); //send note on
keys[i].keySent = key; //save what key is sent
keys[i].debounce = DEBOUNCE; //set debounce value
}
}
else{ //key has been on
if(keys[i].value == HIGH){ //key is off now
if(--keys[i].debounce == 0){ //if debounce counter is 0
noteOff(keys[i].keySent); //send note off, keySent is the correct one even if ovtave or offset have changed
}
}
else{ //key has not gone off
keys[i].debounce = DEBOUNCE;//reset debounce
}
}
}
}
void loop() {
handleOctaveSwitchEvents();
handleKeyEvents();
}