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
| // Developed in Arduino IDE 1.8.13 (Windows)
#include <FastLED.h> // FastLED version 3.3.3 [url]https://github.com/FastLED/FastLED[/url]
#include "MIDIUSB.h" // MIDIUSB version 1.0.4 [url]https://www.arduino.cc/en/Reference/MIDIUSB[/url]
#define BUTTON_PIN1 3
#define BUTTON_PIN2 2
#define LED_ACT_PIN 4
#define LED_PIN 5
#define NUM_LEDS 144
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define AMPERAGE 1500 // Power supply amperage in mA
#define NOTE_OFFSET 29 // Offset of first note from left (depends on Piano)
#define LEDS_PER_NOTE 2 // How many LED are on per key
#define LED_INT_STEPS 8 // LED Intensity button steps
#define DEBUG
CRGB leds[NUM_LEDS];
byte color = 0xFFFFFF;
int ledProgram = 1;
int ledIntensity = 2; // default LED brightness
int ledBrightness = ceil(255 / LED_INT_STEPS * ledIntensity); // default LED brightness
int buttonState1;
int buttonState2;
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long debounceDelay = 50;
//------------------------------------------- FUNCTIONS ----------------------------------------------//
const char* pitch_name(byte pitch) {
static const char* names[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
return names[pitch % 12];
}
int pitch_octave(byte pitch) {
return (pitch / 12) - 1;
}
void SetNote(byte pitch, CRGB color) {
int b = 0;
for (int a = 0; a < LEDS_PER_NOTE; a++) {
//if (int(pitch) > 66){b=1;}else{b=0;} // led stripe solder joints compensation
leds[(((pitch - NOTE_OFFSET)*LEDS_PER_NOTE)+ a - b)]= color;
}
}
void SetNote2(byte pitch, int hue) {
int b = 0;
for (int a = 0; a < LEDS_PER_NOTE; a++) {
//if (int(pitch) > 66){b=1;}else{b=0;} // led stripe solder joints compensation
leds[(((pitch - NOTE_OFFSET)*LEDS_PER_NOTE)+a - b)].setHSV(hue, 255, 255);
}
}
void noteOn(byte channel, byte pitch, byte velocity) {
if (velocity > 0) {
switch (channel) {
// Left Hand
case 144: // Default Channel is 144
case 155:
case 145 ... 149:
if (ledProgram == 1){
SetNote(pitch,0x0000FF);
}else if (ledProgram == 2){
SetNote(pitch,0x00FF00);
}else if (ledProgram == 3){
SetNote2(pitch,pitch*velocity);
}else if (ledProgram == 4){
SetNote2(pitch,velocity*2);
}else if (ledProgram == 5){
SetNote(pitch,0xFFFFFF);
}else if (ledProgram == 6){
SetNote2(pitch,ceil((pitch-NOTE_OFFSET) * (255/(NUM_LEDS/LEDS_PER_NOTE))));
}else{
}
break;
// Right Hand
case 156:
case 150 ... 154:
SetNote(pitch,0x00FF00);
break;
default:
SetNote(pitch,0xFFFFFF);
break;
}
if (DEBUG == true){ Serial.println("Note ON - Channel:" + String(channel) + " Pitch:" + String(pitch) + " Note:" + pitch_name(pitch) + String(pitch_octave(pitch)) + " Velocity:" + String(velocity)); }
}else{
SetNote(pitch,0x000000); // black
if (DEBUG == true){ Serial.println("Note OFF2 - Channel:" + String(channel) + " Pitch:" + String(pitch) + " Note:" + pitch_name(pitch) + String(pitch_octave(pitch)) + " Velocity:" + String(velocity)); }
}
FastLED.show();
}
void noteOff(byte channel, byte pitch, byte velocity) {
if (ledProgram == 5){
fill_rainbow( leds, NUM_LEDS, 0, 5);
}else{
SetNote(pitch,0x000000); // black
}
#ifdef DEBUG
Serial.print("Note OFF - Channel:");
Serial.print(String(channel));
Serial.print(" Pitch:");
Serial.print(String(pitch));
Serial.print(" Note:");
Serial.print(pitch_name(pitch) + String(pitch_octave(pitch)));
Serial.print(" Velocity:");
Serial.println(String(velocity));
#endif
FastLED.show();
}
void controlChange(byte channel, byte control, byte value) {
#ifdef DEBUG
Serial.print("Control - Channel:");
Serial.print(String(channel));
Serial.print(" Control:");
Serial.print(String(control));
Serial.print(" Value:");
Serial.println(String(value));
#endif
}
//-------------------------------------------- SETUP ----------------------------------------------//
void setup() {
#ifdef DEBUG
Serial.begin(115200);
#endif
delay( 3000 ); // power-up safety delay
FastLED.setMaxPowerInVoltsAndMilliamps(5, AMPERAGE);
pinMode(BUTTON_PIN1, INPUT_PULLUP);
pinMode(BUTTON_PIN2, INPUT_PULLUP);
pinMode(LED_ACT_PIN, OUTPUT);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness(ledBrightness);
fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
SetNote(NOTE_OFFSET+ledProgram-1,0xFF0000);
FastLED.show();
#ifdef DEBUG
Serial.println("MIDI2Neopixel is ready!");
#endif
}
//--------------------------------------------- LOOP ----------------------------------------------//
void loop() {
////////////////////////////////////////////// Button 1 /////////////////////////////////////////////
int reading1 = digitalRead(BUTTON_PIN1);
if (reading1 != lastButtonState1) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (reading1 != buttonState1) {
buttonState1 = reading1;
// only toggle the LED if the new button state is HIGH
if (buttonState1 == LOW) {
ledProgram++;
if (ledProgram>6){
ledProgram = 1;
}
if (DEBUG == true){ Serial.println("Button 1 pressed! Program is " + String(ledProgram)); }
// Set LED indication for program
fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
SetNote(NOTE_OFFSET+ledProgram-1,0xFF0000);
FastLED.show();
}
}
}
////////////////////////////////////////////// Button 2 /////////////////////////////////////////////
int reading2 = digitalRead(BUTTON_PIN2);
if (reading2 != lastButtonState2) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
if (reading2 != buttonState2) {
buttonState2 = reading2;
// only toggle the LED if the new button state is HIGH
if (buttonState2 == LOW) {
ledIntensity++;
SetNote(NOTE_OFFSET+ledIntensity-2,0x000000);
if (ledIntensity>LED_INT_STEPS){
ledIntensity = 1;
}
// Map ledIntensity to ledBrightness
ledBrightness = map(ledIntensity, 1, LED_INT_STEPS, 3, 255);
FastLED.setBrightness(ledBrightness);
#ifdef DEBUG
Serial.print("Button 2 pressed! Intensity is ");
Serial.print(String(ledIntensity));
Serial.print(" and brightness to ");
Serial.print(String(ledBrightness));
#endif
// Set LED indication for program
SetNote(NOTE_OFFSET+ledIntensity-1,0x00FF00);
FastLED.show();
}
}
}
////////////////////////////////////////// MIDI Read routine ////////////////////////////////////////
midiEventPacket_t rx = MidiUSB.read();
if (rx.header) {digitalWrite(LED_ACT_PIN, HIGH);}
switch (rx.header) {
case 0:
break; //No pending events
case 0x9:
noteOn(rx.byte1,rx.byte2,rx.byte3);
break;
case 0x8:
noteOff(rx.byte1,rx.byte2,rx.byte3);
break;
case 0xB:
controlChange(rx.byte1 & 0xF,rx.byte2,rx.byte3);
break;
default:
#ifdef DEBUG
Serial.print("Unhandled MIDI message: ");
Serial.print(rx.header, HEX);
Serial.print("-");
Serial.print(rx.byte1, HEX);
Serial.print("-");
Serial.print(rx.byte2, HEX);
Serial.print("-");
Serial.println(rx.byte3, HEX);
#endif
break;
}
if (rx.header) {digitalWrite(LED_ACT_PIN, LOW);}
lastButtonState1 = reading1;
lastButtonState2 = reading2;
}
//------------------------------------------ END OF LOOP -------------------------------------------// |