I re-read the Bounce documentation which allowed me a more comprehensive understanding of what that library can do for me, and I think that I have found a more elegant way to program this sketch by using Bounce to tidy up some redundancies in my sketch.
That said, I am still frequently recording more than one MIDI message per button contact. I am going to try to some different buttons when I can get some later in the week, but if there is any further wisdom on this problem it would be much appreciated!
I have tried bounce values as small as 5 ms and as great as 1000 ms with this code:
Also, further to a comment from Nick Gammon earlier in this thread, the buttons are on pull-down, not pull-up. (Is pull-up better and why?)
#include <MIDI.h>
#include <Bounce.h>
// set the pins:
const int buttonStop = 2;
const int buttonGo = 3;
const int buttonLast = 4;
const int buttonNext = 5;
// debounce the pushbuttons at 500 milliseconds:
Bounce bounceStop = Bounce(buttonStop, 5);
Bounce bounceGo = Bounce(buttonGo, 5);
Bounce bounceLast = Bounce(buttonLast, 5);
Bounce bounceNext = Bounce(buttonNext, 5);
void setup() {
MIDI.begin();
// initialize the pushbutton pin as an input:
pinMode(buttonStop, INPUT);
pinMode(buttonGo, INPUT);
pinMode(buttonLast, INPUT);
pinMode(buttonNext, INPUT);
}
void loop(){
// check to see if the state of the buttons has changed. if a button has been pushed, send a MIDI message:
if (bounceStop.update() && bounceStop.read() == HIGH) {
MIDI.sendNoteOn(1,127,5);
Bounce bounceStop = Bounce(buttonStop, 100);
}
if (bounceGo.update() && bounceGo.read() == HIGH) {
MIDI.sendNoteOn(2,127,5);
Bounce bounceGo = Bounce(buttonGo, 100);
}
if (bounceLast.update() && (bounceLast.read() == HIGH)) {
MIDI.sendNoteOn(3,127,5);
Bounce bounceLast = Bounce(buttonLast, 100);
}
if (bounceNext.update() && bounceNext.read() == HIGH) {
MIDI.sendNoteOn(4,127,5);
Bounce bounceNext = Bounce(buttonNext, 100);
}
}
I am immensely grateful for your help and guidance.