Still unable to send CC using MIDI Library

Hello, I am struggling to understand what is going on with my code.

I try to send a control change message on channel #1, control #16. I get erratic values on erratic control numbers. I thought it might be an int to byte conversion problem but I don't even know how to check that.

Here's my code:

#include <MIDI.h>

// Variables:
int cc = 0;
int AnalogValue = 0; // define variables for the controller data
int lastAnalogValue = 0; // define the "lastValue" variables

void setup() {
  //  launch MIDI
  MIDI.begin(4);
}

void loop() {
  AnalogValue = analogRead(0);
  //  convert to a range from 0 to 127:
  cc = AnalogValue/8;
  // check if analog input has changed
  if (lastAnalogValue != cc) {
    MIDI.sendControlChange(16,cc,1);
    // update lastAnalogValue variable
    lastAnalogValue = cc;
  }  //  endif
}

I'm completely stuck and not determined to give up, so any hint, link or help of any kind would be appreciated! Thanks!

Note that control values from 120 to 127 are RESERVED for the Channel Mode Messages. You should limit your value to the range 0-119. (See: Specs)

It's the controller number, not its value, that needs to be below 120...

midi.org:
Controller numbers 120-127 are reserved as "Channel Mode Messages" (below). (ccccccc) is the controller number (0-119). (vvvvvvv) is the controller value (0-127).

I would suggest declaring cc as

byte cc = 0;

because that is the expected type for the control value:

[

void MIDI_Class::sendControlChange 	( 	byte  	ControlNumber,
		byte  	ControlValue,
		byte  	Channel 
	)

16 (General Purpose Controller 1 ) looks like a reasonable choice of controller number. It is unlikely to be mapped to anything by default however.](Arduino MIDI Library: MIDI_Class Class Reference)

Thanks,

I have actually found that my circuit was wrong :blush: when I tried my previous working MIDI out code... I feel stupid for posting before even trying that, sorry... I do receive correct data now, but only for a few seconds, it then becomes erratic again. I thought some bits could be lost during the serial transmission... Has anobody experienced something similar? I'm using a MIDI socket that fits in my breadbord, should I try to solder wires to the legs instead? Could it be more reliable or are there other things to check first?

Can we see your circuit?

Sure, it looks like this.

Comparing your circuit to the reference circuit from the MIDI Manufacturers Association, they have two inverters and a 220 ohm resistor betwen the serial out and pin 5 of the DIN socket.

So, I tried out your sketch on an Arduino UNO r3. I made the change I suggested earlier regarding byte rather than integer types.
Since I was using the rugged circuits MIDI sheild, you will also see some extra lines which enable MIDI.
To make monitoring easier, I added a delay in the main loop.

Connection was to the MIDI-in on my audio interface (PreSonus AudioBox 44VSL). Monitoring was with MIDI-OX

// from arduino forum
// http://arduino.cc/forum/index.php/topic,122378.msg946983.html#msg946983
// modified to change variables from int to byte, 
// using rugged circuits MIDI shield

#include <MIDI.h>

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

// Variables:
byte cc = 0;
int AnalogValue = 0; // define variables for the controller data
byte lastAnalogValue = 0; // define the "lastValue" variables

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

void loop() {
  AnalogValue = analogRead(0);
  //  convert to a range from 0 to 127:
  cc = AnalogValue/8;
  // check if analog input has changed
  if (lastAnalogValue != cc) {
    MIDI.sendControlChange(16,cc,1);
    // update lastAnalogValue variable
    lastAnalogValue = cc;
  }  //  endif
    delay(500);
}

Results were as expected, I got all values between 0 and 127 inclusive over the range of travel of the pot. I used a 10-turn 1/4W wirewound 10k linear pot. Monitoring for several minutes did not reveal any erratic readings.

Looking at page 2 of the schematic shows a similar output circuitry to the MMA reference one - a couple of inverters and a 240R resistor.

Thanks a lot Nantonos for testing it, I'll try with a delay in case my problem comes from spamming the serial port.

For what its worth, I have been following this and one of your other posts, as I was having very similar issues. Nantoons was quite help ful there as well, for me my issue, as best as I can tell is that the pots I had were garbage, thy were new, or at least as far as I know, bought them Very cheap of ebay. so i need a different pot for another project, so I had gotten some more, and it seems like that was my issue! and the strange thing is that i got the same stable results from a 1k and a 20k pot so that answers my question if it maters on the pots resistance, as I was using 5k pots, and though maybe I needed 10k only because it seems like that's what is in many tutorials, I do think it would be a good size to use though.
In one of your questions you asked if you should solder your midi port, in my experience, any wiggle room will send all sorts of garbage thru so you may want to do that or if you are still needing it to be in the bread board, find some nice tight holes if you can and if it seems suspect you can always wiggle it around a little. and as a side note, I am using slightly modified version of your other code, combined with a couple lines of nantoons, because, I picked up a midi shield to eliminate wiring problems, and then realized that i still had the same issues, but, that it was the same shield that nantoons was using.
Good luck, and post your results
HANKENSTIEN

