indicator lights for buttons

Status
Not open for further replies.

edrummer

Well-known member
I need to add an indicator light to each button in this sketch, so that a led is turned on with the note on command and off with the note off command. Anyone know how this could be done?




Code:
/* Buttons to USB MIDI Example

   You must select MIDI from the "Tools > USB Type" menu

   To view the raw MIDI data on Linux: aseqdump -p "Teensy MIDI"

   This example code is in the public domain.
*/

#include <Bounce.h>

// the MIDI channel number to send messages
const int channel = 1;

// Create Bounce objects for each button.  The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5);  // 5 = 5 ms debounce time
Bounce button2 = Bounce(2, 5);  // which is appropriate for good
Bounce button3 = Bounce(3, 5);  // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(5, 5);  // if a button is too "sensitive"
Bounce button6 = Bounce(6, 5);  // to rapid touch, you can
Bounce button7 = Bounce(7, 5);  // increase this time.
Bounce button8 = Bounce(8, 5);
Bounce button9 = Bounce(9, 5);
Bounce button10 = Bounce(10, 5);
Bounce button11 = Bounce(11, 5);

void setup() {
  // Configure the pins for input mode with pullup resistors.
  // The pushbuttons connect from each pin to ground.  When
  // the button is pressed, the pin reads LOW because the button
  // shorts it to ground.  When released, the pin reads HIGH
  // because the pullup resistor connects to +5 volts inside
  // the chip.  LOW for "on", and HIGH for "off" may seem
  // backwards, but using the on-chip pullup resistors is very
  // convenient.  The scheme is called "active low", and it's
  // very commonly used in electronics... so much that the chip
  // has built-in pullup resistors!
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);  // Teensy++ 2.0 LED, may need 1k resistor pullup
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP); // Teensy 2.0 LED, may need 1k resistor pullup
}

void loop() {
  // Update all the buttons.  There should not be any long
  // delays in loop(), so this runs repetitively at a rate
  // faster than the buttons could be pressed and released.
  button0.update();
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();
  button6.update();
  button7.update();
  button8.update();
  button9.update();
  button10.update();
  button11.update();

  // Check each button for "falling" edge.
  // Send a MIDI Note On message when each button presses
  // Update the Joystick buttons only upon changes.
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)
  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 (button3.fallingEdge()) {
    usbMIDI.sendNoteOn(63, 99, channel);  // 63 = D#4
  }
  if (button4.fallingEdge()) {
    usbMIDI.sendNoteOn(64, 99, channel);  // 64 = E4
  }
  if (button5.fallingEdge()) {
    usbMIDI.sendNoteOn(65, 99, channel);  // 65 = F4
  }
  if (button6.fallingEdge()) {
    usbMIDI.sendNoteOn(66, 99, channel);  // 66 = F#4
  }
  if (button7.fallingEdge()) {
    usbMIDI.sendNoteOn(67, 99, channel);  // 67 = G4
  }
  if (button8.fallingEdge()) {
    usbMIDI.sendNoteOn(68, 99, channel);  // 68 = G#4
  }
  if (button9.fallingEdge()) {
    usbMIDI.sendNoteOn(69, 99, channel);  // 69 = A5
  }
  if (button10.fallingEdge()) {
    usbMIDI.sendNoteOn(70, 99, channel);  // 70 = A#5
  }
  if (button11.fallingEdge()) {
    usbMIDI.sendNoteOn(71, 99, channel);  // 71 = B5
  }

  // Check each button for "rising" edge
  // Send a MIDI Note Off message when each button releases
  // For many types of projects, you only care when the button
  // is pressed and the release isn't needed.
  // rising = low (pressed - button connects pin to ground)
  //          to high (not pressed - voltage from pullup resistor)
  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
  }
  if (button3.risingEdge()) {
    usbMIDI.sendNoteOff(63, 0, channel);  // 63 = D#4
  }
  if (button4.risingEdge()) {
    usbMIDI.sendNoteOff(64, 0, channel);  // 64 = E4
  }
  if (button5.risingEdge()) {
    usbMIDI.sendNoteOff(65, 0, channel);  // 65 = F4
  }
  if (button6.risingEdge()) {
    usbMIDI.sendNoteOff(66, 0, channel);  // 66 = F#4
  }
  if (button7.risingEdge()) {
    usbMIDI.sendNoteOff(67, 0, channel);  // 67 = G4
  }
  if (button8.risingEdge()) {
    usbMIDI.sendNoteOff(68, 0, channel);  // 68 = G#4
  }
  if (button9.risingEdge()) {
    usbMIDI.sendNoteOff(69, 0, channel);  // 69 = A5
  }
  if (button10.risingEdge()) {
    usbMIDI.sendNoteOff(70, 0, channel);  // 70 = A#5
  }
  if (button11.risingEdge()) {
    usbMIDI.sendNoteOff(71, 0, channel);  // 71 = B5
  }

  // MIDI Controllers should discard incoming MIDI messages.
  // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
  while (usbMIDI.read()) {
    // ignore incoming messages
  }
}
 
