MIDI messages all over the place

Hi forum.
I just got my first Arduino about two weeks ago so this is my first post.

I am trying to make a sliderpotentiometer control the volume of Cubase via MIDI.

I use the MIDI library and my sketch so far is very inspired by some other posts that i have read.

My problem is that the serial port get totally bombarded with crazy MIDI messages even though it should only be sending when i change the potmeter.
First of all the glitch obbviously is more than the simple bitbug, though i have no smoothing in the code yet. I know this because i monitor the signal with MIDI-OX and S2MIDI. From here i can see that all kinds of messages are being send on all channels at all velocitys.
Secondly is the rate of messages. They come very fast, probably about 20 per second. This again telles me that something is completely wrong.

i know that it is not a hardwarefail since i hvae tested a code that works to do just the same but written in a very other way, without the midi library.

also i have narrowed it down to the "MIDI.sendControlChange" command. The third parameter that controls the midi channel to send to, seems to cause the problem. when i set this to anything else than 1 to 16, it will simply send nothing. whatever i put in the other parameters when it is set to a channel between 1 and 16, the problem is there again.

I really feel like i have been reading half the forum to find out more about this but it just does not make sense to me why the library function will not work like this.

Please look at my code and see what the problem might be.
if it is some lame problem im am very sorry but as mentioned i am very new to Arduino, electronics and programming.

// Written by Jos Mirth for an Arduino Mega 2550. A potentiometer must be conected to analog inputpin A0

// This program cheks for changes in the value of a potentiometer (sliderpot) and sends it, if any, as a MIDI-ControlChange-message to change channelvolume
// It Uses the MIDI library, that can be downloaded from Arduino.cc 
// The information runs over the serial usb port. MIDI data is sent as BYTE and at baudrate 31250. For a PC to read this data, two programs are needed on the PC.
// First a program that searches and converts bytes from the serial connection into MIDI date, must be installed. For example use "S2midi"
// Then a virtual MIDI cable must be installed so that the first program has a place to send the data. for example use "LoopBe1"
// Additionally a program called MIDI-OX can be used to monitor and sendback MIDI messages on the PC

#include <MIDI.h>                   // Includes the MIDI library

int spot1;
int spot1_old;

void setup(){
MIDI.begin(MIDI_CHANNEL_OMNI);                      // Initiates MIDI-communication on the serial usb port on MIDI-channel 1 by default. Channels go from 1 to 16
		                    // To select specific channel othan than 1, type “MIDI.begin(4);” for channel 4
		                    // To select all channels, type “MIDI.begin(MIDI_CHANNEL_OMNI);”
		                    // Also it enables the Thru, with no filtering (everything caught is sent back)
MIDI.turnThruOff();	            // Turns off MIDI thru
}

void loop(){
    spot1 = analogRead(A0/8);   // Reads spot1 at analog inputpin A1,divided by 8 for midi (0-127)
    if (spot1 != spot1_old){      // If value of spot1 has changed
        MIDI.sendControlChange(7, spot1, 1);
                                    // Send MIDI ControlChange message with value of spot1
                                        // "7" means "channel volume", "spot1" is the value to write, "1" means write to channel 1
                                        // sendControlChange (byte ControlNumber, byte ControlValue, byte Channel);
                                        // Parameters:
                                        // ControlNumber:  The controller number (0 to 127). See http://www.somascape.org/midi/tech/spec.html#ctrlnums
                                        // ControlValue:   The value for the specified controller (0 to 127).
                                        // Channel:        The channel on which the message will be sent (1 to 16). 
        spot1_old = spot1;          // Updates the stored value of spot1 to enable looping of checking for valuechanges
    }
}

DAW_Controller.pde (2.55 KB)

My problem is that the serial port get totally bombarded with crazy MIDI messages even though it should only be sending when i change the potmeter.

The problem is that analog readings are not completely steady. Even though the voltage (pot setting) isn't changing at all it's not uncommon for the analog ready to go up or down by a couple of steps. That's just inherent noise and imprecision in the system.

Secondly is the rate of messages. They come very fast, probably about 20 per second. This again telles me that something is completely wrong.

It's not really a surprise. Your loop() function as written is certainly capable of sending out blasts of messages whenever the analog reading changes by just a little bit. I'd recommend adding in some filtering or even basic time delays.

--
Beat707: MIDI drum machine / sequencer / groove-box for Arduino

The messages that I recieve, are all kinds of messages and actually mostly with almost the same value of input. if the problem was that the analog signal to my sliderpot was unstady, would it not only be the value of the channel volume that would jumt up and down?

I noticed another error in your code:

    spot1 = analogRead(A0/8);   // Reads spot1 at analog inputpin A1,divided by 8 for midi (0-127)

That should be:

    spot1 = analogRead(A0)/8;   // Reads spot1 at analog inputpin A1,divided by 8 for midi (0-127)

I'm not sure what your other problems are, though slowing down the rate of sending serial data will help. You may just be overwhelming MIDI-OX.

--
The Flexible MIDI Shield: MIDI IN/OUT, stacking headers, your choice of I/O pins

midi connected to tx pin while uploading new sketch version?

Sorry, I didn't get that this was for MIDI over USB. I accidentally left tx connected to MIDI OUT a few times when uploading a sketch and that really caused some weird MIDI to be sent to my synth. Not sure how MIDI USB works, or how your particular serial MIDI drivers work, but if it sees all data sent between host and Arduino while uploading maybe something similar will happen?

Also did you look at the source code for the MIDI library to tell what it does differently from your code?

Can you post the full code for using or not using MIDI library? I can compare that to a similar old sketch I have. Also curious what is going on.