View Full Version : Extra '}' in USBMIDI example on website

02-11-2014, 11:55 PM

in the transmit example, I think there is an extra '}' after the last button3 if statement

#include <Bounce.h> // Bounce library makes button change detection easy
const int channel = 1;

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
void setup() {
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);

void loop() {
// Note On messages when each button is pressed
if (button1.fallingEdge()) {
usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
if (button2.fallingEdge()) {
usbMIDI.sendNoteOn(61, 99, channel); // 61 = C#4
if (button3.fallingEdge()) {
usbMIDI.sendNoteOn(62, 99, channel); // 62 = D4
// Note Off messages when each button is released
if (button1.risingEdge()) {
usbMIDI.sendNoteOff(60, 0, channel); // 60 = C4
if (button2.risingEdge()) {
usbMIDI.sendNoteOff(61, 0, channel); // 61 = C#4
if (button3.risingEdge()) {
usbMIDI.sendNoteOff(62, 0, channel); // 62 = D4

// MIDI Controllers should discard incoming MIDI messages.
while (usbMIDI.read()) {

02-11-2014, 11:58 PM
also - I am confused as to what exactly usbMIDI.read() is doing. Is it necessary if I'm only using the teensy to transmit MIDI data from button presses? I am not receiving anything


02-12-2014, 05:07 AM
I haven't looked at the midi stuff but at a guess I'd leave usbMIDI.read() in there as harmless seeing as you are not interested in what it is reading but (guessing here) you don't know if it is clearing a buffer which may otherwise overflow untidily - doubtful it overflows untidily but erring on the side of caution is usually more productive than not.

That closing brace is the partner to the one in the line
void loop() {A simple enough experiment is to try compiling it with and without the closing brace - unless my eyes are rather more tired than I think they are the code can compile with the brace intact and will not compile without it.

02-12-2014, 05:24 AM
yeah I will just keep it in there for now and maybe its role will become more clear with time.

I tried compiling the code as is and it wouldn't, and then after clearing that bracket - it did. The closing for the loop() is actually after the while(usbMIDI.read())
(or before??) Having deleted the bracket I mentioned, the code works with the while() within the loop, so I guess I'll just keep it like this

thanks so much!

02-12-2014, 05:33 AM
sorry - "I will just keep 'it' in there..." was in reference to usbMIDI.read()

02-12-2014, 06:26 AM
I understood that you were referring to the .read()

Turns out my eyes were tired. Sorry, I didn't even look properly at the lines after the brace you indicated with red/bold, I saw the // start of a comment line and the basic shape of what I decided was just an empty (thought of but not yet written) function which had all its braces too :o

I should have gone to the page (you linked to it and everything (2*:o)) you were talking about and looked at it with all the neat indentation; looks like the addition of discarding incoming messages might have been an after-thought addendum and it should have been written before the brace you indicate instead of after it with another brace following.

02-12-2014, 11:18 AM
also - I am confused as to what exactly usbMIDI.read() is doing. Is it necessary if I'm only using the teensy to transmit MIDI data from button presses? I am not receiving anything

You don't want to receive anything. Regardless, you need to deal with anything being sent (by discarding it, not leaving it to accumulate) because whatever you are talking to may talk back. See this thread for more details (http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash?highlight=discard+incoming+MIDI+messages) (yes, failing to discard could cause a crash in some cases).

02-21-2014, 05:54 PM
Just joined and am a teensy noob. I mailed Paul about this yesterday after reading the MIDI example being discussed. I had used vi's '%' key (which finds matching braces, parenthesis) to confirm that the red brace above was the match for the loop function's open brace, meaning (I assumed) the code below it was outside loop{} and would never execute. I hadn't tried to compile it, but it makes sense that it failed for zalterman (thanks!).

Paul - if we're correct, can you please remove the red brace to close this out? Many thanks!

04-04-2017, 12:54 PM
Just a quick followup to this very old thread... the web page has been updated.