LED light on pin 13

Gkup

New member
Forgive my ignorance in electronics. I know (next to) nothing about all this. I have however altered some example code to do what I want it to do. It works great! This is a simple MIDI sketch that uses 5 buttons and a toggle switch to send program changes. When the toggle switch is enabled it changes what program changes are sent.Thus giving me 10 different program changes. I have the on-board led light turning on when I turn on the toggle switch letting me know I'm in the 2nd bank of programs. My question is, do I need some sort of resister when using a regular LED light? I have some left over LEDs from an Arduino UNO kit that I got about a year ago and never used. They have 2 long leads on them. One shorter than the other. Can I just connect the ground and pin 13 to the LED and make this work? I appreciate any help you can give this novice. I'm using a Teensy 3.2. Here is the code I've hobbled together. Thanks.

/* Simple Teensy DIY USB-MIDI controller.
Created by Liam Lacey, based on the Teensy USB-MIDI Buttons example code.

Contains 8 push buttons for sending MIDI messages,
and a toggle switch for setting whether the buttons
send note messages or CC messages.

The toggle switch is connected to input pin 0,
and the push buttons are connected to input pins 1 - 8.

You must select MIDI from the "Tools > USB Type" menu for this code to compile.

To change the name of the USB-MIDI device, edit the STR_PRODUCT define
in the /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/cores/usb_midi/usb_private.h
file. You may need to clear your computers cache of MIDI devices for the name change to be applied.

See https://www.pjrc.com/teensy/td_midi.html for the Teensy MIDI library documentation.

*/
//The number of push buttons. I've changed this to use just 5 buttons instead of 8.
//Had to skip pins 2,4 and 6 due to poor soldering skills. lol.

#include <Bounce.h>
const int ledPin = 13;
const int NUM_OF_BUTTONS = 8;
const int MIDI_CHAN = 10;
const int DEBOUNCE_TIME = 15;

Bounce buttons[NUM_OF_BUTTONS + 1] =
{
Bounce (0, DEBOUNCE_TIME),
Bounce (1, DEBOUNCE_TIME),
Bounce (2, DEBOUNCE_TIME),
Bounce (3, DEBOUNCE_TIME),
Bounce (4, DEBOUNCE_TIME),
Bounce (5, DEBOUNCE_TIME),
Bounce (6, DEBOUNCE_TIME),
Bounce (7, DEBOUNCE_TIME),
Bounce (8, DEBOUNCE_TIME)
};

const int MIDI_MODE_ONE = 0;
const int MIDI_MODE_TWO = 1;

//Variable that stores the current MIDI mode of the device (what type of messages the push buttons send).
int midiMode = MIDI_MODE_ONE;

//Arrays the store the exact note and CC messages each push button will send.
const int MIDI_ONE_NUMS[NUM_OF_BUTTONS] = {11, 0, 12, 0, 13, 0, 14, 15};
const int MIDI_ONE_VALS[NUM_OF_BUTTONS] = {127, 127, 127, 127, 127, 127, 127, 127};
const int MIDI_TWO_NUMS[NUM_OF_BUTTONS] = {1, 0, 2, 0, 3, 0, 4, 5};
const int MIDI_TWO_VALS[NUM_OF_BUTTONS] = {127, 127, 127, 127, 127, 127, 127, 127};

//const int MIDI_NOTE_NUMS[NUM_OF_BUTTONS] = {10, 41, 42, 43, 36, 37, 38, 39};
//const int MIDI_NOTE_VELS[NUM_OF_BUTTONS] = {110, 110, 110, 110, 110, 110, 110, 110};
//const int MIDI_CC_NUMS[NUM_OF_BUTTONS] = {24, 25, 26, 27, 20, 21, 22, 23};
//const int MIDI_CC_VALS[NUM_OF_BUTTONS] = {127, 127, 127, 127, 127, 127, 127, 127};