I'm using 10k pots but the problem doesn't come from these. If I send their read value to the serial monitor, everything's fine (apart from some minor flickering).

Do you have any other brands to try, I went through a whole lot b4 i figured out it was the pots, I ha even checked mine with a ohm meter, and they seemed stable enough, but, in the end that's what it was for me. but, im using abelton, as nantoons who is a very helpful person has shown me that the code, I was using which I believe was based on yours worked for me on the software, but not one the hardware, he just posted, some code for me on the other post on here, I believe you started that one, as well, I just did a reply, so it should be towards the top, maybe some of the code he gave me will benefit you? good luck, who would have thought this simple task would end up being so difficult.
Ive built extremely more complex projects with lcds, and composite tv output with customized "text and video", temperature controlled 110vac window fan, "night" lights using 110v ac & relays, temp,humidity,altitude,and Barometric Pressure all displayed on a lcd, digital tape measures using sonar, and another one using Ir witch can also me used as safety or proximity sensor/ switches in a production plant, Infrared heat and motion detection alarm systems, an electric tuba, a synthesizer,base and treble visual Via Led volume meters, MORE...and then these dang midi controllers, for some reason, have really been tough and compared to the others these should be a walk in the park, and for me it ended up being the dang pots, who would have thought?

Hi following the post I finally got a solution!!!

I'm using the first code posting by Yan_g

#include <MIDI.h>

// Variables:
int cc = 0;
int AnalogValue = 0; // define variables for the controller data
int lastAnalogValue = 0; // define the "lastValue" variables

void setup() {
// launch MIDI
MIDI.begin(4);
}

void loop() {
AnalogValue = analogRead(0);
// convert to a range from 0 to 127:
cc = AnalogValue/8;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(16,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;
} // endif
}

and I've just made a modify th circuit puttin a 220ohm resistor between MIDI din port pin4 to digital input 1 on my arduino 2009

I've tested a 10k , 100k and 1m pots and all works fine...

I can't put another pots on the arduino using the same code...

here's the code...

#include <MIDI.h>
#define MIDI_ENABLE 12

// Variables:
int cc = 0;
int AnalogValue = 0; // define variables for the controller data
int lastAnalogValue = 0; // define the "lastValue" variables

void setup() {

// launch MIDI
MIDI.begin(8);
}

void loop() {
AnalogValue = analogRead(0);
// convert to a range from 0 to 127:
cc = AnalogValue/8;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(16,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;
}
AnalogValue = analogRead(3);
// convert to a range from 0 to 127:
cc = AnalogValue/8;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(17,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;
} // endif

}

Hello! I'm doing Midi Controller and have tested many different codes to get it work Code in this topi works perfect. But I have trouble to get another potentiometer connected... If I mod the code like this

#include <MIDI.h>

// Variables:
int cc_potikka1 = 0;
int AnalogValue_potikka1 = 0; // define variables for the controller data
int lastAnalogValue_potikka1 = 0; // define the "lastValue" variables

int cc_potikka2 = 0;
int AnalogValue_potikka2 = 0; // define variables for the controller data
int lastAnalogValue_potikka2 = 0; // define the "lastValue" variables

void setup() {
  //  launch MIDI
  MIDI.begin(4);

  
}

void loop() 
{
	AnalogValue_potikka1 = analogRead(0);
	AnalogValue_potikka2 = analogRead(1);
	//  convert to a range from 0 to 127:
	cc_potikka1 = AnalogValue_potikka1/8;
	cc_potikka2 = AnalogValue_potikka2/8;
	// check if analog input has changed
	
	if (lastAnalogValue_potikka1 != cc_potikka1) 
	{
		MIDI.sendControlChange(16,cc_potikka1,1);
		// update lastAnalogValue variable
		lastAnalogValue_potikka1 = cc_potikka1;
	}  //  endif
	
	if (lastAnalogValue_potikka2 != cc_potikka2) 
	{
		MIDI.sendControlChange(17,cc_potikka2,2);
		// update lastAnalogValue variable
		lastAnalogValue_potikka2 = cc_potikka2;
	}  //  endif
}

and try to mapping that my software (Traktor) and using those as pitchfaders, Traktor move both faders sametime, doesn't matter which fader I touch. Can somebody explain me how this need to change to get it work right?

if you do...
if (abs(lastAnalogValue_potikka2 - cc_potikka2) > 6) {
MIDI.sendControlChange(17,cc_potikka2,2);
lastAnalogValue_potikka2 = cc_potikka2;
}

this will give you a gate which remains shut for very low fluctuations (noise) but then opens for large movements (when you actually move the fader/pot).
You effectively filter out the noise.