Pages: [1]   Go Down
Author Topic: Still unable to send CC using MIDI Library  (Read 3927 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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!
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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: http://www.midi.org/techspecs/midimessages.php)
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote from: 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).
Logged

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would suggest declaring cc as

byte cc = 0;

because that is the expected type for the control value:


Code:
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.
« Last Edit: October 06, 2012, 02:37:35 pm by Nantonos » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,

I have actually found that my circuit was wrong  smiley-red 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?
Logged

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can we see your circuit?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sure, it looks like this.


* midiPot.png (39.45 KB, 413x511 - viewed 144 times.)
Logged

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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.
« Last Edit: October 06, 2012, 06:44:10 pm by Nantonos » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 1
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Collegno, Turin
Offline Offline
Newbie
*
Karma: 0
Posts: 37
Arduino lovers
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I'm using the first code posting by Yan_g

Quote
#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...
Logged

Collegno, Turin
Offline Offline
Newbie
*
Karma: 0
Posts: 37
Arduino lovers
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

here's the code...

Quote
#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(smiley-cool;
}

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
 
}


Logged

Pages: [1]   Go Up
Jump to: