This is my first post here and also my first time attempting any sort of project like this, but I'm making a midi controller with four faders using the BlokasLabs USBMIDI library and I'm struggling to figure out how to get the faders to send midi CCs 11, 1, 21, 7. Could anyone take a look at the code below and advise me on how to do this? It should be a simple fix (I hope) but I've got no idea what I'm doing.
Any help would be appreciated!
#include <midi_serialization.h>
#include <usbmidi.h>
// See midictrl.png in the example folder for the wiring diagram,
// as well as README.md.
void sendCC(uint8_t channel, uint8_t control, uint8_t value) {
USBMIDI.write(0xB0 | (channel & 0xf));
USBMIDI.write(control & 0x7f);
USBMIDI.write(value & 0x7f);
}
void sendNote(uint8_t channel, uint8_t note, uint8_t velocity) {
USBMIDI.write((velocity != 0 ? 0x90 : 0x80) | (channel & 0xf));
USBMIDI.write(note & 0x7f);
USBMIDI.write(velocity &0x7f);
}
const int ANALOG_PIN_COUNT = 4;
const int BUTTON_PIN_COUNT = 2;
// Change the order of the pins to change the ctrl or note order.
int analogPins[ANALOG_PIN_COUNT] = { A3, A2, A1, A0 };
int buttonPins[BUTTON_PIN_COUNT] = { 9, 8 };
int ccValues[ANALOG_PIN_COUNT];
int buttonDown[BUTTON_PIN_COUNT];
int readCC(int pin) {
// Convert from 10bit value to 7bit.
return analogRead(pin) >> 3;
}
int isButtonDown(int pin) {
return digitalRead(pin) == 0;
}
void setup() {
// Initialize initial values.
for (int i=0; i<ANALOG_PIN_COUNT; ++i) {
pinMode(analogPins[i], INPUT);
ccValues[i] = readCC(analogPins[11, 1, 21]);
}
for (int i=0; i<BUTTON_PIN_COUNT; ++i) {
pinMode(buttonPins[i], INPUT);
digitalWrite(buttonPins[i], HIGH);
buttonDown[i] = isButtonDown(buttonPins[i]);
}
}
void loop() {
//Handle USB communication
USBMIDI.poll();
while (USBMIDI.available()) {
// We must read entire available data, so in case we receive incoming
// MIDI data, the host wouldn't get stuck.
u8 b = USBMIDI.read();
}
for (int i=0; i<ANALOG_PIN_COUNT; ++i) {
int value = readCC(analogPins[i]);
// Send CC only if th has changed.
if (ccValues[i] != value) {
sendCC(0, i, value);
ccValues[i] = value;
}
}
for (int i=0; i<BUTTON_PIN_COUNT; ++i) {
int down = isButtonDown(buttonPins[i]);
if (down != buttonDown[i]) {
sendNote(0, 64 + i, down ? 127 : 0);
buttonDown[i] = down;
}
}
// Flush the output.
USBMIDI.flush();
}
Thank you so much for this response! I'll try to implement your suggestions a little later today!
As for the first part of your comment, it should say " i " in the square brackets but I accidentally changed it when I was trying to work out the code.
I think I'm getting what you're saying though, so hopefully it'll work and I don't mangle it too much!
I inserted your code into my sketch where it was supposed to go and added the necessary CC channels to the "etc. etc." portion of your code, but after trying it out with one fader connected to the board, it works- but, the one fader changes all four parameters. Is this likely an issue as a result of only testing it with one fader or does this seem like it might be a code issue?
I'm aware that these are really vague and particular questions to answer, so if you need any additional information in order to help, please let me know!
Here's the code, but I tried it out with more of the faders and it seems to be working, so I think it should be ok.
#include <midi_serialization.h>
#include <usbmidi.h>
// See midictrl.png in the example folder for the wiring diagram,
// as well as README.md.
void sendCC(uint8_t channel, uint8_t control, uint8_t value) {
USBMIDI.write(0xB0 | (channel & 0xf));
USBMIDI.write(control & 0x7f);
USBMIDI.write(value & 0x7f);
}
void sendNote(uint8_t channel, uint8_t note, uint8_t velocity) {
USBMIDI.write((velocity != 0 ? 0x90 : 0x80) | (channel & 0xf));
USBMIDI.write(note & 0x7f);
USBMIDI.write(velocity &0x7f);
}
const int ANALOG_PIN_COUNT = 4;
const int BUTTON_PIN_COUNT = 2;
// Change the order of the pins to change the ctrl or note order.
int analogPins[ANALOG_PIN_COUNT] = { A3, A2, A1, A0 };
int buttonPins[BUTTON_PIN_COUNT] = { 9, 8 };
int ccValues[ANALOG_PIN_COUNT];
int buttonDown[BUTTON_PIN_COUNT];
int readCC(int pin) {
// Convert from 10bit value to 7bit.
return analogRead(pin) >> 3;
}
int isButtonDown(int pin) {
return digitalRead(pin) == 0;
}
void setup() {
// Initialize initial values.
for (int i=0; i<ANALOG_PIN_COUNT; ++i) {
pinMode(analogPins[i], INPUT);
ccValues[i] = readCC(analogPins[i]);
}
for (int i=0; i<BUTTON_PIN_COUNT; ++i) {
pinMode(buttonPins[i], INPUT);
digitalWrite(buttonPins[i], HIGH);
buttonDown[i] = isButtonDown(buttonPins[i]);
}
}
void loop() {
//Handle USB communication
USBMIDI.poll();
while (USBMIDI.available()) {
// We must read entire available data, so in case we receive incoming
// MIDI data, the host wouldn't get stuck.
u8 b = USBMIDI.read();
}
for (int i=0; i<ANALOG_PIN_COUNT; ++i) {
int value = readCC(analogPins[i]);
if (ccValues[i] != value) {
int whichCC;
if(i == 0) whichCC = 11; // or whatever CC slider 0 should send
if(i==1) whichCC = 1; // or whatever CC slider 1 should send.
if(i==2) whichCC = 21; // or whatever CC slider 1 should send.
if(i==3) whichCC = 7; // or whatever CC slider 1 should send.
sendCC(0, whichCC, value);
ccValues[i] = value;
}
// Send CC only if th has changed.
if (ccValues[i] != value) {
sendCC(0, i, value);
ccValues[i] = value;
}
}
for (int i=0; i<BUTTON_PIN_COUNT; ++i) {
int down = isButtonDown(buttonPins[i]);
if (down != buttonDown[i]) {
sendNote(0, 64 + i, down ? 127 : 0);
buttonDown[i] = down;
}
}
// Flush the output.
USBMIDI.flush();
}