Problem with sending MIDI CC messages

Hello, I've written a sketch that is supposed to send MIDI CC messages from 2 pots. I wanted to use controls 21 & 22 (chose them because of that list).

The music software I use allows mapping devices parameters (such as a synth's filter frequency) to MIDI controllers according to the messages it receives. The problem is I receive messages from seemingly random controls between 0 and 119, instead of the 21 & 22 I specified in my code. I know very little (if any) about MIDI and didn't find any dummy-friendly info on the web...

I outputted the variables to the serial monitor and found nothing strange. Can anybody help me out?

Here is my code:

// Variables:
int input_nb = 2; // select number of desired analog inputs (max 6)
int AnalogValue[2] = {0,0}; // define variables for the controller data
int lastAnalogValue[2] = {0,0}; // define the "lastValue" variables
int midiCCselect[2] = {22,23}; // select the midi Controller Number for each input (22 to 31 are free)
int thresh[2] = {1,1}; // select threshold for each analog input

void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250); // 31250
}

void loop() {
  for (int i =0; i < input_nb; i++) {
    //  My potentiometer gave a range from 0 to 1023:
    AnalogValue[i] = analogRead(i);
    //  convert to a range from 0 to 127:
    int cc = AnalogValue[i]/8;

    // check if analog input has changed
    if (cc != lastAnalogValue[i] ) {
      //send control change on cc#i
      midiCC(0xB0, midiCCselect[i], cc);
      //Serial.println(String(midiCCselect[i])+": "+String(cc));
      // update lastAnalogValue variable
      lastAnalogValue[i] = cc;
    }

  }  //  endfor
}

// sends a Midi CC. 
void midiCC(byte CC_data, byte c_num, byte c_val){
  Serial.write(CC_data);
  Serial.write(c_num);
  Serial.write(c_val);
}

I had the same problem with something similair on my project too when using Arduino 1.0 and later.
Adding (byte) will help the compiler as sending as a byte instead sending a high order byte and a low byte.

From the post which PaulS helped me

Serial.read() returns an int, with the error value in the high order byte, and the data in the low order byte. If you know that the Serial.read() function will not return an error, and you should because you have called Serial.available() and are only calling Serial.read() when there is something to read, then you should be storing the result in a byte sized variable, not a multibyte variable.

After that I began reading the changelog of Arduino >1.0 what actually has changed.

Asfar as I know this worked for my project adding (byte)

void midiCC(byte CC_data, byte c_num, byte c_val){
  Serial.write((byte)CC_data);
  Serial.write((byte)c_num);
  Serial.write((byte)c_val);
}

I'll try that out asap, thanks!

I just tried but there is no noticeable change... I still get erratic data on erratic cc numbers :frowning:

Allow me to bump this and express my extreme distress, I have absolutely no clue where to go from here... :frowning:

Hello. Have you tried formatting all the data bytes in the 0x.. as you did for the 0xB0 command?

What about instead of serial.write using serial.print ?

I'm doing a similar program but with digital input only (yet)

Here is my code as far as the serial comm goes:

void midiSend(char status, char data1, char data2) {
  Serial.print(status, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}

I'm using Arduino 0022, not 1.0
Regards.

Make this line:-

int cc = AnalogValue[i]/8;

say

cc = AnalogValue[i]/8;

and add

int cc = -1

outside the loop function

also make the if into:-

// check if analog input has changed
    if ( abs(cc - lastAnalogValue[i]) > 2 ) {

Hello, And thank you you to all who can help, and hopefully I WE can help others as well and eventually turn this into a tutorial, so its Pretty thorough.
I have been trying to get my home brew midi controller to work for a little while now, and this seems like the best code yet ay least for me. I use Arduino IDE V0.23 or V1.0

// Variables:
int input_nb = 6; // select number of desired analog inputs (max 6)
int AnalogValue[6] = {0,0,0,0,0,0}; // define variables for the controller data
int lastAnalogValue[6] = {0,0,0,0,0,0}; // define the "lastValue" variables
int midiCCselect[6] = {22,23,24,25,26,27}; // select the midi Controller Number for each input (22 to 31 are free)
int thresh[6] = {1,1,1,1,1,1}; // select threshold for each analog input

void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250); // 31250
}

