Simple 4 fader MIDI controller code

Total beginner here, so apologies for the noob question - I’ve searched the forums and got HALF way there, but can’t seem to finish the code myself:

I’m simply trying to map 4 linear faders to MIDI CC numbers 16-19 on channel1.

I’ve got one fader working but can’t work out how to replicate for the others.

If anyone has any tips/code I’d be hugely grateful and can hopefully learn from what you’ve done.

Thank you, J

Post your existing code? And circuit?

Post your existing code? And circuit?

Thanks Mark, by circuit, do you mean what hardware? I’m using a Teensy 2.0

This code was taken from elsewhere and seems to work for fader 1 (A0), which is transmitting on CC16. There’s probably lots of extraneous code in there too.

/* merged different examples for Teensy by Paul Stoffregen
to test MIDI IN & OUT

This example code is in the public domain.
#include <Bounce.h>

const int channel = 1;
Bounce button5 = Bounce(5, 5); // if a button is too “sensitive”

// the MIDI continuous controller for each analog input
const int controllerA0 = 16; // 16 = general purpose

int previousA0 = -1;

elapsedMillis msec = 0;

void setup() {

pinMode(5, INPUT_PULLUP);


void loop() {; // USB MIDI receive
// only check the analog inputs 50 times per second,
// to prevent a flood of MIDI messages

if (button5.fallingEdge()) {
usbMIDI.sendNoteOn(65, 99, channel); // 65 = F4
if (button5.risingEdge()) {
usbMIDI.sendNoteOff(65, 0, channel); // 65 = F4

if (msec >= 20) {
msec = 0;
int n0 = analogRead(0) / 8;
int n1 = analogRead(1) / 8;
// only transmit MIDI messages if analog input changed
if (n0 != previousA0) {

usbMIDI.sendControlChange(controllerA0, n0, channel);
previousA0 = n0;

void OnControlChange(byte channel, byte control, byte value) {

by circuit, do you mean what hardware?

No this is a diagram showing how it is all wired up. These are called schematics, not to be confused with the utter crap which is a Fritzing physical layout diagram.

Also please read the how to use this forum sticky post to see how to post code.

There's probably lots of extraneous code in there too

Yes a lot of crap.

At it simplest you need to repeat this section for each fader / analogue input

if (n0 != previousA0) {
      usbMIDI.sendControlChange(controllerA0, n0, channel);
      previousA0 = n0;      

You need a different variable n0, previousA0 and controllerA0 for each of the extra faders.

This will produce workable but crap code. To stop the code being crap you need to use arrays in place of those individual variables and send all four using a for loop.

I would also replace

if (n0 != previousA0)


if (abs(n0 - previousA0) >= 2)

So that noise on the input signal doesn’t keep triggering a send and the change in value up or down has to be at least two ( the abs() function call gets you up or down )

Wow... Thanks @grumpy_mike that worked and will be perfect for the performance I'm doing this week. Will look further into the code and get some tutorials on how to clean things up.

Thanks again, super happy that it's working!!