-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidiPatchChange.ino
195 lines (139 loc) · 4.35 KB
/
midiPatchChange.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
#include <EEPROM.h>
byte seven_seg_digits[10][7] = {
{ 1, 1, 1, 1, 1, 1, 0 }, // = 0
{ 0, 1, 1, 0, 0, 0, 0 }, // = 1
{ 1, 1, 0, 1, 1, 0, 1 }, // = 2
{ 1, 1, 1, 1, 0, 0, 1 }, // = 3
{ 0, 1, 1, 0, 0, 1, 1 }, // = 4
{ 1, 0, 1, 1, 0, 1, 1 }, // = 5
{ 1, 0, 1, 1, 1, 1, 1 }, // = 6
{ 1, 1, 1, 0, 0, 0, 0 }, // = 7
{ 1, 1, 1, 1, 1, 1, 1 }, // = 8
{ 1, 1, 1, 0, 0, 1, 1 } // = 9
};
const int LED = 13; // the pin for the debug LED
const int BUTTON1 = 11; // the input pin for patch change next
const int BUTTON2 = 12; // the input pin for patch change previous
// state varables:
int ledState = LOW; // the current state of the output pin
int buttonState1; // the current reading from the input pin
int lastButtonState1 = LOW; // the previous reading from the input pin
long lastDebounceTime1 = 0; // the last time the output pin was toggled
int buttonState2; // the current reading from the input pin
int lastButtonState2 = LOW; // the previous reading from the input pin
long lastDebounceTime2 = 0; // the last time the output pin was toggled
// config varables
long debounceDelay = 50; // the debounce time; increase if the output flickers
// patch is the curret MIDI patch
int patch = 0;
// memory location where we save current patch number in case of power down
int addr = 0;
void setupPins() {
// setup 7 segment display:
//for (byte i = 2; i <= 9; i++) {
//pinMode(i, OUTPUT);
//}
// setup the two input buttons:
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
// setup the LED output, this is used just for debugging
pinMode(LED, OUTPUT);
}
void setupOutputs() {
// turn on the LED
digitalWrite(LED, HIGH);
//sevenSegWrite(patch);
// turn the dot off the 7 segment display which is attached to pin 9
//digitalWrite(9, 0);
// setup MIDI out at the correct baud rate
Serial.begin(31250);
}
byte readPatchFromMemory() {
// read the current patch # from memory
byte patchFromMemory = EEPROM.read(addr);
// if for some strange reason it's outside of boundry, reset it
if (patchFromMemory > 128) {
patchFromMemory = 128;
}
if (patchFromMemory < 1) {
patchFromMemory = 1;
}
return patchFromMemory;
}
void setup() {
patch = readPatchFromMemory();
setupPins();
setupOutputs();
}
void sevenSegWrite(byte digit) {
byte pin = 2;
for (byte segCount = 0; segCount < 7; ++segCount) {
digitalWrite(pin, seven_seg_digits[digit][segCount]);
++pin;
}
}
void turnOffSevenSeg() {
for (byte i = 2; i <= 9; i++) {
digitalWrite(i, LOW);
}
}
void changePatch() {
// indicate a patch change event
Serial.write(192);
// indicate the new patch. Patch #1 is MIDI 0
Serial.write(patch - 1);
// blink the power light
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
digitalWrite(13, HIGH); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
digitalWrite(13, HIGH); // turn the LED off by making the voltage LOW
// update 7 segment display
//sevenSegWrite(patch);
// save patch number to memory in case of power down
EEPROM.write(addr, patch);
//digitalWrite(LED, ledState);
//if (ledState ==1 ) { ledState = 0; } else { ledState = 1; }
//digitalWrite(LED, ledState);
}
void loop() {
int reading = digitalRead(BUTTON1);
if (reading != lastButtonState1) {
// reset the debouncing timer
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (reading != buttonState1) {
buttonState1 = reading;
if (buttonState1 == HIGH) {
patch++;
if (patch > 128) {
patch = 1;
}
changePatch();
}
}
}
lastButtonState1 = reading;
reading = digitalRead(BUTTON2);
if (reading != lastButtonState2) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
if (reading != buttonState2) {
buttonState2 = reading;
if (buttonState2 == HIGH) {
if (patch == 1) {
patch = 128;
} else {
patch--;
}
changePatch();
}
}
}
lastButtonState2 = reading;
}