I've got a simple (SPST) footswitch button connected to a Teensy 2.0 that I'm trying to create multiple button-press events for. I've got it working in every way but one so far (so frustrating!!). Here's what's working:
The "short press" event sends the correct commands on the rising edge of the button (i.e. when the footswitch button has reached the top of its spring-loaded post).
The "long press" event sends the correct commands after the button has been pressed for the correct amount of time.
My problem is that once I let go of the button after a "long press" event, the commands for the "short press" event are sent even though I don't want them to, because the button has returned to its rising edge (it's spring-loaded, as I mentioned). Can someone help me figure this out?. I'm new at this, and this is mostly code I've cobbled together from elsewhere.
Thanks in advance!
Here's the code for the button:
Here are the .h and .cpp files we've cobbled together from sample code that we found. How do we get the long press to both work and sync with the 2 devices:
Button.h
Button.cpp
Footswitch.cpp
The "short press" event sends the correct commands on the rising edge of the button (i.e. when the footswitch button has reached the top of its spring-loaded post).
The "long press" event sends the correct commands after the button has been pressed for the correct amount of time.
My problem is that once I let go of the button after a "long press" event, the commands for the "short press" event are sent even though I don't want them to, because the button has returned to its rising edge (it's spring-loaded, as I mentioned). Can someone help me figure this out?. I'm new at this, and this is mostly code I've cobbled together from elsewhere.
Thanks in advance!
Here's the code for the button:
Here are the .h and .cpp files we've cobbled together from sample code that we found. How do we get the long press to both work and sync with the 2 devices:
Button.h
Code:
#include <Arduino.h>
#include <Bounce2.h>
#ifndef Button_h
#define Button_h
#define DEBOUNCE_DELAY 10
#define LONG_PRESS 250
class Button {
public:
Button(int pin);
bool update();
bool wasPressed();
bool wasLongPressed();
private:
Bounce *debouncer;
long pressedAt;
bool isActive;
bool isLongPress;
};
#endif
Button.cpp
Code:
Button::Button(int pin) {
pinMode(pin, INPUT_PULLUP);
debouncer = new Bounce(pin, DEBOUNCE_DELAY);
pressedAt = 0;
isActive = false;
isLongPress = false;
}
bool Button::update() {
bool didChange;
isLongPress = false;
didChange = debouncer->update();
if (debouncer->fell()) {
pressedAt = millis();
isActive = true;
}
if (debouncer->rose()) {
isActive = false;
}
if (isActive && (millis() - pressedAt) > LONG_PRESS) {
isActive = false;
isLongPress = true;
didChange = true;
}
return didChange;
}
bool Button::wasPressed() {
return debouncer->rose();
}
bool Button::wasLongPressed() {
return isLongPress;
}
Footswitch.cpp
Code:
#include <Arduino.h>
#include "Button.h"
#define BUTTON_1 PIN_B0
Button *buttons[1];
void setup() {
buttons[0] = new Button(BUTTON_1);
}
void sendMMC(uint16_t length, uint8_t* msg) {
static uint8_t buffer[20] = { 0xf0, 0x7f, 0x7f, 0x06 };
memcpy(buffer + 4, msg, length);
buffer[length + 4] = 0xf7;
usbMIDI.sendSysEx(length + 5, buffer, true);
}
void loop() {
// PLAY START
if (buttons[0]->update()) {
if (buttons[0]->wasPressed()) {
sendMMC(1, (uint8_t*)"\x02"); // Play
}
if (buttons[0]->wasLongPressed()) {
sendMMC(1, (uint8_t*)"\x01"); // Stop
sendMMC(8, (uint8_t*)"\x44\x06\x01\x00\x00\x00\x00\x00"); // Locate zero
}
}
while (usbMIDI.read()) {} // ignore incoming messages
}
Last edited: