Add example sketch.
This commit is contained in:
parent
1a0ecea44a
commit
0cdaf634a7
55
examples/switch_panel.ino
Normal file
55
examples/switch_panel.ino
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// 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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user