//==============================================================================
//==============================================================================
//==============================================================================
//The setup function. Called once when the Teensy is turned on or restarted

void setup()
{
// Configure the pins for input mode with pullup resistors.
// The buttons/switch connect from each pin to ground. When
// the button is pressed/on, the pin reads LOW because the button
// shorts it to ground. When released/off, 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(ledPin, OUTPUT);
for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
{
pinMode (i, INPUT_PULLUP);
}

}

//==============================================================================
//==============================================================================
//==============================================================================
//The loop function. Called over-and-over once the setup function has been called.

void loop()
{
//==============================================================================
// Update all the buttons/switch. 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.
for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
{
buttons.update();
}

//==============================================================================
// Check the status of each push button

for (int i = 0; i < NUM_OF_BUTTONS; i++)
{

//========================================
// Check each button for "falling" edge.
// Falling = high (not pressed - voltage from pullup resistor) to low (pressed - button connects pin to ground)

if (buttons[i + 1].fallingEdge())
{
if (midiMode == MIDI_MODE_ONE)
usbMIDI.sendProgramChange (MIDI_ONE_NUMS, MIDI_CHAN, 0);
//digitalWrite(ledPin, HIGH); // set the LED on
//delay(1000); // wait for a second
//}
else
usbMIDI.sendProgramChange (MIDI_TWO_NUMS, MIDI_CHAN, 0);
}

//========================================
// Check each button for "rising" edge
// Rising = low (pressed - button connects pin to ground) to high (not pressed - voltage from pullup resistor)

} //for (int i = 0; i < NUM_OF_BUTTONS; i++)

//==============================================================================
// Check the status of the toggle switch, and set the MIDI mode based on this.
if (buttons[0].fallingEdge())
{
midiMode = MIDI_MODE_ONE;
digitalWrite(ledPin, HIGH); // set the LED on
//delay(200);
}
else if (buttons[0].risingEdge())
{
midiMode = MIDI_MODE_TWO;
digitalWrite(ledPin, LOW); // set the LED off
//delay(200);
}

//==============================================================================
// MIDI Controllers should discard incoming MIDI messages.
// http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
while (usbMIDI.read())
{
// ignoring incoming messages, so don't do anything here.
}

}
 
Last edited:
Pin number 13 is already "tied" to the onboard LED of Teensy 3.2. So it will always engage the onboard LED.
That said, you can still add another (external) LED to pin 13.
LEDs need series resistors to limit the current flowing through them. The value of the required R depends on the LED specifications; anyway, a usually safe bet is something like 220-470 Ohm.
So, you may connect the longer lead of your LED to pin 13, the shorter lead of the LED to one lead of a 220-470 Ohm resistor, and the other lead of the resistor to Ground.
 
Thank you

Pin number 13 is already "tied" to the onboard LED of Teensy 3.2. So it will always engage the onboard LED.
That said, you can still add another (external) LED to pin 13.
LEDs need series resistors to limit the current flowing through them. The value of the required R depends on the LED specifications; anyway, a usually safe bet is something like 220-470 Ohm.
So, you may connect the longer lead of your LED to pin 13, the shorter lead of the LED to one lead of a 220-470 Ohm resistor, and the other lead of the resistor to Ground.

That answers everything for me. Thank you very much.
 
I have solved the initial problem with help from Xfer. Thank you very much. I now have a couple of other things I'd like to do. Firstly, when I power off, then power on the Teensy if the toggle switch is in the second mode position the light does not automatically turn on when it starts up. I'm using newer code to utilize a RGB led bulb to change colors according to the button I press. I'll list that code below. The other LED light turns on and off according to what mode I'm in. How can I check the mode and turn on the 2nd mode led before the loop on startup. I tried placing the mode check code before the loop but got all kinds of errors. I then tried a boolean statement and I just don't know how to implement that (obviously) in the Arduino environment. I come from a VB background and in VB I would simply use something like this.

