Hi. I’ve gone through various “toggle led on and off” sketches but am struggling with multiple buttons and leds.
Basically I have a simple midi footswitch controller using the control surface library and the input is via 8 push to make switches that send note on/off data. That works fine given the device it’s controlling is very basic. Have to Toggle to switch functions on and off.
However I need to add 6 leds to 6 of the buttons as I need a visual sign of what I’ve switched on so I know to switch it back off, hence the toggle led.
A lot of the sketches are quite old and have seen quite a few variants so am a bit stuck.
I need to read the state of 6 buttons and change the led status of a corresponding LED every time I press a button.
Was going off this sketch but am a bit confused with how to get the code right for multiple buttons and leds
#include <Control_Surface.h> // https://github.com/tttapa/Control-Surface
using namespace MIDI_Notes;
USBMIDI_Interface midi; // Instantiate a MIDI Interface to use
// Get the length of an array
template <class T, size_t N> constexpr size_t len(T (&)[N]) { return N; }
// Create an array of switches that send out MIDI Note On/Off messages
NoteButtonLatched footswitches[] = {
{ 2, note(C, 4) },
{ 3, note(Db, 4) },
{ 4, note(D, 4) },
{ 5, note(Eb, 4) },
};
const pin_t ledPins[] = { 6, 7, 8, 9 };
static_assert(len(footswitches) == len(ledPins),
"Error: requires same number of switches as LEDs");
void setup() {
Control_Surface.begin(); // Initialize the Control Surface
for (auto pin : ledPins) // Initialize LED pins
pinMode(pin, OUTPUT);
}
void loop() {
Control_Surface.loop(); // Update the Control Surface
// Loop over all switches and LEDs
for (size_t i = 0; i < len(ledPins); ++i)
// If the button was pressed, this means the toggled state changed
if (footswitches[i].getButtonState() == Button::Falling)
// Update the LED state to reflect the toggled switch state
digitalWrite(ledPins[i], footswitches[i].getState() ? HIGH : LOW);
}
Thanks for the info, much appreciated. Just waiting for some extra hardware so I can upload and test the sketches.
As I’m using the control surface library, the 2nd sketch supplied is more than likely the one I use once I’ve matched up the pin configuration and inputted my midi note info.
To answer the query regarding the buttons, yes, I want to have 6 non latching push to make switches sending a midi note and toggling an led on and off, that’s sited just above each switch. I’m controlling a basic Dmx lighting desk so I can swap between chases etc whilst playing. Unfortunately Each chases function requires a toggle on and off and won’t simply switch between chases, hence the led to remind me what’s on
Hi. I’ve Cobbled together some hardware and tweaked the pin inputs and outputs to test the sketch supplied by Pieter as well as changed the midi interface to serial so I can test the midi output on my Dmx desk.
The LEDs work perfectly, toggle on and off with each button press, but the midi side doesn’t. Have to make 2 presses of a button to send the midi note. I’m guessing it’s to do with NoteButtonLatched.
Thanks for the tip, was just reviewing the user guide
#include <Control_Surface.h> // https://github.com/tttapa/Control-Surface
using namespace MIDI_Notes;
HardwareSerialMIDI_Interface midi = {Serial, MIDI_BAUD}; // Instantiate a MIDI Interface to use
// Get the length of an array
template <class T, size_t N> constexpr size_t len(T (&)[N]) {
return N;
}
// Create an array of switches that send out MIDI Note On/Off messages
NoteButtonLatched footswitches[] = {
{ 2, 0 },
{ 3, 120 },
{ 4, 121 },
{ 5, 122 },
{ 6, 32 },
{ 7, 40 },
{ 8, 48 },
{ 9, 126, },
};
const pin_t ledPins[] = { 10, 11, 12, 13, A0, A1, A2, A3 };
static_assert(len(footswitches) == len(ledPins),
"Error: requires same number of switches as LEDs");
void setup() {
Control_Surface.begin(); // Initialize the Control Surface
for (auto pin : ledPins) // Initialize LED pins
pinMode(pin, OUTPUT);
}
void loop() {
Control_Surface.loop(); // Update the Control Surface
// Loop over all switches and LEDs
for (size_t i = 0; i < len(ledPins); ++i)
// If the button was pressed, this means the toggled state changed
if (footswitches[i].getButtonState() == Button::Falling)
// Update the LED state to reflect the toggled switch state
digitalWrite(ledPins[i], footswitches[i].getState() ? HIGH : LOW);
}
This specifies the input pin number and the midi note. Previously I had 8 lines of code (1 for each button) which included the midi channel. Worked a treat.
static bool ledStates[len(ledPins)] {};
// Loop over all switches and LEDs
for (size_t i = 0; i < len(ledPins); ++i)
// If the button was pressed
if (footswitches[i].getButtonState() == Button::Falling) {
ledStates[i] ^= 1; // Toggle the state
// Update the LED state to reflect the new state
digitalWrite(ledPins[i], ledStates[i] ? HIGH : LOW);
}
However, this way it is impossible to keep the LEDs on the Arduino and the other MIDI device in sync, since neither of them has any way of knowing the state of the other.