12
« on: May 14, 2023, 12:58:47 PM »
hi, so i just saw this now and i'm planning todo a whole thread on this - but i managed to code an arduino with 4 bankakble midi over usb footswitches, and up to 4 potentiometers
I'm using the control-surface library, and it works.
this is my code:
#include <Control_Surface.h> // Include the Control Surface library
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
// Instantiate four Banks, with eight tracks per bank.
Bank<4> bank(8);
// │ └───── number of tracks per bank
// └───────────── number of banks
// Instantiate a Bank selector to control which one of the four Banks is active.
IncrementDecrementSelector<4> selector = {
bank, // Bank to manage
{2, 3}, // Push button pins (increment, decrement)
Wrap::Wrap, // Wrap around
};
// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
Bankable::CCButtonLatched<4> buttons[] {
{ bank, 4, 0x10 },
// │ │ └──── Base MIDI CC controller number
// │ └───────── Button pin number
// └───────────── Bank that changes the controller number
{ bank, 5, 0x11 },
{ bank, 6, 0x12 },
{ bank, 7, 0x13 },
};
// The bank controls the offset of the controller number relative to the
// button's base address. The amount of offset depends on the "number of
// tracks per bank" argument used when creating the bank. In this case,
// it's 8 tracks per bank, so for every bank you go up, 8 is added to the
// controller number of each button:
//
// │ Button 1 │ Button 2 │ Button 3 │ Button 4 │ Offset
// ────────┼────────────┼────────────┼────────────┼────────────┤
// Bank 1 │ 0x10 │ 0x11 │ 0x12 │ 0x13 │ 0×8=0
// Bank 2 │ 0x18 │ 0x19 │ 0x1A │ 0x1B │ 1×8=8
// Bank 3 │ 0x20 │ 0x21 │ 0x22 │ 0x23 │ 2×8=16
// Bank 4 │ 0x28 │ 0x29 │ 0x2A │ 0x2B │ 3×8=24
// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 10, 11, 12, 13 };
// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }
static_assert(length(buttons) == length(ledPins),
"Error: requires same number of buttons as LEDs");
CCPotentiometer knobsTop[] {
{A0, MIDI_CC::General_Purpose_Controller_1},
{A1, MIDI_CC::General_Purpose_Controller_2},
{A2, MIDI_CC::General_Purpose_Controller_3},
{A3, MIDI_CC::General_Purpose_Controller_4},
};
void setup() {
Control_Surface.begin(); // Initialize the Control Surface
for (auto pin : ledPins) // Set the pinMode to output for all LEDs
pinMode(pin, OUTPUT);
}
void loop() {
Control_Surface.loop(); // Update the Control Surface
// Loop over all buttons and LEDs
for (size_t i = 0; i < length(buttons); ++i)
// Update the LED states to reflect the toggled switch states
digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}