Yes, I do have te Rugged Circuits Flexible MIDI shield, although after realising that it was not (despite what the product page says) actually compatible with the Mega, I had stopped trying to use it because it would not fit my current project.
Anyway I also have a UNO so I took your code and ran it on the UNO with the RC MIDI shield. I made some changes to tid things up, corect types, what seemed to e to be better vatriable names, more comments with links. And I actually used the MIDI Library (your code includes MIDI.h but does not actually call any of the functons in the library).
I ran it, connecting MIDI Out to the MIDI In on by audio interface and then using MIDI OX to view the results. This was the code:
// Read multiple potentiometers and send the results over MIDI out.
// from
// http://arduino.cc/forum/index.php/topic,117264.0.html
// adapted to actually use the MIDI library :)
#include <MIDI.h>
// MIDI library
// class reference http://arduinomidilib.sourceforge.net/a00001.html
// download http://arduinomidilib.sourceforge.net/files.html
#define COMPILE_MIDI_IN 0 // Set to 1 to use MIDI input.
#define COMPILE_MIDI_OUT 1 // Set to 1 to use MIDI output.
#define COMPILE_MIDI_THRU 0 // Set out and in to 1, and this to 1, enables MIDI passthru
// Values for Rugged Cicuits Flexible MIDI sheild
// http://ruggedcircuits.com/html/flexible_midi_shield.html
// Jumpers:
// Rx on D0
// Tx on D1
// MIDI enable on D12
// MIDI enable pin, avoids conflict of Serial between USB for programming and DIN MIDI I/O
#define MIDI_ENABLE 12
// Variables:
static int input_nb = 2; // select number of desired analog inputs (max 6 for Uno)
int AnalogValue[2] = {0,0}; // raw analog readings
byte lastCValue[2] = {255,255}; // previous controller values
// select the midi Controller Number for each input (22 to 31 are free)
// for list, see
// http://www.midi.org/techspecs/midimessages.php#3
byte midiCCselect[2] = {22,23};
byte cc; // Analog reading converted to 7 bit value
byte channel = 1; // MIDI channel number from 1 (not 0) to 16
void setup() {
// launch MIDI
MIDI.begin();
// enable MIDI
pinMode(MIDI_ENABLE, OUTPUT);
digitalWrite(MIDI_ENABLE, HIGH);
}
void loop() {
for (int i =0; i < input_nb; i++) {
// Arduino ADC gives values in range 0 to 1023
AnalogValue[i] = analogRead(i);
// convert to a control value, range from 0 to 127, for MIDI:
cc = AnalogValue[i]/8;
// check if control value has changed from last time
if (cc != lastCValue[i] ) {
//send control change on cc#i
MIDI.sendControlChange(midiCCselect[i], cc, channel);
// update last controller value to current one
lastCValue[i] = cc;
} // end if
} // end for
}
Aaaand - no output whatsoever.
To check the program flow I then made a debug version - instead of outputing MIDI it send lots of chatty debug messages to Serial. This is the code:
// Read multiple potentiometers and send the results over MIDI out.
// from
// http://arduino.cc/forum/index.php/topic,117264.0.html
// adapted to actually use the MIDI library :)
#include <MIDI.h>
// MIDI library
// class reference http://arduinomidilib.sourceforge.net/a00001.html
// download http://arduinomidilib.sourceforge.net/files.html
#define COMPILE_MIDI_IN 0 // Set to 1 to use MIDI input.
#define COMPILE_MIDI_OUT 1 // Set to 1 to use MIDI output.
#define COMPILE_MIDI_THRU 0 // Set out and in to 1, and this to 1, enables MIDI passthru
// Values for Rugged Cicuits Flexible MIDI sheild
// http://ruggedcircuits.com/html/flexible_midi_shield.html
// Jumpers:
// Rx on D0
// Tx on D1
// MIDI enable on D12
// MIDI enable pin, avoids conflict of Serial between USB for programming and DIN MIDI I/O
#define MIDI_ENABLE 12
// Variables:
static int input_nb = 2; // select number of desired analog inputs (max 6 for Uno)
int AnalogValue[2] = {0,0}; // raw analog readings
byte lastCValue[2] = {255,255}; // previous controller values
// select the midi Controller Number for each input (22 to 31 are free)
// for list, see
// http://www.midi.org/techspecs/midimessages.php#3
byte midiCCselect[2] = {22,23};
byte cc; // Analog reading converted to 7 bit value
byte channel = 1; // MIDI channel number from 1 (not 0) to 16
void setup() {
// launch MIDI
//MIDI.begin();
Serial.begin(57600);
// enable MIDI
//pinMode(MIDI_ENABLE, OUTPUT);
//digitalWrite(MIDI_ENABLE, HIGH);
}
void loop() {
for (int i =0; i < input_nb; i++) {
// Arduino ADC gives values in range 0 to 1023
AnalogValue[i] = analogRead(i);
Serial.print("AnalogValue[");
Serial.print(i, DEC);
Serial.print("] = ");
Serial.println(AnalogValue[i]);
// convert to a control value, range from 0 to 127, for MIDI:
cc = AnalogValue[i]/8;
Serial.print("cc = ");
Serial.println(cc, HEX);
// check if control value has changed from last time
if (cc != lastCValue[i] ) {
//send control change on cc#i
Serial.println("Changed ===============================");
//MIDI.sendControlChange(midiCCselect[i], cc, channel);
// update last controller value to current one
lastCValue[i] = cc;
} // end if
} // end for
delay(2000);
}
Result - exactly as expected. Code is fine, its just that no MIDI is being sent.
Coincidentally, today my Teensy 3.0 (ARM-based Arduino-compatible) and Teensy 2.0 (AVR-based Arduino compatible) arrived in the post. These have the handy property that as well as using the USB to upload programs, you cn also set it to be a USB keyboard, or a USB mouse, or a generic USB HID device ... or USB MIDI. So I ran the same code on Teensy 2.0, set up as USB MIDI. Here is the code:
// Read multiple potentiometers and send the results over MIDI out.
// from
// http://arduino.cc/forum/index.php/topic,117264.0.html
// adapted to run on Teensy with usbMIDI feature
// http://www.pjrc.com/teensy/td_midi.html
// Variables:
static int input_nb = 2; // select number of desired analog inputs (max 6 for Uno)
int AnalogValue[2] = {0,0}; // raw analog readings
byte lastCValue[2] = {255,255}; // previous controller values
// select the midi Controller Number for each input (22 to 31 are free)
// for list, see
// http://www.midi.org/techspecs/midimessages.php#3
byte midiCCselect[2] = {22,23};
byte cc; // Analog reading converted to 7 bit value
byte channel = 1; // MIDI channel number from 1 (not 0) to 16
void setup() {
// launch MIDI by selecting USB MIDI in the Arduino IDE with Teensyduino installed
// http://www.pjrc.com/teensy/teensyduino.html
}
void loop() {
for (int i =0; i < input_nb; i++) {
// Arduino ADC gives values in range 0 to 1023
AnalogValue[i] = analogRead(i);
// convert to a control value, range from 0 to 127, for MIDI:
cc = AnalogValue[i]/8;
// check if control value has changed from last time
if (cc != lastCValue[i] ) {
//send control change on cc#i
usbMIDI.sendControlChange(midiCCselect[i], cc, channel);
// update last controller value to current one
lastCValue[i] = cc;
} // end if
} // end for
}
Result - MIDI output to MIDI OX displayed as expected.
I was running the Teensy on a breadboard, maybe that is a bit noisy, because I saw some +- 1 variability in the 7 bit data (so, lots of MIDI messages with very similar values) while on Arduino I was getting genuine 10-bit data with +-1 or maybe 2 variability (so, no actual change when reduced down to 7-bit valuses for MIDI).
But apart from that, the Teensy 2.0 used as USB MIDI just worked as expected, while I couldn't get the RC MIDI sheild using DIN MIDI to either input or output correctly to MIDI OX. Since USB MIDI is faster than the DIN version, as well, this seems like a win all round and I plan to use the Teensy 2.0 in my controller project and consign the Rugged Circuits MIDI shield to the parts bin.
I did not observe the variability that you mentioned. No erratic values at all.
I wonder if you have the pots connected to ground correctly or if you are using them in a very elecvtrically noisy environment?