One LED, many notes? #1001
Answered
by
fitzgreyve
fitzgreyve
asked this question in
Q&A
-
NoteLED lights a single LED if a specific single MIDI note message is recieved. What's the simplest way to light one (and only one!) LED if a note is recieved within a specific range. For example any note C(1) to C(5) on channel_1 ? Many thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
fitzgreyve
Feb 6, 2024
Replies: 2 comments 3 replies
-
When should the LED be turned off? |
Beta Was this translation helpful? Give feedback.
1 reply
-
You could try something like this (untested): #include <Control_Surface.h> // Include the Control Surface library
// Instantiate a MIDI over USB interface.
USBMIDI_Interface midi;
// MIDI Input Element that listens for a range of MIDI Note events and turns
// on/off the LED if the velocity is above/below the threshold.
class CustomNoteLED
// First, inherit from the MatchingMIDIInputElement base class. Indicate that
// you want to listen to a range of MIDI Note events by specifying the MIDI
// message type, and use a 2-byte range MIDI message matcher, since MIDI Note
// events have two data bytes.
: public MatchingMIDIInputElement<MIDIMessageType::NoteOn,
TwoByteRangeMIDIMatcher> {
public:
// Constructor
CustomNoteLED(pin_t ledPin, MIDIAddress address, uint8_t range_len)
: MatchingMIDIInputElement({address, range_len}), ledPin(ledPin) {}
// Called once upon initialization, set up the pin mode for the LED.
void begin() override { ExtIO::pinMode(ledPin, OUTPUT); }
// Called when an incoming MIDI Note message matches this element's matcher
// (i.e. it has the right MIDI address, channel and cable).
// The match object that's passed as a parameter contains the velocity value
// of the Note message that matched.
void handleUpdate(TwoByteRangeMIDIMatcher::Result match) override {
ExtIO::digitalWrite(ledPin, match.value >= threshold ? HIGH : LOW);
}
// Called when the input element is reset (e.g. by an "All notes off" MIDI
// event).
void reset() override { ExtIO::digitalWrite(ledPin, LOW); }
private:
pin_t ledPin;
const static uint8_t threshold = 0x01;
};
// Instantiate the LED that will light up when any note from C1 to C5 is
// playing.
CustomNoteLED led {
LED_BUILTIN, // Pin with the LED connected
{MIDI_Notes::C(1), Channel_1}, // Note C1 on MIDI channel 1
MIDI_Notes::C(5) - MIDI_Notes::C(1), // Length of the range
};
void setup() {
Control_Surface.begin(); // Initialize Control Surface
}
void loop() {
Control_Surface.loop(); // Update the Control Surface
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works well, and a good starting point for further modification - many thanks.