arduino-joystick/examples/multiplexer/multiplexer.ino

51 lines
1.9 KiB
C++

// 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>
#include <Mux.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, admux::Pinset(A2, A3, A4, A5), 4);
// now we can add the rest of the buttons
joystick.AddMuxButton(A1, mux, 0, BUTTON_PASSTHRU);
joystick.AddMuxButton(A1, mux, 1, BUTTON_PASSTHRU);
joystick.AddMuxButton(A1, mux, 2, BUTTON_PASSTHRU);
joystick.AddMuxButton(A1, mux, 3, BUTTON_PASSTHRU);
joystick.AddMuxButton(A1, mux, 4, BUTTON_PASSTHRU);
joystick.AddMuxButton(A1, mux, 5, BUTTON_PASSTHRU);
joystick.AddMuxButton(A1, mux, 6, BUTTON_PASSTHRU);
joystick.AddMuxButton(A1, mux, 7, BUTTON_PASSTHRU);
joystick.Init();
}
void loop() {
// check all the button states and send any changes
joystick.Update();
}