-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidi_guard.ino
More file actions
245 lines (214 loc) · 6.78 KB
/
midi_guard.ino
File metadata and controls
245 lines (214 loc) · 6.78 KB
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
#include <Adafruit_NeoPixel.h>
#include <MIDI.h>
#define RE_CLK_PIN 2
#define RE_DT_PIN 3
#define RE_BUTTON_PIN 4
#define SWITCH_PIN 5
#define LED_PIN 6
#define KEYPAD_PIN A7
// ------------------------------- SWITCH SETUP --------------------------------
bool setupMode = false;
// -------------------------------- MIDI SETUP ---------------------------------
MIDI_CREATE_DEFAULT_INSTANCE();
const byte MIDI_CHANNEL = 1;
// ---------------------------- ROTARY ENCODER SETUP ---------------------------
int encoder0Pos = 0;
int valRotary;
int lastValRotary;
int valPot = 0;
bool valChange = false;
byte keyPadTolerance = 10;
byte keyPadButton;
byte keyPadOld;
int note = 36; // C3 (https://newt.phys.unsw.edu.au/jw/notes.html)
int velocity = 127;
// ------------------------------ LED RING SETUP -------------------------------
const byte NUM_PIXELS = 12;
Adafruit_NeoPixel ledRing = Adafruit_NeoPixel(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
// #############################################################################
void setup() {
// MIDI
MIDI.begin(MIDI_CHANNEL);
// MIDI.setHandleNoteOn(handleNoteOn);
// MIDI.setHandleNoteOff(handleNoteOff);
// Start led ring
ledRing.begin();
ledRing.setBrightness(50); // 0 - 255
ledRing.setPixelColor(10, ledRing.Color(0, 150, 0));
ledRing.setPixelColor(8, ledRing.Color(0, 150, 0));
ledRing.setPixelColor(6, ledRing.Color(0, 150, 0));
ledRing.show();
// Switch
pinMode(SWITCH_PIN, INPUT_PULLUP);
// Setup rotary encoder pins
pinMode(RE_CLK_PIN, INPUT_PULLUP);
pinMode(RE_DT_PIN, INPUT_PULLUP);
pinMode(RE_BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RE_CLK_PIN), readEncoder, CHANGE);
Serial.begin(9600);
}
// #############################################################################
void loop() {
readKeyPad();
readSwitch();
// Rotary encoder
readREButton();
// if (valRotary>lastValRotary)
// {
// valPot = min(valRotary, 127);
// MIDI.sendControlChange(20, min(valRotary, 120), 1);
// Serial.print("MINUS");
// }
// if(valRotary<lastValRotary)
// {
// valPot = max(valRotary, 0);
// MIDI.sendControlChange(20, max(valRotary, 0), 1);
// Serial.print("PLUS");
// }
// lastValRotary = valRotary;
// Serial.println(valRotary);
// Serial.println(" ");
// Serial.println(valPot);
}
// #############################################################################
void readEncoder() {
if (digitalRead(RE_CLK_PIN) == digitalRead(RE_DT_PIN)) {
encoder0Pos--;
} else {
encoder0Pos++;
}
valRotary = encoder0Pos / 7;
// valPot = encoder0Pos / 5;
if (valRotary>lastValRotary) {
valPot++;
valPot = min(valPot, 12);
valChange = true;
}
else if(valRotary<lastValRotary) {
valPot--;
valPot = max(0, valPot);
valChange = true;
} else {
valChange = false;
}
Serial.println(valPot);
lastValRotary = valRotary;
}
void readKeyPad() {
int sensorValue = analogRead(KEYPAD_PIN);
if (sensorValue - keyPadTolerance <= 1023 && sensorValue + keyPadTolerance >= 1023) {
keyPadButton = 1;
} else if (sensorValue - keyPadTolerance <= 930 && sensorValue + keyPadTolerance >= 930) {
keyPadButton = 2;
} else if (sensorValue - keyPadTolerance <= 853 && sensorValue + keyPadTolerance >= 853) {
keyPadButton = 3;
} else if (sensorValue - keyPadTolerance <= 785 && sensorValue + keyPadTolerance >= 785) {
keyPadButton = 4;
} else if (sensorValue - keyPadTolerance <= 729 && sensorValue + keyPadTolerance >= 729) {
keyPadButton = 5;
} else if (sensorValue - keyPadTolerance <= 681 && sensorValue + keyPadTolerance >= 681) {
keyPadButton = 6;
} else if (sensorValue - keyPadTolerance <= 637 && sensorValue + keyPadTolerance >= 637) {
keyPadButton = 7;
note = 40;
} else if (sensorValue - keyPadTolerance <= 600 && sensorValue + keyPadTolerance >= 600) {
keyPadButton = 8;
note = 43;
} else if (sensorValue - keyPadTolerance <= 567 && sensorValue + keyPadTolerance >= 567) {
keyPadButton = 9;
note = 45;
} else if (sensorValue - keyPadTolerance <= 536 && sensorValue + keyPadTolerance >= 536) {
keyPadButton = 10;
note = 47;
} else if (sensorValue - keyPadTolerance <= 509 && sensorValue + keyPadTolerance >= 509) {
keyPadButton = 11;
note = 49;
} else if (sensorValue - keyPadTolerance <= 485 && sensorValue + keyPadTolerance >= 485) {
keyPadButton = 12;
note = 50;
} else {
keyPadButton = 0;
}
if (keyPadOld != keyPadButton) {
note = keyPadButton * 8 + 10;
MIDI.sendNoteOn(note, velocity, 1);
// delay(200); // Wait for a second
// MIDI.sendNoteOff(note, 0, MIDI_CHANNEL); // Stop the note
keyPadOld = keyPadButton;
}
}
void readREButton() {
if (digitalRead(RE_BUTTON_PIN) == LOW){
// Button pressed
clearLedRing();
}
}
void readSwitch() {
if (digitalRead(SWITCH_PIN) == LOW){
// UP
if (valChange) {
clearLedRing();
}
lightLedRing(0, valPot, valRotary);
setupMode = true;
// ledRing.setPixelColor(1, ledRing.Color(10, 150, 50));
// ledRing.setPixelColor(2, ledRing.Color(10, 150, 70));
// ledRing.setPixelColor(3, ledRing.Color(10, 150, 90));
ledRing.show();
} else {
// DOWN
setupMode = false;
// ledRing.setPixelColor(4, ledRing.Color(100, 10, 10));
// TODO: clear only when changed
// clearLedRing();
colorLedRing(0, keyPadButton, ledRing.Color(100, 10, 10));
ledRing.show();
}
}
void setupValue() {
switch (keyPadButton) {
case 0:
ledRing.setBrightness(valRotary + 10);
ledRing.show();
break;
case 1:
MIDI.sendControlChange(21, min(valRotary, 120), MIDI_CHANNEL);
break;
case 2:
MIDI.sendControlChange(22, min(valRotary, 120), MIDI_CHANNEL);
break;
}
}
void clearLedRing() {
for(int i = 0; i< NUM_PIXELS; i++){
ledRing.setPixelColor(i, 0x000000);
ledRing.show();
}
}
void lightLedRing(int startIndex, int nPixels, int colorIdx) {
for(int i=startIndex;i<startIndex+nPixels;i++){
ledRing.setPixelColor(i, colorWheel(colorIdx));
ledRing.show();
}
}
void colorLedRing(int startIndex, int nPixels, uint32_t ledColor) {
for(int i=startIndex;i<startIndex+nPixels;i++){
ledRing.setPixelColor(i, ledColor);
ledRing.show();
}
}
uint32_t colorWheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return ledRing.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return ledRing.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return ledRing.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
uint32_t dimColor(uint32_t color, uint8_t width) {
return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
}