it should be fairly easy to do what you want, figure out what pins you want to use for led indicators. then set that pin as output. then in the part of the program that detects the falling edge light the indicator, then in the part that detects the rising edge turn off the indicator.
 
In setup, add pinMode(), like this:

Code:
void setup() {
  pinMode(13, OUTPUT);
  // Configure the pins for input mode with pullup resistors.

In loop, add digitalWrite(), like this:

Code:
  if (button0.fallingEdge()) {
    usbMIDI.sendNoteOn(60, 99, channel);  // 60 = C4
    digitalWrite(13, HIGH);
  }

and this:

Code:
  if (button0.risingEdge()) {
    usbMIDI.sendNoteOff(60, 0, channel);  // 60 = C4
    digitalWrite(13, LOW);
  }

Then you will see the orange LED turn on and off. Easy, right?
 
There will be 40 buttons, I wanted to see if I could get it working first then see if I can expand the pin count with MCP23017 port expanders. I have a T3.6, all 24 analog pins are used for pots, so I still need 40 pins for the switches and 40 pins fot the leds correct?
 
Did I do something wrong? the led doesn't come on either the one on the board or if I put one on pin 13.

Code:
#include <Bounce.h>

// the MIDI channel number to send messages
const int channel = 1;

// Create Bounce objects for each button.  The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5);  // 5 = 5 ms debounce time
Bounce button2 = Bounce(2, 5);  // which is appropriate for good
Bounce button3 = Bounce(3, 5);  // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(5, 5);  // if a button is too "sensitive"
Bounce button6 = Bounce(6, 5);  // to rapid touch, you can
Bounce button7 = Bounce(7, 5);  // increase this time.
Bounce button8 = Bounce(8, 5);
Bounce button9 = Bounce(9, 5);
Bounce button10 = Bounce(10, 5);
Bounce button11 = Bounce(11, 5);

void setup() {
  // Configure the pins for input mode with pullup resistors.
  // The pushbuttons connect from each pin to ground.  When
  // the button is pressed, the pin reads LOW because the button
  // shorts it to ground.  When released, the pin reads HIGH
  // because the pullup resistor connects to +5 volts inside
  // the chip.  LOW for "on", and HIGH for "off" may seem
  // backwards, but using the on-chip pullup resistors is very
  // convenient.  The scheme is called "active low", and it's
  // very commonly used in electronics... so much that the chip
  // has built-in pullup resistors!
  pinMode(13, OUTPUT);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);  // Teensy++ 2.0 LED, may need 1k resistor pullup
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP); // Teensy 2.0 LED, may need 1k resistor pullup
}

