Help req with toggle led - 6 buttons 6 leds

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

Thanks. Dave

Here is a basic 4 button/LED version of what you describe. It can be improved

const byte buttonPins[] = {A3, A2, A1, A0};
const byte ledPins[] = {3, 5, 6, 9};
byte previousButtonStates[] = {HIGH, HIGH, HIGH, HIGH};
byte currentButtonState;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int p = 0; p < 4; p++)
  {
    pinMode(buttonPins[p], INPUT_PULLUP);
    pinMode(ledPins[p], OUTPUT);
  }
}

void loop()
{
  for (int p = 0; p < 4; p++)
  {
    currentButtonState = digitalRead(buttonPins[p]);
    if (currentButtonState != previousButtonStates[p] && currentButtonState == LOW)
    {
      digitalWrite(ledPins[p], !digitalRead(ledPins[p]));
    }
    previousButtonStates[p] = currentButtonState;
  }
}

Hello
Do you mean to toggle a led ON/OFF with one button and that for a group of X leds and buttons?

This should work:

#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

Thanks

Dave

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

Dave
sketch_jun14a.ino (1.3 KB)

Please post the code here, using code tags when you do rather than attaching it

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);
}

Thanks. It really is easier for everyone posted in code tags and people are more likely to look at it if you make it easy for them

I know very little about MIDI, but where in the code are the messages to MIDI sent out ?

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.

NoteButton button1 = {2, {0, CHANNEL_1},
};

Then change it back to

NoteButton footswitches[] { ... };

and keep the state yourself:

  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.

Slight delay in getting all the parts together but am sorted now. Thanks again for the help with this.
Cheers

Dave

hi , dave71. I have a question for you, could we speak privately on it? thank you

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.