I've created a simple midi preset controller that scrolls using separate up and down foot switches. When both are pressed at the same time it sends a different program change, however i'm find it quite hard to do as both foot switches have to be pressed at exactly the same time, is there a way to make this double press more forgiving?
Also if anyone has any advice about how to incorporate long press functionality that would be great!
Thanks
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
const byte LEDs[] = {15, 14, 16, 10, 9, 8, 7, 6}; // Pins for LEDs
const byte dipSwitchPins[] = {4, 5}; // Pins for dip switches
int Up = 2;
int Down = 3;
void setup() {
MIDI.begin();
pinMode (Up, INPUT_PULLUP); // Momentary switch 1
pinMode (Down, INPUT_PULLUP); // Momentary switch 2
pinMode(dipSwitchPins[0], INPUT_PULLUP); // Dip switch 1
pinMode(dipSwitchPins[1], INPUT_PULLUP); // Dip switch 2
for (byte i = 0; i < 8; i++) pinMode(LEDs[i], OUTPUT);
}
void loop() {
static int progNo = 0;
static bool upNow, downNow = false;
static byte channelNo = 1; // Default MIDI channel
upNow = digitalRead(Up) == LOW;
downNow = digitalRead(Down) == LOW;
// Check if both up and down buttons are pressed
if (upNow && downNow) {
progNo = 127; // Set to preset 127
MIDI.sendProgramChange(byte(progNo), channelNo);
delay(55); // not a Debounce delay
delay (50);
} else {
// Check individual button presses
if (upNow) {
progNo++; // Increment program change
if (progNo > 7) progNo = 0; // Wrap around to 0
MIDI.sendProgramChange(byte(progNo), channelNo);
delay(158);
}
if (downNow) {
progNo--; // Decrement program change
if (progNo < 0) progNo = 7; // Wrap around to 7
if (progNo == 126) progNo = 7;
MIDI.sendProgramChange(byte(progNo), channelNo);
delay(158);
}
}
// Update LEDs and print program and channel number
for (byte i = 0; i < 8; i++) digitalWrite(LEDs[i], progNo != i ? LOW : HIGH);
// Read dip switches to set MIDI channel
byte dipSwitchState = digitalRead(dipSwitchPins[0]) | (digitalRead(dipSwitchPins[1]) << 1);
channelNo = map(dipSwitchState, 0, 3, 1, 4);
}