Here's the code that we were using. We have some ON-ON switches and ON-OFF-ON ones.
#include <Bounce.h>
#include <elapsedMillis.h>
Bounce d_switch_0 = Bounce(0,10);
Bounce d_switch_1 = Bounce(1,10);
Bounce d_switch_2 = Bounce(2,10);
Bounce d_switch_3 = Bounce(3,10);
Bounce t1_switch_4 = Bounce(4,10);
Bounce t2_switch_4 = Bounce(5,10);
Bounce t1_switch_5 = Bounce(6,10);
Bounce t2_switch_5 = Bounce(7,10);
elapsedMillis timeElapsed;
unsigned int t_interval = 200; //interval to hold buttons on for when needed
void setup() {
// put your setup code here, to run once:
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
//UPDATE
d_switch_0.update();
d_switch_1.update();
d_switch_2.update();
d_switch_3.update();
t1_switch_4.update();
t2_switch_4.update();
t1_switch_5.update();
t2_switch_5.update();
//ON-ON Switches (will treat as if ON-OFF)
//want to pulse one joy button for one direction of flip, and another for other direction
if (d_switch_0.fallingEdge()) {
Joystick.button(1,1);
timeElapsed = 0;
}
else if (d_switch_0.risingEdge()) {
Joystick.button(2,1);
timeElapsed = 0;
}
else if (d_switch_1.fallingEdge()) {
Joystick.button(3,1);
timeElapsed = 0;
}
else if (d_switch_1.risingEdge()) {
Joystick.button(4,1);
timeElapsed = 0;
}
else if (d_switch_2.fallingEdge()) {
Joystick.button(5,1);
timeElapsed = 0;
}
else if (d_switch_2.risingEdge()) {
Joystick.button(6,1);
timeElapsed = 0;
}
else if (d_switch_3.fallingEdge()) {
Joystick.button(7,1);
timeElapsed = 0;
}
else if (d_switch_3.risingEdge()) {
Joystick.button(8,1);
timeElapsed = 0;
}
if (timeElapsed > t_interval) {
Joystick.button(1,0);
Joystick.button(2,0);
Joystick.button(3,0);
Joystick.button(4,0);
Joystick.button(5,0);
Joystick.button(6,0);
Joystick.button(7,0);
Joystick.button(8,0);
}
//ON-OFF-ON switches: one joy button for top and bottom, if neither are on pulse middle button
if (t1_switch_4.fallingEdge()) {
Joystick.button(9,1);
timeElapsed = 0;
}//switch up
if (t1_switch_4.risingEdge() || t2_switch_4.risingEdge()) {
Joystick.button(10,1);
timeElapsed = 0;
}//switch middle
if (t2_switch_4.fallingEdge()) {
Joystick.button(11,1);
timeElapsed = 0;
}//switch down
if (t1_switch_5.fallingEdge()) {
Joystick.button(12,1);
timeElapsed = 0;
}//switch up
if (t1_switch_5.risingEdge() || t2_switch_5.risingEdge()) {
Joystick.button(13,1);
timeElapsed = 0;
}//switch middle
if (t2_switch_5.fallingEdge()) {
Joystick.button(14,1);
timeElapsed = 0;
}//switch down
if (timeElapsed > t_interval) {
Joystick.button(9,0);
Joystick.button(10,0);
Joystick.button(11,0);
Joystick.button(12,0);
Joystick.button(13,0);
Joystick.button(14,0);
}
}//end of voiploop