Trying to create a code for a simple two button controller, I want to add some more functionality when both switches are pressed together.
When both buttons are short pressed I'd like to serial print 'looper' and when both buttons are long pressed i'd like to serial print 'kill dry signal'.
Could anyone help me with this, bit stuck on this so any help would be much appreciated.
#include <ezButton.h>
const byte LEDs[] = {15, 14, 16, 10, 9, 8, 7, 6}; // Pins for LEDs
const byte dipSwitchPins[] = {4, 5}; // Pins for dip switches
const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
ezButton button1(2); // create ezButton object that attaches to pin 2
ezButton button2(3); // create ezButton object that attaches to pin 3
unsigned long pressedTime1 = 0;
unsigned long releasedTime1 = 0;
bool isPressing1 = false;
bool isLongDetected1 = false;
unsigned long pressedTime2 = 0;
unsigned long releasedTime2 = 0;
bool isPressing2 = false;
bool isLongDetected2 = false;
void setup() {
Serial.begin(9600);
button1.setDebounceTime(50); // set debounce time to 50 milliseconds for button1
button2.setDebounceTime(50); // set debounce time to 50 milliseconds for button2
Serial.begin(9600);
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() {
button1.loop(); // MUST call the loop() function for button1 first
button2.loop(); // MUST call the loop() function for button2 first
static int progNo = 0;
static byte channelNo = 1; // Default MIDI channel
// Button 1 handling
if (button1.isPressed()) {
pressedTime1 = millis();
isPressing1 = true;
isLongDetected1 = false;
}
if (button1.isReleased()) {
isPressing1 = false;
releasedTime1 = millis();
long pressDuration1 = releasedTime1 - pressedTime1;
if (pressDuration1 < SHORT_PRESS_TIME) {
Serial.println("Up");
delay(200);
}
}
if (isPressing1 && !isLongDetected1) {
long pressDuration1 = millis() - pressedTime1;
if (pressDuration1 > LONG_PRESS_TIME) {
Serial.println("Double");
isLongDetected1 = true;
}
}
// Button 2 handling
if (button2.isPressed()) {
pressedTime2 = millis();
isPressing2 = true;
isLongDetected2 = false;
}
if (button2.isReleased()) {
isPressing2 = false;
releasedTime2 = millis();
long pressDuration2 = releasedTime2 - pressedTime2;
if (pressDuration2 < SHORT_PRESS_TIME) {
Serial.println("Down");
delay(200);
}
}
if (isPressing2 && !isLongDetected2) {
long pressDuration2 = millis() - pressedTime2;
if (pressDuration2 > LONG_PRESS_TIME) {
Serial.println("Half");
isLongDetected2 = true;
}
for (byte i = 0; i < 8; i++) digitalWrite(LEDs[i], progNo != i ? LOW : HIGH);
byte dipSwitchState = digitalRead(dipSwitchPins[0]) | (digitalRead(dipSwitchPins[1]) << 1);
channelNo = map(dipSwitchState, 0, 3, 1, 4);
}
}