56 lines
1.3 KiB
C++
56 lines
1.3 KiB
C++
// An example sketch using the joystick library.
|
|
// In this example, we have 5 toggle switches that are configured
|
|
// as double-action pulsed buttons - that is, whenever they are toggled, they
|
|
// emit a short button press.
|
|
|
|
#include <Joystick.h>
|
|
|
|
bool debug = false;
|
|
Joystick joystick(debug);
|
|
|
|
// Number of connected buttons.
|
|
int num_buttons = 5;
|
|
// Last known state of each pin.
|
|
int states[10] = {-1, -1, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
|
|
|
|
void parsePins(bool init=false) {
|
|
// Input: figure out which pins have changed.
|
|
bool something_pressed = false;
|
|
for (int i = 2; i < 2 + num_buttons; i++) {
|
|
int newstate = digitalRead(i);
|
|
if (newstate != states[i]) {
|
|
states[i] = newstate;
|
|
if (!init) {
|
|
joystick.PressButton(i-2);
|
|
something_pressed = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Output: Actually press the appropriate buttons, wait, then clear them all.
|
|
if (something_pressed) {
|
|
joystick.Write();
|
|
joystick.ReleaseAllButtons();
|
|
joystick.Write();
|
|
}
|
|
}
|
|
|
|
void InitInputs() {
|
|
for (int i = 2; i < 2 + num_buttons; i++) {
|
|
pinMode(i, INPUT_PULLUP);
|
|
}
|
|
parsePins(true);
|
|
}
|
|
|
|
void setup() {
|
|
pinMode(13, OUTPUT);
|
|
digitalWrite(13, LOW);
|
|
joystick.Init();
|
|
InitInputs();
|
|
}
|
|
|
|
void loop() {
|
|
parsePins();
|
|
if (debug) delay(900);
|
|
}
|