void loop() {
  // Update all the buttons.  There should not be any long
  // delays in loop(), so this runs repetitively at a rate
  // faster than the buttons could be pressed and released.
  button0.update();
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();
  button6.update();
  button7.update();
  button8.update();
  button9.update();
  button10.update();
  button11.update();

  // Check each button for "falling" edge.
  // Send a MIDI Note On message when each button presses
  // Update the Joystick buttons only upon changes.
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)


   if (button0.fallingEdge()) {
    usbMIDI.sendNoteOn(60, 99, channel);  // 60 = C4
    digitalWrite(13, HIGH);
  }
  if (button1.fallingEdge()) {
    usbMIDI.sendNoteOn(61, 99, channel);  // 61 = C#4
  }
  if (button2.fallingEdge()) {
    usbMIDI.sendNoteOn(62, 99, channel);  // 62 = D4
  }
  if (button3.fallingEdge()) {
    usbMIDI.sendNoteOn(63, 99, channel);  // 63 = D#4
  }
  if (button4.fallingEdge()) {
    usbMIDI.sendNoteOn(64, 99, channel);  // 64 = E4
  }
  if (button5.fallingEdge()) {
    usbMIDI.sendNoteOn(65, 99, channel);  // 65 = F4
  }
  if (button6.fallingEdge()) {
    usbMIDI.sendNoteOn(66, 99, channel);  // 66 = F#4
  }
  if (button7.fallingEdge()) {
    usbMIDI.sendNoteOn(67, 99, channel);  // 67 = G4
  }
  if (button8.fallingEdge()) {
    usbMIDI.sendNoteOn(68, 99, channel);  // 68 = G#4
  }
  if (button9.fallingEdge()) {
    usbMIDI.sendNoteOn(69, 99, channel);  // 69 = A5
  }
  if (button10.fallingEdge()) {
    usbMIDI.sendNoteOn(70, 99, channel);  // 70 = A#5
  }
  if (button11.fallingEdge()) {
    usbMIDI.sendNoteOn(71, 99, channel);  // 71 = B5
  }

  // Check each button for "rising" edge
  // Send a MIDI Note Off message when each button releases
  // For many types of projects, you only care when the button
  // is pressed and the release isn't needed.
  // rising = low (pressed - button connects pin to ground)
  //          to high (not pressed - voltage from pullup resistor)
  if (button0.risingEdge()) {
    usbMIDI.sendNoteOff(60, 0, channel);// 60 = C4
    digitalWrite(13, LOW);
  }
  if (button1.risingEdge()) {
    usbMIDI.sendNoteOff(61, 0, channel);  // 61 = C#4
  }
  if (button2.risingEdge()) {
    usbMIDI.sendNoteOff(62, 0, channel);  // 62 = D4
  }
  if (button3.risingEdge()) {
    usbMIDI.sendNoteOff(63, 0, channel);  // 63 = D#4
  }
  if (button4.risingEdge()) {
    usbMIDI.sendNoteOff(64, 0, channel);  // 64 = E4
  }
  if (button5.risingEdge()) {
    usbMIDI.sendNoteOff(65, 0, channel);  // 65 = F4
  }
  if (button6.risingEdge()) {
    usbMIDI.sendNoteOff(66, 0, channel);  // 66 = F#4
  }
  if (button7.risingEdge()) {
    usbMIDI.sendNoteOff(67, 0, channel);  // 67 = G4
  }
  if (button8.risingEdge()) {
    usbMIDI.sendNoteOff(68, 0, channel);  // 68 = G#4
  }
  if (button9.risingEdge()) {
    usbMIDI.sendNoteOff(69, 0, channel);  // 69 = A5
  }
  if (button10.risingEdge()) {
    usbMIDI.sendNoteOff(70, 0, channel);  // 70 = A#5
  }
  if (button11.risingEdge()) {
    usbMIDI.sendNoteOff(71, 0, channel);  // 71 = B5
  }

  // MIDI Controllers should discard incoming MIDI messages.
  // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
  while (usbMIDI.read()) {
    // ignore incoming messages
  }
}
 
I am using a T3.2 to test this I have a led connected to pin 13. I tested this with the blink sketch both the led and the orange one on the board work but nothing with the sketch in post # 8
 
Am I missing something? I simplified the code for just 3 buttons, the lights work with blink sketch. Just trying to get 1 to work on pin13, no response

Code:
#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(13, OUTPUT);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);

}

void loop() {
 
  button0.update();
  button1.update();
  button2.update();


  if (button0.fallingEdge()) {
    usbMIDI.sendNoteOn(60, 99, channel);  // 60 = C4 
    digitalWrite(13, HIGH);
  }
  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
    digitalWrite(13, LOW);
  }
  if (button1.risingEdge()) {
    usbMIDI.sendNoteOff(61, 0, channel);  // 61 = C#4
  }
  if (button2.risingEdge()) {
    usbMIDI.sendNoteOff(62, 0, channel);  // 62 = D4
  }

  while (usbMIDI.read()) {

  }
}
 
Do your switches pull the pin to ground?

Perhaps you could wire the LED Cathode to the switch and the Anode to power thru a current limiting resistor. That way when the switch is activated the LED would light and when not pressed, the LED extinguishes. No extra ports or programming required.
 
the led needs to come on or off with the the note on/note off commands. The buttons will be mapped to parameters like record, so when I press that button the led in the switch also lights. they need to stay on or off exactly with the note on/note off. Are you saying the reason the led doesn't work is because the switches are connected to digital pins and gnd?
 
