I am helping with a school project to build a lighting desk. We have the basic parts, including a small desk with a low number of faders, and this is where my task comes in. The software, Magicq by Chamsys is able to use MIDI to change fader values in the software, so I have to code a teensy 3.2 that can take the analogue input from a linear pot, encode it into a midi note, which then gets sent to Chamsys over USB where Chamsys decodes it, and uses it to change the value on one of the software faders. The problems that I am currently experiencing are that a): The fader value in analogue form is fairly jumpy, and will not stay at the same value for more than about half a second, it (the value) moves around with around a +-10 tolerance. b): I have no real idea how to encode an analogue value into MIDI, and then send it over USB.
To solve your first problem, you could average multiple analog readings.
A MIDI Control Change value is 7 bits wide, while the result from analogRead is 10 bits wide. So just shift the reading 3 bits to the right using the bitshift operator (>>).
You might be interested in my MIDI Controller library. This makes it very easy to create a MIDI controller, with a lot of features, but with clean, simple code. For example:
#include <MIDI_controller.h> // include the library
const static byte Channel_Volume = 0x7; // controller number 7 is defined as Channel Volume in the MIDI implementation.
const static size_t analogAverage = 8; // Use the average of 8 samples to get smooth transitions and prevent noise
Analog fader1(A0, Channel_Volume, 1); // Create a new instance of the class 'Analog, called 'fader1', on pin A0, that sends MIDI messages with controller 7 (channel volume) on channel 1.
Analog fader2(A1, Channel_Volume, 2);
Analog fader3(A2, Channel_Volume, 3);
Analog fader4(A3, Channel_Volume, 4);
void setup(){
USBMidiController.blink(LED_BUILTIN); // flash the built-in LED (pin 13 on most boards) on every message
USBMidiController.setDelay(15); // wait 15 ms after each message not to flood the connection
USBMidiController.begin(); // Initialise the USB MIDI connection
delay(1000); // Wait a second...
fader1.average(analogAverage); // Use the average of 8 samples to get smooth transitions and prevent noise
fader2.average(analogAverage);
fader3.average(analogAverage);
fader4.average(analogAverage);
}
void loop(){
fader1.refresh(); // refresh the fader (check whether the input has changed since last time, if so, send it over MIDI)
fader2.refresh();
fader3.refresh();
fader4.refresh();
}