Dim Pressed as boolean
If Pressed = true then
turn the light on
else
turn the light off.
end if

I just don't know how to do that in the Arduino environment. I appreciate any help you can give.


//Had to skip buttons 2,4 and 6 due to poor soldering skills. lol.
#include <Bounce.h>
const int ledPin = 13;
const int NUM_OF_BUTTONS = 8;
const int MIDI_CHAN = 10;
const int DEBOUNCE_TIME = 35;
const int redPin = 20;
const int greenPin = 21;
const int bluePin = 22;

Bounce buttons[NUM_OF_BUTTONS + 1] =
{
Bounce (0, DEBOUNCE_TIME),
Bounce (1, DEBOUNCE_TIME),
Bounce (2, DEBOUNCE_TIME),
Bounce (3, DEBOUNCE_TIME),
Bounce (4, DEBOUNCE_TIME),
Bounce (5, DEBOUNCE_TIME),
Bounce (6, DEBOUNCE_TIME),
Bounce (7, DEBOUNCE_TIME),
Bounce (8, DEBOUNCE_TIME)
};

const int MIDI_MODE_ONE = 0;
const int MIDI_MODE_TWO = 1;
const int MIDI_ONE_NUMS[NUM_OF_BUTTONS] = {11, 0, 12, 0, 13, 0, 14, 15};
const int MIDI_TWO_NUMS[NUM_OF_BUTTONS] = {1, 0, 2, 0, 3, 0, 4, 5};
int midiMode = MIDI_MODE_ONE;

void setup()
{
// Configure the pins for input mode with pullup resistors.
// The buttons/switch connect from each pin to ground. When
// the button is pressed/on, the pin reads LOW because the button
// shorts it to ground. When released/off, 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(ledPin, OUTPUT);
for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
{
pinMode (i, INPUT_PULLUP);
}
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

} //for (int i = 0; i < NUM_OF_BUTTONS; i++)

void loop()
{
for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
{
buttons.update();
}

for (int i = 0; i < NUM_OF_BUTTONS; i++)
{

if (buttons[i + 1].fallingEdge())
{
if (midiMode == MIDI_MODE_ONE)
usbMIDI.sendProgramChange (MIDI_ONE_NUMS, MIDI_CHAN, 0);
else
usbMIDI.sendProgramChange (MIDI_TWO_NUMS, MIDI_CHAN, 0);
}

} //for (int i = 0; i < NUM_OF_BUTTONS; i++)

if (buttons[0].fallingEdge())
{
midiMode = MIDI_MODE_ONE;
digitalWrite(ledPin, HIGH); // set the LED off
analogWrite(redPin, 10);
analogWrite(bluePin, 10);
analogWrite(greenPin, 0);
}

else if (buttons[0].risingEdge())
{
midiMode = MIDI_MODE_TWO;
digitalWrite(ledPin, LOW); // set the LED on
analogWrite(redPin, 10);
analogWrite(bluePin, 10);
analogWrite(greenPin, 0);
}
//----------------My RGB light code-------------------------------------
if (buttons[1].fallingEdge())
{
analogWrite(redPin, 500);
analogWrite(bluePin, 0);
analogWrite(greenPin,0);
}
else if (buttons[3].fallingEdge())
{
analogWrite(redPin, 0);
analogWrite(bluePin, 0);
analogWrite(greenPin, 500);
}
else if (buttons[5].fallingEdge())
{
analogWrite(redPin, 0);
analogWrite(bluePin, 500);
analogWrite(greenPin, 0);
}
else if (buttons[7].fallingEdge())
{
analogWrite(redPin, 500);
analogWrite(bluePin, 0);
analogWrite(greenPin, 500);
}
else if (buttons[8].fallingEdge())
{
analogWrite(redPin, 100);
analogWrite(bluePin, 100);
analogWrite(greenPin, 100);
}
//-----------------------------------------------

while (usbMIDI.read())
{
}
}
 
Back
Top