Go Down

Topic: Simple 4 fader MIDI controller code (Read 631 times) previous topic - next topic

John_Hendicott

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

MarkT

Post your existing code?  And circuit?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

John_Hendicott

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() {
  usbMIDI.setHandleControlChange(OnControlChange);
 
   pinMode(5, INPUT_PULLUP);
 
}

void loop() {
  usbMIDI.read(); // USB MIDI receive
   // only check the analog inputs 50 times per second,
  // to prevent a flood of MIDI messages
 
   button5.update();
  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) {
}

Grumpy_Mike

#3
Aug 21, 2019, 09:31 am Last Edit: Aug 21, 2019, 09:39 am by Grumpy_Mike
Quote
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.

Quote
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
Code: [Select]
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
Code: [Select]
if (n0 != previousA0)
With
Code: [Select]
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 )

John_Hendicott

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

Go Up