The light only stays on while I hold the button down, I need it to be on with the note on and off with the note off commands is there a way to do that? Most of the switches work like this, if you press the button while it's off it will turn it on, if you press it while it's on it will turn it off. I just need to have the leds do exactly what the note on or off is doing
 
Last edited:
The light only stays on while I hold the button down, I need it to be on with the note on and off with the note off commands is there a way to do that? Most of the switches work like this, if you press the button while it's off it will turn it on, if you press it while it's on it will turn it off. I just need to have the leds do exactly what the note on or off is doing

Ah, normally the note goes on when the switch is pressed and goes off when the switch is released, like a piano key. And that's what people have explained how to do and that's what you've coded.

But that's not what you want, you want the switch press to toggle note on/off and the LED to track the note on/off state. This is a sticky key logic rather than the normal key logic. You cannot do that using just the switch io lines, you'll need separate io lines for the switches and for the LED's.

In your code example we can fix button0 by replacing all the if (button* logic with this:
Code:
  if (button0.fallingEdge()) {
    if (digitalRead(13)) { 
      /* note 60 = C4 is on, turn off */
      usbMIDI.sendNoteOff(60, 0, channel);  // 
    } else { 
      /* note 60 = C4 is off, turn on */
      usbMidi.sendNoteOn(60, 99, channel); 
    }
    /* toggle note on/off state for note 60 = C4 */
    digitalWrite(13, ! digitalRead(13));
  }

Now pin 13 should track your definition of note on/off logic for the switch mapped to button0. We're using pin 13 to both store and signal the note on/off state. And we're only tracking button presses, we ignore the rising edges altogether.

If you wire up LED's with current limiting resistors for your other switches, allocating a separate io pin for each switch and for each LED, then you should be able to adapt the button0/pin13 logic to make other keys work the way you want. Then you still need to figure out how to set up the IO extenders that you'll need for all those switches and LED's.
 
The reason I'm using this sketch is it's the only one that works with all the parameters I need to control in Ableton. Most simply turn on if off or off if on but one parameter allows you to map many buttons (up to 128) in series. I've had a hard time explaining this but for example, I'm using 24 buttons, so when mapping I press and hold button #1 while pressing #24 then all the buttons in between will be mapped to the parameter. This one works like preset buttons on a radio dial, when one button is pressed , it stays on till another one of the 24 is pressed. Only one of the 24 is on at a time. I don't understand why but this code works perfectly with that. So if the lights can be made to come on or off with the note on/off it would work perfectly. Thanks again.
 
Last edited:
Now I get an error saying usb midi not declared in this scope. I don't think this will work for the other parameter though. I don't understand why the lights go off when the button is released but (for example) the record parameter stays on till the button is pressed again. I thought there would be a way to make the lights behave the way the software does, but maybe this in the software and can' be done this way. Is there a way to write something like

if (button0.fallingEdge()) {
usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
toggle led(13, HIGH);

if (button0.risingEdge()) {
usbMIDI.sendNoteOff(60, 0, channel); // 60 = C4
toggle led(13, LOW);
}
 
So do you have an example?

I personally do not. I hope someone will chime in, but basicly. if you set the button for falling edge. then check for the next falling edge and unset it
that way it will be off till you hit the button, then when you hit the button again it will be off..
basicly set a flag that you check during the button push and if its on, turn it off, if its off, turn it on.
 
I believe this may work but it won't compile because of the error 'usbMidi' was not declared in this scope
 
the led needs to come on or off with the the note on/note off commands. The buttons will be mapped to parameters like record, so when I press that button the led in the switch also lights. they need to stay on or off exactly with the note on/note off. Are you saying the reason the led doesn't work is because the switches are connected to digital pins and gnd?[/QUOTE


Perhaps. It depends on the LED color and the Teensy that you are trying to use. LEDs at the red end of the spectrum have a low forward voltage required to illuminate while the colors at the blue/white end have a higher forward voltage. I think that a Teensy 3.6 would have a more difficult time trying to illuminate a blue LED while it would easily illuminate a red LED, although I have not tried this. So if you wired the LEDs across the switch, you may have very dim LEDs. This of course is only a guess and you have not posted your wiring diagram.
 
Status
Not open for further replies.
Back
Top