I would like to add leds to each button switch in the first code, The first code sends midi messages to a DAW I would like it to also turn on a led as an indicator light. The switch needs to turn the light on if off and off if on. The second code is an example that does this, it works well with one button. I would like to have the 3 buttons on pins 0, 1, 2 and the leds on pins 3, 4, and 5.

#include <Bounce.h>

const int channel = 1;
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5);  
Bounce button2 = Bounce(2, 5);  

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
void loop() {

  if (button0.fallingEdge()) {
    usbMIDI.sendNoteOn(60, 99, channel);  // 60 = C4 
  if (button1.fallingEdge()) {
    usbMIDI.sendNoteOn(61, 99, channel);  // 61 = C#4
  if (button2.fallingEdge()) {
    usbMIDI.sendNoteOn(62, 99, channel);  // 62 = D4

  if (button0.risingEdge()) {
    usbMIDI.sendNoteOff(60, 0, channel);// 60 = C4
  if (button1.risingEdge()) {
    usbMIDI.sendNoteOff(61, 0, channel);  // 61 = C#4
  if (button2.risingEdge()) {
    usbMIDI.sendNoteOff(62, 0, channel);  // 62 = D4
  while (usbMIDI.read()) {

Second sketch:

Simple toggle switch
Created by: P.Agiakatsikas

int button = 0;
int led = 13;
int status = false;

void setup(){
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH

void loop(){
//a) if the button is not pressed the false status is reversed by !status and the LED turns on
//b) if the button is pressed the true status is reveresed by !status and the LED turns off

if (digitalRead(button) == true) {
  status = !status;
  digitalWrite(led, status);
} while(digitalRead(button) == true);
  delay(50); // keeps a small delay
  • Your "Simple toggle switch" code toggles the button status on/off on each button press. Your MIDI sketch is more like an organ key, where the note plays as long as the key is held down, and stops playing when the button is released. Do you want the LEDs to toggle on/off on each button press even though that is not how the MIDI note behaves?
    – Duncan C
    Sep 17, 2019 at 18:36
  • This is not a code writing service. You should attempt to edit your code yourself, and post questions if you have trouble.
    – Duncan C
    Sep 17, 2019 at 18:37
  • The midi sketch is used to to switch things on or off not for playing notes, I don't know why but this is the only sketch I found that works. I have tried to change the note on/off to cc messages but it does not work. The midi sketch will turn on a parameter in Ableton (for example) the record function and and it will stay on until pressed again. which is exactly what I need the lights to do. I am not asking anybody to wright the complete code for me Just an example how to do it.
    – edrummer
    Sep 17, 2019 at 19:20

1 Answer 1


I'd suggest creating an array of MIDI button structs. For each entry you'd have a button object, an LED pin, and a BOOL to track the state of thate button.

Write a function that loops through the array of structs, checks the state of each one, and does the work to play/stop the notes and toggle the LED states based on the state of each button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.