Hi, I made a MIDI-controller using a Teensy LC. I want the 'PREVIOUS' and 'NEXT' button to send their MIDI message again at a frequency of three times a second when I press it down for more then one second and stop when I let it go.
I can't seem to figure out how that works. Can someone help me? Here is the code I am using:
#include <Bounce.h>
const int NUM_OF_BUTTONS = 4;
const int MIDI_CHAN = 16;
const int DEBOUNCE_TIME = 20;
Bounce buttons[NUM_OF_BUTTONS] =
{
Bounce (0, DEBOUNCE_TIME),
Bounce (1, DEBOUNCE_TIME),
Bounce (2, DEBOUNCE_TIME),
Bounce (3, DEBOUNCE_TIME)
};
const int MIDI_NOTE_NUMS[NUM_OF_BUTTONS] = {1, 2, 3, 4};
const int MIDI_NOTE_VELS[NUM_OF_BUTTONS] = {127, 127, 127, 127};
void setup()
{
for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
{
pinMode (i, INPUT_PULLUP);
}
}
void loop()
{
for (int i = 0; i < NUM_OF_BUTTONS; i++)
{
buttons[i].update();
}
if (buttons[0].fallingEdge()) // GO!
{
usbMIDI.sendNoteOn (MIDI_NOTE_NUMS[1], MIDI_NOTE_VELS(127), MIDI_CHAN);
}
else if (buttons[0].risingEdge())
{
usbMIDI.sendNotOff (MIDI_NOTE_NUMS[1], 0, MIDI_CHAN);
}
if (buttons[1].fallingEdge()) // PANIC!
{
usbMIDI.sendNoteOn (MIDI_NOTE_NUMS[2], MIDI_NOTE_VELS(127), MIDI_CHAN);
}
else if (buttons[1].risingEdge())
{
usbMIDI.sendNotOff (MIDI_NOTE_NUMS[2], 0, MIDI_CHAN);
}
if (buttons[2].fallingEdge()) // PREVIOUS
{
usbMIDI.sendNoteOn (MIDI_NOTE_NUMS[3], MIDI_NOTE_VELS(127), MIDI_CHAN);
}
else if (buttons[2].risingEdge())
{
usbMIDI.sendNotOff (MIDI_NOTE_NUMS[3], 0, MIDI_CHAN);
}
if (buttons[3].fallingEdge()) // NEXT
{
usbMIDI.sendNoteOn (MIDI_NOTE_NUMS[4], MIDI_NOTE_VELS(127), MIDI_CHAN);
}
else if (buttons[3].risingEdge())
{
usbMIDI.sendNotOff (MIDI_NOTE_NUMS[4], 0, MIDI_CHAN);
}
while (usbMIDI.read())
{
// ignoring incoming messages, so don't do anything here.
}
}