void loop() {
  for (int i =0; i < input_nb; i++) {
    //  My potentiometer gave a range from 0 to 1023:
    AnalogValue[i] = analogRead(i);
    //  convert to a range from 0 to 127:
    int cc = AnalogValue[i]/8;

    // check if analog input has changed
    if (cc != lastAnalogValue[i] ) {
      //send control change on cc#i
      midiCC(0xB0, midiCCselect[i], cc);
      //Serial.println(String(midiCCselect[i])+": "+String(cc));
      // update lastAnalogValue variable
      lastAnalogValue[i] = cc;
    }

  }  //  endfor
}

// sends a Midi CC. 
void midiCC(byte CC_data, byte c_num, byte c_val){
  Serial.write((byte)CC_data);
  Serial.write((byte)c_num);
  Serial.write((byte)c_val);
}

I have also tried cesarferrolho;s code, and that doesn't seem to change anything

Code:

void midiSend(char status, char data1, char data2) {
  Serial.print(status, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}

I am using (5) 5k potentiometer and (1) 10k switch/button all to analog ins, but I still cannot get it to work. I do get some info on the serial terminal, but of course its just a bunch of gibberish, and I'm sure part of that is not being able to listen at a baud rate: of Exactly 31250 but each individual knob sends out a different type of "word" or "letters" of gibberish, and each individual knob generally sends the same gibberish each time.

In the first few lines of the original code that's on here every thing was related to 2 controls so when I added the
*int input_nb = 6; * instead of *int input_nb = 2; * It would not compile.[/u][/u]
So I added the int midiCCselect[6] = {22,23,24,25,26,27}; [/i] again It would not compile (The errors all pointed me to these areas) so I figured I OK I get the idea and made every thing in The first 5 lines of the code into a set of (6) .
* I even played around with the numbers from a chart i found that lists all the midi control number and tried plugging some of those in like 7 or 11 witch are for volume. I also tried 64 I think it was for sustain pedal, because I was trying this on my Keyboard,and separately my computer using software like Puredata (PD), hydrogen, and even got a trial of abelton witch is what everyone seems to use in tutorials on YouTube. It still did not change anything.*
I'm using a cheap USB to midi adapter and if I hook the keyboard up to the PC I can make some stuff happen.I was suspect of the USB to midi device it self, so I finally rewired and fixed my keyboard to get confirmation that it was not the USB Device. SO I do know that the USB device works and it lights up green for receive message and blue for send when I try my home made device nothing lights up. My device is similar to what you see in these pics

These images are from http://itp.nyu.edu/physcomp/Labs/MIDIOutput and I even tried to use code the on that page among others, nothing seems to work!
* The difference in the schematic is I have (5) 5k pots with their center / wipe / sens pin connected separately at A0 through A5 , but no resistors inline with the pots. Just a 220 ohm at the 5v midi connector, and a 10k at the switch and all the proper grounds. I've read that you can use any value above 1k for potentiometers for midi. also all the connections on the pots are soldered in Parallel with the wipes or sens (Middle wire) each going to a different analog in port A0~A5, and all the connections to the midi jack are soldered as well just to be sure of secure connection, the reason being is that this was a different project at one time where I used the 5 pots and a headphone out as a simple synth it's the common one you see floating around every where on youtube, Instructibles, Fritzing, and my site Rduino.com*
ANY AND ALL HELP WILL BE APPRECIATED TO GET THIS TO WORK I HAVE BUILT MUCH MORE COMPLICATED THINGS,
THIS ONE JUST WILL NOT DO ANYTHING BUT BE A PAPERWEIGHT!
Thank you for your time and effort in at least reading this please feel free with ideas im about out, ive tried code from probably 10 sites nothing works!
2 thing's I thought of while typing all this is that:
- 1. I'm only using USB power I'm not using any LED's or anything, but maybe it needs more power, do you guys find you need more power?
- 2. is it necessary to use a 10k or even a 5k because my pots are 5k's as a pulldown to ground, other schematics do not use it, but I just re-noticed it on the pic's do you guys use pulldowns?

write()

Writes binary data to the serial port. This data is sent as a byte or series of bytes

Your MIDI function expects three byte values as parameters and you are passing three (signed) ints. Maybe the language has some clever type conversion, but why not declare them that way in the first place, rather than relying on implicit conversion or explicit casts?

byte AnalogValue[6] = {0,0,0,0,0,0}; // define variables for the controller data
byte lastAnalogValue[6] = {0,0,0,0,0,0}; // define the "lastValue" variables
byte midiCCselect[6] = {22,23,24,25,26,27}; // select the midi Controller Number for each input (22 to 31 are free)

Also, there should be a 220R resistor in series between your digital out and pin 5 of the MIDI socket.

"Nantonos" Thank you for your reply's i admit some of these functions and codes confuse me, but ive never touched c code before this im using what i know from HTML Perl and PHP,and with those I know enough to get some things worcking and change variables configs etc, I can usually figure out a proublem or get evrything to talk to each other like MYSQL ect. , except HTML i know most of that,but, I must admit in your first quote on the write() Function in relation to the 3 lines you cited.
I do not understand what you mean. maybe a little more info or an example?
In fact im not even sure at all what this top line is about.

byte AnalogValue[6] = {0,0,0,0,0,0}; // define variables for the controller data

this next line as I understand it is the last vallu of the message being 0 a return to off or o out of 127 if you will.

byte lastAnalogValue[6] = {0,0,0,0,0,0}; // define the "lastValue" variables

And these I understand to be the actual program number being sent like a number 7 is reserved for volume.

byte midiCCselect[6] = {22,23,24,25,26,27}; // select the midi Controller Number for each input (22 to 31 are free)

But this is just what I am assuming from the code i got from the top of the page, also I noticed that their is a descripancy on how my midi id hooked up, ive looked at a few of the different schematics and some show what is above, but even in thhose 2 pictures the fritzing pic does not match the schematic the fritzing has or at least from the looks of it the bottom pin going to 5+ but its labeled 2? I have mine wired like this pic

I alsso have own more concern why maybe none of this is working for me at all, im wondering if according to the schematic shown above I have it connected exactly as shown assuming that your looking at the back of the midi terminal, and it occurs to me that MAYBE they are showing the front of the terminal, but the lugs are on the back, so my plus and (TX) should be switched.
See, i do not know what pin number is what, none of my connectors are numbered, SO this has just occurred to me. mine is wired so that looking at the back of the terminal mine is the all metal kind with 2 screw holes, not the pcb type so looking at the back at the solder terminals top middle is not used 1st down on left and right side are not used. 2nd pin down on the left goes to 1(tx) middle and if im correct this is where you suggested I also need a 220 resistor. The bottom pin goes to ground, and 2nd pin down on right goes to 220 ohm then 5+ remember as I did this im looking at the back, but maybe the schematic shows the front witch would make my positive and my digital out (TX) backwards or hooked to the wrong side, can anyone confirm this? how do they describe the pin #'s by looking at the front of the plug as in where you would plug in or the back as it looks like in the schematic. because somethings wrong, I cannot get this thing to do any examples from the Example sketches from V023 or V1 of the IDE If everything wasn't soldered up, id just switch them and see, but as I mentioned, most of this was already in an enclosure all soldered up, for a different project an being this was such a simple change, i only need to remove a few wires, and ad a few.Plus the box is its metal, so I even got heat shrink on some of the positive leads, and the whole board is covered in Chrysler build tape so nothing shorts out being in the metal box and all.
so if anyone can answer the pin out question first of all.
I just want to hear one sound come out or one control move, even if its the wrong one, ill know Im getting SOMEWHERE, and then i can move on to the code issue "Nantonos" suggested above, or maybe that's what i need? im at a loss Ive built way more complex thing with the arduino, and this should be simple, and I know its usually the simple things you overlook, that's what made me think of the opposite sides of the pi out because looking at the 2 pics I posted before those were right next to each other in the same tutorial, and those don't match to one another either? AS ALWAYS ANY INFO OR HELP ALWAYS APPRECIATED! THANKS FOR READING.

ON ANOTHER PAGE i FOUND THIS PIC

and if im correct at looking at his we are looking thru the port so the +5 would be the 2nd pin up from the bottom wich means mine should be in alignment, that is of course its not from the back side, that is my issue.

(EDIT A FEW MIN'S LATER)
BUT.......
Then I found a pdf with another schematic, and I copied, and cropped it then put it on my server.

Its from the same source as the other post, but, now makes me believe, that my lines are backwards, So i think im going to just have to take it all apart unsolder and re solder re solder everything just to test, and hope i dont fry my board, if i haven't already if it was hooked up wrong no actually I think it would have fried the keyboard or the usb to midi adapter first.
I hope some one can shed some light on this for me, I feel dumb, Ive done so many project with the arduino I have all 3 types, and a stupid connector has got me baffled! duhh! but We all live and learn right, so here is another PIC and although this is a plastic the pins are the same, and this one seems to show the 2 extra ground pins in the front which makes me believe im looking at it through the connector, and that i do in fact have my TX and 5V crossed, but the odd thing of this one is that it says to put the resistor a 240 not 220 on the TX I cant imagine that the difference in 220 to 240 is all that much but they do not show too put one on the positive side, witch every other schematic shows?

Pin numbering diagrams show the front of the plug, yes; not the back where you solder on the wires.

OK, so after switching the tx and the +5 thanks again "Nantonos" it actually does some stuff, but its not reliable at all, granted, i didn't solder anything yet, but for few Minuets every knob works perfect right up the row 22,23,24,25,26,and the switch 27 all worked 0-127!
then it started jumping all around, i double checked and re tightened the connections, no difference 22, still works 0-127, 23 goes from 10 to 100 instead of 0-127, 24 goes from being 24 to also Chanel 25, 25, and also the number moves from aprox 33~51~ not 0-127, 25 is completely random at what it will do, 26 works 60-110~, and 27 the switch many times does nothing? then i checked the ground wire and made another connection and it worked fine for a little while then quit.
then ill un plug it to reset every thing and all kinds of random stuff is generated then it acts similar to as I described.
but the good news is I am getting SOME messages out, but their not correct, only sometimes rarely, but usually not? WTF?
sounds like a short or a lose wire, but i checked everything even re flashed the chip. ahhh now what it like I need to completly start over from scratch. This should be so simple compared to many other ive done web server that controlled radio controlled car,s sonar sensors with distance and temp and humidity out put to lcd, servo projects, IR remote control 110v relays for wall Outlet, in light socket rf relay socket control ect, the MINI-Synth,the "trombone" the tv out with custom graphics and text and fonts, to name a few, those all took a great deal more info, parts, code, and time, this one is just my Nemesis!
BUT I JUST FOUND A PLACE WITH LOTS OF LINKS THAT MAY HELP
http://arduino.cc/forum/index.php/topic,22336.0.html

Well I Figured out the problem, or at least caused one? :~
So while tacking each section apart trying to figure out where alll the " noise" was coming from in my pots, and why signals wer jumping from one pot to another, even though im using the 3 pin setup, first of all i disconnected all the pots, and it was still sending random noise occasional, odd, while trying to figure this out I used a shield with pins and so I could hook different pots up with the 3 pin connect quickly and buttons, no difference, except after removing the shield I tried hooking up the wipe to the analog ports again rally pointless at hthis point because, I know it does not do anything, then it finally happened that familiar smell of burnt electronics. while it still lights up and gives pull power to 5v and 3.3 to 3.3 the red led lights up, so i uploaded, the simple blinky no nlink, so i added an led, in case the internal or a resistor blew, still nothing, but i figure if all the power is ok at the various voltages, that it must be the chip, so I ran the loop back test I think its called, and that worked so WTF? i then tried the basic sketch that auto fades an led on pin 9 nothing, so i pulled the nano from my ir intrusion alarm, and used the same code "fade" worked fine, I then uploaded it again to my uno and nothing, so after reading the info I could fine everything points to atmega328 so I have ordered a new one. anyone else have any other thoughts on this? or something else it could be, to me it seems to be the weakest link, especially when the power system works and the voltage regulators, the only thing that makes me worry some what that it may be something else, is the fact that it will act like its uploading code I would think their would be an error their using version 1.0 of the IDE. This was my first arduino i ever bought, and so it been through quite a bit, while learning, so Im guessing that the beginners mistakes that have been made and things like that its probably a miracle it lasted this long infact i had already ordered some new header standoff pins as a few have gotten worn out, none related to this project, but still needed to be done. Maybe it burning out was actually a good thing, and the cause of all my strange problems with the pots and the midi channels finally had a reason the chip was on its last leg? lets hope a new chip fixes it.
Thanks to those who have been following this misadventure, now any input on chip failure would be helpful....

OK, After frying the other board, im starting over, and i decided, to pick up a midi shield, to save time, and get something to work then reverse from there to make my own (eventualy), Anyway I picked up the RUGGED CIRCUITS " FLEXIBLE MIDI SHIELD" so "Nantonos" , I appreciate all of your help before, and I realize after going back through this stuff it is the same shield you were talking about. I used your code, and it would only send a note no Potentiometer wipes, so i combined the two codes, and now have it correctly allowing the use of a potentiometer, But it is still erratic, for example To make this code I used the first part of the code shown at the top, and add the define pin 12 and the require midi.h.
it works, but very erratic, when i say erratic, if i plug in 1 potentiometer it works fine, if i plug the pot into the next analog port, it will control both ports jumping between 22, and 23, no mater what numbers i give and how many "spots" i add the 1 pot controls everything at once, I want to be able to assign pot 1 to volume on track 1, and pot 2 to volume on track 2 , and once i get stuff working, even assign them to effects or whatever , im using abelton so it is very easy to assign a pot to a location, but in the end its like it all sends the same data, while programing. when i have one pot programed, and I try to program the next as i turn the knob, it will move thru the CC's like 23,24,25 etc while programing. doesn't make sense, this is with just 2 pots wired directly to plus 5 ground and one in A0 and on in A1, In the end, either pot will control everything programed at the same time..
here is sample code.

#include <MIDI.h>
// MIDI enable pin, avoids conflict of Serial between USB for programming and DIN MIDI I/O
#define MIDI_ENABLE 12

// Variables:
int input_nb = 2; // select number of desired analog inputs (max 6)
int AnalogValue[2] = {0,0}; // define variables for the controller data
int lastAnalogValue[2] = {0,0}; // define the "lastValue" variables
int midiCCselect[2] = {22,23}; // select the midi Controller Number for each input (22 to 31 are free)
int thresh[2] = {1,1}; // select threshold for each analog input

void setup() {
    // enable MIDI
  pinMode(MIDI_ENABLE, OUTPUT);
  digitalWrite(MIDI_ENABLE, HIGH);
  //  launch MIDI
  MIDI.begin();
}

void loop() {
  for (int i =0; i < input_nb; i++) {
    //  My potentiometer gave a range from 0 to 1023:
    AnalogValue[i] = analogRead(i);
    //  convert to a range from 0 to 127:
    int cc = AnalogValue[i]/8;

    // check if analog input has changed
    if (cc != lastAnalogValue[i] ) {
      //send control change on cc#i
      midiCC(0xB0, midiCCselect[i], cc);
      //Serial.println(String(midiCCselect[i])+": "+String(cc));
      // update lastAnalogValue variable
      lastAnalogValue[i] = cc;
    }

  }  //  endfor
}

// sends a Midi CC. 
void midiCC(byte CC_data, byte c_num, byte c_val){
  Serial.write(CC_data);
  Serial.write(c_num);
  Serial.write(c_val);
}

It has taken me awhile to figure out how to combine the codes, as I cannot find ANY code sources, for the rugged circuits midi shield, except what is on there website, and what you had here, but nothing about building your own "mixer" the whole idea is simple, and some pots to analog ins, and it should work, Nope, I even have a brand new arduino r3 as trying all the other suggestions from before fried my board.
so to anyone where can I find some sketches or what is wrong with either code. remember im using the rugged circuits flexible midi shield witch I thought would make thing easier, but Im no better of then b4 except now i have to add extra code that I cannot find Info on to make the shield work correctly.
What a cluster f#c^

AGAIN "Nantonos"
I KNOW YOU PROBABLY HAVE SOME INFO HERE AS I SAID I NOTICED YOU HAVE THE SAME SHIELD< IT SEEMS LIKE WE ARE THE ONLY 2? I CANT FIND HELP ON IT ANYWHERE!
Thanks for reading, and yes i did try adding the byte parts to the end of the sketch made no difference either way.
Thanks for your time
Hankenstien

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?

man I just wrote this big reply , and I accidentally hit the back button on my mouse, and lost it all.
I did get the code i posted on here last to work, I found out after buying some new pots for a different project, that all the pots i bought were garbage!, they seem to be pretty smooth with an ohm meter, but? while hooking those up, i had 5 ports working fine with various pot sizes, from 1k up to 20k and even a slider that I don't remember what the resistance value was, I had a button but it appears that it was an always on, so didn't work correctly. so I believe it was the pots, and that also answers the question of does it matter what size pot to use basically no but dont just buy the 10 for a dollar ones!
I believe one reason why the code that works for me doesn't work for you, and the code you give me doesn't work for me, maybe because your using hardware, and im using software (abelton), that's just a guess, but the code does work for me now that i have different pots, but if i can use your code, and it will still work, then i know its ready for further projects, im waiting on a new patch cable, so i can hook it to my keyboard as we speak.
What was the reason it did not work with the mega? was it because of the location of the midi (din) plugs, or was it another issue?
Im also curious as what to do now that ive used up my six analog inputs, I want to add a few buttons and if possible maybe even more pots? what sort of code is needed to add them on digital pins can I just ad more inputs, at the top of my code and add more CC #'s and use the non-PWM pins, or would I need more code as well?
I am sorry to ask so many questions, but i know that this will help many others as well, because I tried to contact the manufacturers with my code, and they would not even give me a single pointer about any of it they said sorry we do not give code support, yet they don't have much for instructions other than only one jumper in each row, and two sample sketches, witch, are not very helpful.
If you search Google for the rugged circuits flexible midi shield you get their store, these postings, and a you tube video of a guy showing of the rotator, witch he made very nice, but it is just to show his finished piece no code, no explanations etc. so basically their is no support although I did ask we shall see if anything comes of it.
so I do not even know what is specifically required in the code, and I haven even started on the midi in witch would not be part of this project anyway, so Again I thank you for your help.
I have seen the new "arm-duinos" some are even dual core etc, wow the power differences.... plus the built in capabilities, im still learning the basics b4 I jump to those, plus don't have the cash.
One last thing, before you toss your shield in the parts bin, do you have any sketches or code, you'd be willing to share, whether it be 1 or 100 I have a server and a site rduino.com , we can figure out details of how to transfer if ur willing to share, but I figure its worth an ask, before they end up deleted. even if I am the only one who owns this shield it would still be helpful to me and id make them available to all if you so wish.
Anyway thank you again for your time, It is very much appreciated, and even though I believe I have my issue figured out, I am going to study / try your code, as ideally i would like to have a hardware and software suitable code. Plus I hope to learn, thats what its all about :slight_smile:
Thanks again For your time and effort
HANKENSTIEN
P.S. I gave ya some karma point not sure what you can do with them but, Again thanks