Add support for multiplexers #2
|
@ -2,21 +2,33 @@
|
||||||
// In this example, we have 3 toggle switches and 2 momentary pushbuttons.
|
// In this example, we have 3 toggle switches and 2 momentary pushbuttons.
|
||||||
// Each button is configured in a different one of the available behaviors.
|
// Each button is configured in a different one of the available behaviors.
|
||||||
|
|
||||||
#include <Bounce2.h>
|
|
||||||
#include <Joystick.h>
|
#include <Joystick.h>
|
||||||
|
|
||||||
bool debug = false;
|
bool debug = false;
|
||||||
Joystick joystick(debug);
|
Joystick joystick(debug);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
// momentary pushbutton #1
|
||||||
joystick.AddButton(9, BUTTON_PASSTHRU);
|
joystick.AddButton(9, BUTTON_PASSTHRU);
|
||||||
|
|
||||||
|
// momentary pushbutton #2 - this one will toggle on or off each time it is pushed
|
||||||
joystick.AddButton(10, BUTTON_LATCHED_MOMENTARY);
|
joystick.AddButton(10, BUTTON_LATCHED_MOMENTARY);
|
||||||
|
|
||||||
|
// a toggle switch that acts like a momentary button - every time it's toggled 'on' it briefly sends
|
||||||
|
// a keypresss
|
||||||
joystick.AddButton(11, BUTTON_PULSED);
|
joystick.AddButton(11, BUTTON_PULSED);
|
||||||
|
|
||||||
|
// like the above, but it sends its button press when toggled on *and* when toggled off
|
||||||
joystick.AddButton(12, BUTTON_PULSED_DOUBLE_ACTION);
|
joystick.AddButton(12, BUTTON_PULSED_DOUBLE_ACTION);
|
||||||
|
|
||||||
|
// again, similar to the above, but it sends two *different* button presses - 'on' will be one button, 'off' another.
|
||||||
joystick.AddButton(13, BUTTON_PULSED_DOUBLE_ACTION_SPLIT);
|
joystick.AddButton(13, BUTTON_PULSED_DOUBLE_ACTION_SPLIT);
|
||||||
|
|
||||||
|
// start up serial communication
|
||||||
joystick.Init();
|
joystick.Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
// check all the button states and send any changes
|
||||||
joystick.Update();
|
joystick.Update();
|
||||||
}
|
}
|
8
examples/multiplexer/Makefile
Normal file
8
examples/multiplexer/Makefile
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
TARGET_BOARD=arduino:avr:uno
|
||||||
|
COM_PORT=/dev/ttyACM0
|
||||||
|
|
||||||
|
build:
|
||||||
|
arduino-cli compile -b ${TARGET_BOARD}
|
||||||
|
|
||||||
|
upload:
|
||||||
|
arduino-cli upload -b ${TARGET_BOARD} -p ${COM_PORT}
|
49
examples/multiplexer/multiplexer.ino
Normal file
49
examples/multiplexer/multiplexer.ino
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
// An example sketch using the joystick library, demonstrating multiplexer support
|
||||||
|
// In this example, we have 21 buttons we want to attach.
|
||||||
|
// Unfortunately, the Arduino Uno only has 17 pins we can use!
|
||||||
|
// So we've added an 8-channel multiplexer, connected to pins A1-A5.
|
||||||
|
//
|
||||||
|
// Note: this may not be the best approach for this many simple buttons. Using an input matrix
|
||||||
|
// would require as few as 10 inputs in this scenario, for example. This is just a demo :)
|
||||||
|
|
||||||
|
#include <Joystick.h>
|
||||||
|
|
||||||
|
bool debug = false;
|
||||||
|
Joystick joystick(debug);
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// All of our digital pins and A0 are taken up with pushbuttons
|
||||||
|
joystick.AddButton(2, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(3, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(4, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(5, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(6, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(7, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(8, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(9, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(10, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(11, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(12, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(13, BUTTON_PASSTHRU);
|
||||||
|
joystick.AddButton(A0, BUTTON_PASSTHRU);
|
||||||
|
|
||||||
|
// to get more room for our inputs, we add an 8-bit multiplexer
|
||||||
|
uint8_t mux = joystick.AddMux(A1, {A2, A3, A4, A5}, 4);
|
||||||
|
|
||||||
|
// now we can add the rest of the buttons
|
||||||
|
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 1); // the last parameter is which channel/pin the input is attached to on the multiplexer
|
||||||
|
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 2);
|
||||||
|
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 3);
|
||||||
|
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 4);
|
||||||
|
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 5);
|
||||||
|
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 6);
|
||||||
|
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 7);
|
||||||
|
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 8);
|
||||||
|
|
||||||
|
joystick.Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// check all the button states and send any changes
|
||||||
|
joystick.Update();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user