arduino-joystick/examples/multiplexer/multiplexer.ino

50 lines
2.0 KiB
Arduino
Raw Normal View History

2021-11-02 19:55:35 +00:00
// 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();
}