Sustain via midi

I lifted the following code from the forum, related to generating a sustaining code “CC64” for midi. However using 1.6.3 I have the following errors. Any help here would be appreciated:

Arduino: 1.6.3 (Windows 7), Board: “Arduino Nano, ATmega328”

sketch_may04a_logan_midi.ino: In function ‘void setup()’:

sketch_may04a_logan_midi.ino:37:3: error: ‘MIDI’ was not declared in this scope

sketch_may04a_logan_midi.ino: In function ‘void loop()’:

sketch_may04a_logan_midi.ino:54:5: error: ‘MIDI’ was not declared in this scope

sketch_may04a_logan_midi.ino:56:5: error: ‘MIDI’ was not declared in this scope

Error compiling.

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

Sustain_midi.ino (1.14 KB)

Use Tools|Auto Format and you will find that the last two if statements and the delay are outside the loop function. The if statement on line 33 isn't formatted in the same way as the others - it is missing an opening brace.


That was helpful, thank you, but the code block doesn't work. Still looking for a solution to send midi sustain command on a button push. Thanks

There was a missing “{” in one of the lines, pretty simple fix. I attached the corrected code. Compiles fine with 1.6.3 here now.

Sustain_midi_Fix.ino (1.15 KB)

That code sends sustain or sostenuto continuously depending on the state of the two buttons.
You need to modify it so that it only sends the MIDI message when the state of the toggleSwitch changes from LOW to HIGH.
Also, do you have an external pullup resistor on pins 2 and 4? If not, the input will float and you'll get random readings. It's best to enable the internal pullups:

 pinMode(toggleSwitch, INPUT_PULLUP);
  pinMode(togglePedal, INPUT_PULLUP);

And a minor fix, if you change the value of midiOut, the program will still output on channel one because it is hard coded. Use this in setup:



I made that change but still no joy! and Yes, I have a pull up on 2, where my focus is, and grounded 4. I tested the 1.6.3 communications, midi, example and that played fine. (scale loop) I tried the "Arduino_MIDI_Library_v4.2" but was unable to get any of the example files to play with out a whole host of errors, so I reverted back to "Arduino_MIDI_Library_v3.2" and now looking into the " MIDI.sendControlChange(64,127,1);" but still with no success. Going to call Yamaha, and see if they can enlighten me.

When I was working with the Yamaha Arranger style keyboards, I had to set up the MIDI IN "MIDI Receive" for all the "parts" and "extras" in the Yamaha's MIDI settings (each on their own channel) and then send the SUSTAIN CC to all 16 channels. Otherwise only the item on channel 1 will be affected.

Least that was the arranger (PSRs950) series of doing it. I suspect Yammie doesn't think much out of the box so I'd expect something similar for the rest of their line. I was controlling them via a Behringer FCB1010.

It was actually easier to use a relay from the Arduino and then the 1/4" jack on the Yamaha keyboard for the Sustain.

Thanks, I'm testing with a yammie, because that is what I had in the garage. Actually trying to use arduino to build a sustain pedal for a microkorg-xl, which does not have a sustain pedal input? A gross oversight. Not being farmiliar with MIDI, the curve was a little steeper than I envisioned. I know that I'm talking to the digital keyboard using the arduino,com,midi, example, but as for configuring midi's "CC#" prefix this remains a bit of a mystery. I believe there is a code prefix for the "control change" or CC# before I send the "64" value"127" and channel"1" structure. I've just not uncovered the magic nugget for that prefix. Something needs to tell midi, what follows is a control change. I have not found it, or probably over thinking it.

I believe there is a code prefix for the "control change" or CC# before I send the "64" value"127" and channel"1" structure.

The command code for control change is generated by the MIDI.sendControlChange function.


What library is that associated with?

I have been using the "Tom Igoe" example found under Communications-Midi. That works, just not with sustain.

I downloaded the "Arduino_MIDI_Library_v4.2" but it was crashing left and right using just the examples, so I reverted back to the "Arduino_MIDI_Library_v3.2" which is stable and complies, but could not get it to communicate. I tried flirting with" MIDI.sendControlChange(64,127,1);" but no success. I will revisit it. Thanks

Have you read the MIDI implementation document for the Yamaha keyboard you’re trying to control? While CC 64 might be universal, I found that Korg and Yamaha’s implementation of it is significantly different on some models.

For example to get the PSRs950 to SUSTAIN, I had to setup the 950’s MIDI IN section first to dedicate a MIDI channel for the upper, lower, rhythms, drums, extras, etc. Otherwise sending it on channel 1 sustain only affected the upper keyboard.

I was controlling it with a MIDI pedal from an FCB1010 and it was sending out CC#27, thus, when I got a CC#27 from the FCB1010 I’d then send out a CC64:

void transPose(byte ccNumber, byte theValue) {
 for (int theChannel=0 ; theChannel < 14 ; theChannel++) {
   MIDI.sendControlChange(ccNumber, theValue, theChannel);

MIDI is very easy, the various companies implementation of it usually isn’t. Takes a lot of trial and error to figure it out. It’s never been as easy as simply sending out a CC and having something happen. Except for a Casio I was working on once a long time ago.

On the microkorg-xl, you’ll have to set up the MIDI routing, the the Global and/or MIDI Filter section, the channel so it’s not MIDI input deaf, and then probably the Control Change section. Once all that’s done correctly, you transmit CC#64,0 DAMPER OFF, CC#64, 127 DAMPER ON.

Plan on spending lots of time with the User Manual and perhaps the MIDI implementation document.

That's good to know. At this point the simplest of loops , is just playing the notes without the sustain, or damper. I did verify CC# 64 is the documented code. Maybe I'll run across town this week end and see how the korg behaves.

void loop()
MIDI.sendNoteOn(60,127,1); // Send a Note (pitch 60, velo 127 on channel 1)
MIDI.sendControlChange(64,0,1); // Sustain, channel 1
digitalWrite(ledPin, LOW);
delay (5000);

MIDI.sendNoteOn(66,127,1); // Send a Note (pitch 66, velo 127 on channel 1)
MIDI.sendControlChange(64,0,1); // Sustain, channel 1
// delay(10);
digitalWrite(ledPin, HIGH);
delay (5000);

Just to close this out, I purchased an el-cheapo usb-midi interface and used midi-ox to monitor the output of the arduino board. As it turns out my code is working fine and sending the proper sequence to the keyboard. However the keyboard is not recognizing it from the arduino only. If I wire the arduino thru the midi-ox and monitor its behavior into the keyboard the keyboard receives the sustain as expected. So there is something in the protocol that the usb device is using that is not documented or not obvious.

Depending on the channel, this works.

if (Sustain == 1)
digitalWrite(ledPin, HIGH);
Serial.write(176); //CC change command (0xB0 - 0xBF depending on the MIDI channel)
Serial.write(64); //sustain command
Serial.write(127); //value (railed)

So my guess is you’re not using a proper MIDI interface board between the Arduino and the Korg. The one I have used numerous times is this one:

A more elaborate version of the same one, built and ready to go can be obtained from this source:

At this moment in time I am building a floorboard controller for a birthday gift. My MIDI interface uses the schematic above and I built it on a strip board:

Connected up to an UNO, along with a 2.4" TFT for my display, a I2C extender for reading the buttons from the floorboard, it works perfectly.

I use the hardware serial port for the MIDI interface (TX/RX). I supply Vcc and GND to the interface board via pins 2 and 3 from the Arduino. So I can upload a sketch without disconnecting the MIDI interface but I get to use the Arduino hardware UART instead of SoftwareSerial and that’s more reliable.

Of course the setup as I have it means I can’t do debugging via the Serial pins because you can’t use MIDI and Serial on the same port. It’s one or the other. When I need to show something, I just use the TFT display.

As I said MIDI is easy, couple a MIDI interface board to the Arduino and it’s a great little processor/controller. If you only need to SEND MIDI, you can make the circuit even smaller but it does limit it so you can never receive MIDI.

This was supposed to be an easy intro to arduino, but that has not been the case. I played with the midi libraries with no success. I came back to the original "Communication, midi, example" using the serial port approach.
modified 13 Aug 2012
by Tom Igoe
I can talk to the keyboard using the built in sample code. I modified it to send the CC# sustain code. I am able to replicate the behavior that I see when the mido-ox communicates with the keyboard, and can get the desired results when passing thru the midi-ox from the arduino. Meaning I can generate the sustain, see it transmitted and keyboard responds.

Just can't get arduino to talk to the keyboard unassisted. The midi protocol, is not complicated and well documented. Arduino, on the other hand I find it easy to get off track and pick up halfbaked libraries or old ones, with marginal incomplete code examples and get lost in the weeds.

Do I need a read, because I was under the impression, that I was in a send only midi mode. On your read, why do you need and opto?

I've never done any MIDI serial style like Tom's sketch, I've always used the MIDI library. And I've always either used a MIDI shield or built my own MIDI I/O hardware.

Debugging could be difficult because MIDIOX might clean up some slop in the background that you're unaware of, and sends properly formatted MIDI to the Korg to make it work. Unless you wrote MidiOX, you have no idea what it's doing to help out.

Personally, I'd scrap whatever you're doing, build a proper balanced MIDI OUT port, connect that to the Korg with a MIDI cable, use the MIDI Library with the MIDI_BASIC_IO and get it working. That shouldn't take more than about 5 minutes of your time.

In regards to MIDI I/O, if you're building a one way controller you don't need a MIDI IN. I.e. a typical open loop system. In an open loop system you have no idea if the MIDI actually did anything or not because it's a dumb controller and receives no feedback from the host. An open loop system doesn't even know if it's connected to a host. Really.

I build closed loop MIDI controllers. When I make a change on the Arduino, the host it's connected to will reflect that change and I can act on it accordingly (change my display, light up an LED, send more commands, etc). That's why I build MIDI IN ports.

As for the Opto on the MIDI IN, well, 5 seconds in Google would get you to the MIDI ORG electrical specification page and all is made abundantly clear:

And yes, you'll see many "other" ways of doing it (I call it fudging) by guys who think they know better. But my gear is valuable to me so I avoid ground loops and protect it with a simple Opto.