Here is the code I wrote for a hand controller with 6 buttons and two pots.
Copy and paste it into the Arduino IDE
In the first section labeled // BUTTONS
Change N_BUTTONS = to the number of buttons you want to use.
Change BUTTON_ARDUINO_PIN to the list of digital pins you connected each button to.
In the next section labeled // POTENTIOMETERS
Change N_POTS = to the number of pots
Change POT_ARDUINO_PIN to the list of analog pins you connected the pots to
Upload it to the Arduino, flash the bios and you are done.
(Copy from the next line and paste into the IDE)
// – Defines the MIDI library – //
// if using with ATmega328 - Uno, Mega, Nano…
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
/////////////////////////////////////////////
// BUTTONS
const int N_BUTTONS = 6; //* total numbers of buttons
const int BUTTON_ARDUINO_PIN[N_BUTTONS] = {0, 2, 4, 6, 8 ,10}; //* pins of each button connected straight to the Arduino
int buttonCState[N_BUTTONS] = {}; // stores the button current value
int buttonPState[N_BUTTONS] = {}; // stores the button previous value
//#define pin13 1 //* uncomment if you are using pin 13 (pin with led), or comment the line if not using
byte pin13index = 12; //* put the index of the pin 13 of the buttonPin[] array if you are using, if not, comment
// debounce
unsigned long lastDebounceTime[N_BUTTONS] = {0}; // the last time the output pin was toggled
unsigned long debounceDelay = 10; //* the debounce time; increase if the output flickers
/////////////////////////////////////////////
// POTENTIOMETERS
const int N_POTS = 2; //* total numbers of pots (slide & rotary)
const int POT_ARDUINO_PIN[N_POTS] = {A1, A3}; //* pins of each pot connected straight to the Arduino
int potCState[N_POTS] = {0}; // Current state of the pot
int potPState[N_POTS] = {0}; // Previous state of the pot
int potVar = 0; // Difference between the current and previous state of the pot
int midiCState[N_POTS] = {0}; // Current state of the midi value
int midiPState[N_POTS] = {0}; // Previous state of the midi value
const int varThreshold = 4; //* Threshold for the potentiometer signal variation
/////////////////////////////////////////////
// MIDI
byte midiCh = 1; //* MIDI channel to be used
byte cc = 105; //* Lowest MIDI CC to be used
byte ccp = 103; //* Lowest MIDI CC to be used
/////////////////////////////////////////////
// SETUP
void setup() {
// Baud Rate
// use if using with ATmega328 (uno, mega, nano…)
Serial.begin(31250); //*
// Buttons
// Initialize buttons with pull up resistors
for (int i = 0; i < N_BUTTONS; i++) {
pinMode(BUTTON_ARDUINO_PIN[i], INPUT_PULLUP);
}
// Pots
for (int i = 0; i < N_POTS; i++)
{
// Initialise the analogue value with a read to the input pin.
potCState[i] = analogRead(POT_ARDUINO_PIN[i]);
}
#ifdef pin13 // inicializa o pino 13 como uma entrada
pinMode(BUTTON_ARDUINO_PIN[pin13index], INPUT);
#endif
}
/////////////////////////////////////////////
// LOOP
void loop() {
buttons();
potentiometers();
}
/////////////////////////////////////////////
// BUTTONS
void buttons() {
for (int i = 0; i < N_BUTTONS; i++) {
buttonCState[i] = digitalRead(BUTTON_ARDUINO_PIN[i]); // read pins from arduino
#ifdef pin13
if (i == pin13index) {
buttonCState[i] = !buttonCState[i]; // inverts the pin 13 because it has a pull down resistor instead of a pull up
}
#endif
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (buttonPState[i] != buttonCState[i]) {
lastDebounceTime[i] = millis();
if (buttonCState[i] == LOW) {
// use if using with ATmega328 (uno, mega, nano…)
MIDI.sendControlChange(cc + i, 127, 1); // cc number, cc value, midi channel
}
}
buttonPState[i] = buttonCState[i];
}
}
}
/////////////////////////////////////////////
// POTENTIOMETERS
void potentiometers() {
for (int i = 0; i < N_POTS; i++) { // Loops through all the potentiometers
potCState[i] = analogRead(POT_ARDUINO_PIN[i]); // reads the pins from arduino
midiCState[i] = map(potCState[i], 0, 1023, 0, 127); // Maps the reading of the potCState to a value usable in midi
potVar = abs(potCState[i] - potPState[i]);
if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
if (midiCState[i] != midiPState[i]) {
MIDI.sendControlChange(ccp + i, midiCState[i], midiCh);
potPState[i] = potCState[i]; // Stores the current reading of the potentiometer to compare with the next
midiPState[i] = midiCState[i];
}
}
}
}