Go Down

Topic: Problem with sending MIDI CC messages (Read 4695 times) previous topic - next topic

yan_g

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:

Code: [Select]

// 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);
}

Dnstje

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
Quote

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)

Code: [Select]

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);
}




yan_g


yan_g

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

yan_g

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

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:

Code: [Select]
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.

Grumpy_Mike

#6
Aug 13, 2012, 06:04 pm Last Edit: Aug 13, 2012, 06:10 pm by Grumpy_Mike Reason: 1
Make this line:-
Code: [Select]
int cc = AnalogValue[i]/8;
say
Code: [Select]
cc = AnalogValue[i]/8;
and add
Code: [Select]
int cc = -1
outside the loop function

also make the if into:-
Code: [Select]
// check if analog input has changed
    if ( abs(cc - lastAnalogValue[i]) > 2 ) {

#7
Oct 04, 2012, 02:29 pm Last Edit: Oct 04, 2012, 02:42 pm by HANKENSTIEN Reason: 1
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
Code: [Select]
// 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: [Select]
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; [/i] instead of int input_nb = 2; [/i] It would not compile.
So I added the int midiCCselect[6] = {22,23,24,25,26,27}; [/u]  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?



Nantonos

#8
Oct 04, 2012, 06:17 pm Last Edit: Oct 04, 2012, 06:32 pm by Nantonos Reason: 1
Quote
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?

Code: [Select]
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.
Code: [Select]
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.
Code: [Select]
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.
Code: [Select]
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.

#10
Oct 08, 2012, 06:16 am Last Edit: Oct 08, 2012, 06:54 am by HANKENSTIEN Reason: 1
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?

Nantonos

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.

Code: [Select]
#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

Go Up