Code:
#define SCENES_COUNT 4
#include <Bounce.h>
const int channel = 1;
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5); // 5 = 5 ms debounce time
Bounce button3 = Bounce(3, 5); // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 5);
const int buttonPin = 2;
const int ledPins[SCENES_COUNT] = {7, 8, 9, 10};
int scene = 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
for (int i = 0; i < SCENES_COUNT; i++)
{
pinMode(ledPins[i], OUTPUT);
}
}
void loop()
{
button0.update();
button1.update();
button3.update();
button4.update();
// If scene button is pressed
if (digitalRead(buttonPin) == HIGH)
{
// Increment scene by 1, and if it's greater than the scene count, reset to 0
if (++scene >= SCENES_COUNT)
{
scene = 0;
}
// Loop through all LEDs
for (int i = 0; i < SCENES_COUNT; i++)
{
// If i equals the current scene, enable this LED, otherwise disable it
digitalWrite(ledPins[i], i == scene ? HIGH : LOW);
}
}
switch (scene)
{
// First scene
case 0:
/* Read other buttons and do stuff here */
if (button0.fallingEdge()) {
usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
}
if (button1.fallingEdge()) {
usbMIDI.sendNoteOn(61, 99, channel); // 61 = C#4
}
if (button3.fallingEdge()) {
usbMIDI.sendNoteOn(63, 99, channel); // 63 = D#4
}
if (button4.fallingEdge()) {
usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4
}
if (button0.risingEdge()) {
usbMIDI.sendNoteOff(60, 0, channel); // 60 = C4
}
if (button1.risingEdge()) {
usbMIDI.sendNoteOff(61, 0, channel); // 61 = C#4
}
if (button3.risingEdge()) {
usbMIDI.sendNoteOff(63, 0, channel); // 63 = D#4
}
if (button4.risingEdge()) {
usbMIDI.sendNoteOff(64, 0, channel); // 64 = E4
}
break;
// Second scene
case 1:
/* Read other buttons and do stuff here */
if (button0.fallingEdge()) {
usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
}
if (button1.fallingEdge()) {
usbMIDI.sendNoteOn(61, 99, channel); // 61 = C#4
}
if (button3.fallingEdge()) {
usbMIDI.sendNoteOn(63, 99, channel); // 63 = D#4
}
if (button4.fallingEdge()) {
usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4
}
if (button0.risingEdge()) {
usbMIDI.sendNoteOff(60, 0, channel); // 60 = C4
}
if (button1.risingEdge()) {
usbMIDI.sendNoteOff(61, 0, channel); // 61 = C#4
}
if (button3.risingEdge()) {
usbMIDI.sendNoteOff(63, 0, channel); // 63 = D#4
}
if (button4.risingEdge()) {
usbMIDI.sendNoteOff(64, 0, channel); // 64 = E4
}
break;
// Third scene
case 2:
/* Read other buttons and do stuff here */
if (button0.fallingEdge()) {
usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
}
if (button1.fallingEdge()) {
usbMIDI.sendNoteOn(61, 99, channel); // 61 = C#4
}
if (button3.fallingEdge()) {
usbMIDI.sendNoteOn(63, 99, channel); // 63 = D#4
}
if (button4.fallingEdge()) {
usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4
}
if (button0.risingEdge()) {
usbMIDI.sendNoteOff(60, 0, channel); // 60 = C4
}
if (button1.risingEdge()) {
usbMIDI.sendNoteOff(61, 0, channel); // 61 = C#4
}
if (button3.risingEdge()) {
usbMIDI.sendNoteOff(63, 0, channel); // 63 = D#4
}
if (button4.risingEdge()) {
usbMIDI.sendNoteOff(64, 0, channel); // 64 = E4
}
break;
// Fourth scene
case 3:
/* Read other buttons and do stuff here */
if (button0.fallingEdge()) {
usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4
}
if (button1.fallingEdge()) {
usbMIDI.sendNoteOn(61, 99, channel); // 61 = C#4
}
if (button3.fallingEdge()) {
usbMIDI.sendNoteOn(63, 99, channel); // 63 = D#4
}
if (button4.fallingEdge()) {
usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4
}
if (button0.risingEdge()) {
usbMIDI.sendNoteOff(60, 0, channel); // 60 = C4
}
if (button1.risingEdge()) {
usbMIDI.sendNoteOff(61, 0, channel); // 61 = C#4
}
if (button3.risingEdge()) {
usbMIDI.sendNoteOff(63, 0, channel); // 63 = D#4
}
if (button4.risingEdge()) {
usbMIDI.sendNoteOff(64, 0, channel); // 64 = E4
}
break;
}
}
so ive gotten a successfull compile and able to use the midi push buttons but the scene button was still not working. i have push buttons connected to D0-D4 on my teensy 2++ and i programmed the d0,d1,d3,d4 as my midi buttons and left d2 for the scene button. should i have used different I/O's on the teensy board for each case?
Bookmarks