From 0cdaf634a70a0b367fbe2a61dd6be05a4ec8333a Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 4 Nov 2015 22:16:07 -0500 Subject: [PATCH] Add example sketch. --- examples/switch_panel.ino | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/switch_panel.ino diff --git a/examples/switch_panel.ino b/examples/switch_panel.ino new file mode 100644 index 0000000..72867d1 --- /dev/null +++ b/examples/switch_panel.ino @@ -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 + +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); +}