I have a working code in which the LED strip responds to MIDI messages from a keyboard, but only displays one color.
How could this code be adjusted to have each pixel display a different color based on the MIDI note received, such as the Keyboard image shows (blue on left of strip moving along the color wheel to green on right)
inline void handleGateChanged(bool inGateActive)
{
digitalWrite(LED_BUILTIN, inGateActive ? LOW : HIGH);
}
inline void pulseGate()
{
handleGateChanged(false);
delay(10);
handleGateChanged(true);
}
// -----------------------------------------------------------------------------
byte noteToStripIndex(byte pitch)
{
byte index;
if(pitch < KEYBOARD_LOWEST_NOTE)
index = 0;
else if(pitch > KEYBOARD_HIGHEST_NOTE)
index = NUMPIXELS -1;
else
index = (NUMPIXELS -1) * (pitch - KEYBOARD_LOWEST_NOTE) / (KEYBOARD_HIGHEST_NOTE - KEYBOARD_LOWEST_NOTE);
if(STRIP_REVERSED)
index = (NUMPIXELS -1) - index;
return index;
}
void handleNotesChanged(bool isFirstNote = true)
{
if (midiNotes.empty())
{
strip.clear();
strip.show();
handleGateChanged(true);
}
else
{
byte pitch = 0;
strip.clear();
for (byte i = 0; i < midiNotes.size(); ++i)
{
if(midiNotes.get(i, pitch))
{
[b]strip.setPixelColor(noteToStripIndex(pitch), 255,255,0);[/b] //follows GRB
}
}
strip.show();
}
}
// -----------------------------------------------------------------------------
void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
{
const bool firstNote = midiNotes.empty();
midiNotes.add(MidiNote(inNote, inVelocity));
//digitalWrite(LED_BUILTIN, LOW);
handleNotesChanged(firstNote);
}
void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
{
midiNotes.remove(inNote);
//digitalWrite(LED_BUILTIN, HIGH);
handleNotesChanged();
}
// -----------------------------------------------------------------------------
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
//Serial.begin(115200);//For debug only
digitalWrite(LED_BUILTIN, HIGH);
strip.begin();
//strip.show(); // Initialize all pixels to 'off'
strip.clear();
strip.show();
}
void loop()
{
MIDI.read();
I'm very new to programming! Any help would be greatly appreciated.
Using Adafruit DotStar LED and Arduino UNO with MIDI input