Hi There,
I'd like to learn how to properly check for button presses using the Bounce Library. The code that I've assembled below works well to send single midi messages. I'd like to press and hold a button to then change and send a different MID message. I see the button.update strings in the code but, I'm really not sure how to use it in my if statements. ? any info or guidance would be appreciated.
//How To Check Button Press
#include <Bounce.h>
const int channel = 1;
int ledPin = 13;
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5);
void setup() {
pinMode(13, OUTPUT);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
}
void loop() {
button0.update();
button1.update();
if (button0.fallingEdge()) {
usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
digitalWrite(ledPin,HIGH);
delay(10);
digitalWrite(ledPin,LOW);
}
if (button1.fallingEdge()) {
usbMIDI.sendNoteOn(61, 99, channel); // 61 = C#4
digitalWrite(ledPin,HIGH);
delay(10);
digitalWrite(ledPin,LOW);
}
if (button0.risingEdge()) {
}
if (button1.risingEdge()) {
}
while (usbMIDI.read()) {
// ignore incoming messages
}
}
'd like to press and hold a button to then change and send a different MID message.
First you may want to update to the Bounce2 library. Available through the library manager of the ide.
There is a library example program named "retrigger", which may give you some ideas on how to do something while a button is held down.
Thanks for the reply ! .. I do have the Bounce 2 Library and have tried a few things with it. but wasn't able to get a clean single midi message sent out. it was a blast of about 50 or so messages.
I'll have a look at the retrigger sketch you've mention ....
I do have the Bounce 2 Library and have tried a few things with it. but wasn't able to get a clean single midi message sent out. it was a blast of about 50 or so messages.
How will you select a new MIDI message to send while a button is held down.?
Doing something once is often accomplished with a boolean control variable. It can be set true or false a different points in the program.
if(beenThereDoneThat == false)
{
//do what needs to be done
beenThereDoneThat = true;
}
The Retrigger Sketch is very interesting. May common handy down the line but not quite what I'm after.
Noting your reply about Booleans, I'll have to study them a bit to familiar at try them.
an example of the behavior I'm after is ... 2 buttons A & B .. when each are pressed, they send a separate midi message. if Button A is pressed and held, Button B's midi message changes. make sense ?
and thanks so much for the replies!
if Button A is pressed and held, Button B's midi message changes. make sense ?
You can easily code for a short or long press on button A. It's easy to change B's message with a long press, and you can go back to the original message on a second long press if desired.
Do you want to have multiple choices for B's message, or just two? You might be able to have a long press on A trigger the code to count a number of presses on B to code for multiple messages.
Here's some example code on how to use Bounce2 to differentiate a long and short press.
#include <Bounce2.h>
#define buttonPin 5
#define shortTime 500
#define longTime 1500
#define noEvent 0
#define shortPress 1
#define longPress 2
// Instantiate a Bounce object called button :
Bounce button = Bounce();
unsigned long buttonPressStartTimeStamp;
unsigned long buttonPressDuration;
boolean startTimeout = false;
boolean endTimeout = false;
byte event;
void setup()
{
Serial.begin(115200);
Serial.println("Long/Short Button Press");
// Setup the button with an internal pull-up
pinMode(buttonPin, INPUT_PULLUP);
button.attach(buttonPin);
button.interval(20);//set debounce interval
}
void loop()
{
event = checkButton();
switch (event)
{
case shortPress:
Serial.println("short press");
//do short press code
break;
case longPress:
Serial.println("long press");
//do long press code
break;
}
}
byte checkButton()
{
byte event = noEvent;
// Update the Bounce instance, does digitalRead of button
button.update();
// Button press transition from HIGH to LOW)
if (button.fell())
{
buttonPressStartTimeStamp = millis();
startTimeout = true;
}
// Button release transition from LOW to HIGH) :
if (button.rose())
{
buttonPressDuration = (millis() - buttonPressStartTimeStamp);
startTimeout = false;
}
if (buttonPressDuration > 0 && buttonPressDuration <= shortTime)
{
event = shortPress;
buttonPressDuration = 0;
}
if (startTimeout == true && (millis() - buttonPressStartTimeStamp) > longTime)
{
event = longPress;
startTimeout = false;
buttonPressDuration = 0;
}
return event;
}
Thank you so much Cattledog ! ... I will give this a shot and report back ! 