2021-11-22 20:10:08 +00:00
|
|
|
// An example sketch using the joystick library, demonstrating recommended usage
|
|
|
|
// of all features.
|
2021-11-24 05:40:28 +00:00
|
|
|
// This code loads the sketch in debug mode. Connect to the Arduino's serial port at 115200 baud to read the output.
|
2021-11-22 20:10:08 +00:00
|
|
|
|
|
|
|
#include <Joystick.h>
|
|
|
|
#include <Matrix.h>
|
|
|
|
#include <Mux.h>
|
|
|
|
|
|
|
|
using namespace admux;
|
|
|
|
|
|
|
|
// Always use defines to match your pins to their logical wiring
|
|
|
|
|
|
|
|
// pins 2 and 3 are attached to a rotary encoder
|
|
|
|
#define ENCODER 2,3
|
|
|
|
|
|
|
|
// these buttons are attached directly to Arduino input pins
|
|
|
|
#define BTN1 4
|
|
|
|
#define BTN2 5
|
|
|
|
#define BTN3 6
|
|
|
|
#define BTN4 7
|
2021-11-24 05:57:41 +00:00
|
|
|
#define BTN5 A0
|
2021-11-22 20:10:08 +00:00
|
|
|
|
2021-11-24 05:40:28 +00:00
|
|
|
// pins 8-13 are attached to a 3x3 scanning input matrix. Pins 8-10 are the row pins, 11-13 are the column pins.
|
|
|
|
#define MATRIX1 8,11
|
|
|
|
#define MATRIX2 9,11
|
|
|
|
#define MATRIX3 10,11
|
|
|
|
#define MATRIX4 8,12
|
|
|
|
#define MATRIX5 9,12
|
|
|
|
#define MATRIX6 10,12
|
|
|
|
#define MATRIX7 8,13
|
|
|
|
#define MATRIX8 9,13
|
|
|
|
#define MATRIX9 10,13
|
2021-11-22 20:10:08 +00:00
|
|
|
|
|
|
|
// A multiplexer is attached to pins A1-A5
|
|
|
|
#define MUX_SIG A1 // the multiplexer's signal pin is on Analog 1
|
|
|
|
#define MUX_ADDR A2,A3,A4,A5 // the multiplexer's address pins are A2-A5, in that order
|
|
|
|
|
|
|
|
// These are 3 buttons attached to the multiplexer channels
|
|
|
|
#define MUX_BTN1 0
|
|
|
|
#define MUX_BTN2 1
|
|
|
|
#define MUX_BTN3 2
|
|
|
|
|
|
|
|
|
2021-11-24 05:40:28 +00:00
|
|
|
bool debug = true;
|
2021-12-22 22:07:57 +00:00
|
|
|
Joystick js(250, debug);
|
2021-11-22 20:10:08 +00:00
|
|
|
|
|
|
|
void setup() {
|
2021-11-24 05:40:28 +00:00
|
|
|
js.Init();
|
|
|
|
|
|
|
|
Serial.println("Adding encoder.");
|
2021-12-21 04:16:07 +00:00
|
|
|
js.AddEncoder(ENCODER, 3, ENCODER_PULSED_SPLIT);
|
2021-11-22 20:10:08 +00:00
|
|
|
|
|
|
|
// Different types of button programming are available. BUTTON_PASSTHRU simply passes on the button's
|
|
|
|
// real state, and will be the most common, but you can also change how the button works in software like so...
|
2021-11-24 05:40:28 +00:00
|
|
|
Serial.println("Adding directly connected buttons.");
|
2021-11-22 20:10:08 +00:00
|
|
|
|
|
|
|
// With BUTTON_PULSED, no matter how long the button stays held down, it will only send a button press for 250ms
|
|
|
|
// then deactivate until pressed again.
|
|
|
|
js.AddButton(BTN1, BUTTON_PULSED);
|
|
|
|
|
|
|
|
// BUTTON_PULSED_DOUBLE_ACTION does the same thing, but sends a *second* 250ms press when the button is deactivated
|
|
|
|
// This is great for an on-off toggle switch that should send an "event" in both directions.
|
|
|
|
js.AddButton(BTN2, BUTTON_PULSED_DOUBLE_ACTION);
|
|
|
|
|
|
|
|
// BUTTON_PULSED_DOUBLE_ACTION_SPLIT is just like the above, but it sends *different* button presses for the "on"
|
|
|
|
// and "off" states.
|
|
|
|
js.AddButton(BTN3, BUTTON_PULSED_DOUBLE_ACTION_SPLIT);
|
|
|
|
|
|
|
|
// BUTTON_LATCHED_MOMENTARY lets you turn a momentary pushbutton into a toggle switch.
|
|
|
|
// One press will "press and hold" the button. The next press releases the button.
|
|
|
|
js.AddButton(BTN4, BUTTON_LATCHED_MOMENTARY);
|
2021-11-24 05:57:41 +00:00
|
|
|
|
|
|
|
// This is a standard PASSTHRU button. It simply mirrors the actual state of the physical button.
|
|
|
|
js.AddButton(BTN5, BUTTON_PASSTHRU);
|
2021-11-22 20:10:08 +00:00
|
|
|
|
|
|
|
// One way to get more room for inputs is to use a scan matrix.
|
|
|
|
// See http://blog.komar.be/how-to-make-a-keyboard-the-matrix/ for a very detailed discussion of how these work.
|
|
|
|
// This is a 3x3 example, so we can get 9 buttons out of 6 inputs.
|
|
|
|
// For faster read results, always add buttons so that buttons with the same *column* are grouped together.
|
2021-11-24 05:40:28 +00:00
|
|
|
Serial.println("Adding matrix.");
|
2021-11-22 20:10:08 +00:00
|
|
|
uint8_t cols[] = {11, 12, 13};
|
2021-11-24 05:40:28 +00:00
|
|
|
Matrix* matrix = new Matrix(cols, 3);
|
2021-11-22 20:10:08 +00:00
|
|
|
js.AddMatrixButton(MATRIX1, matrix, BUTTON_PASSTHRU);
|
|
|
|
js.AddMatrixButton(MATRIX2, matrix, BUTTON_PASSTHRU);
|
|
|
|
js.AddMatrixButton(MATRIX3, matrix, BUTTON_PASSTHRU);
|
|
|
|
js.AddMatrixButton(MATRIX4, matrix, BUTTON_PASSTHRU);
|
|
|
|
js.AddMatrixButton(MATRIX5, matrix, BUTTON_PASSTHRU);
|
|
|
|
js.AddMatrixButton(MATRIX6, matrix, BUTTON_PASSTHRU);
|
|
|
|
js.AddMatrixButton(MATRIX7, matrix, BUTTON_PASSTHRU);
|
|
|
|
js.AddMatrixButton(MATRIX8, matrix, BUTTON_PASSTHRU);
|
|
|
|
js.AddMatrixButton(MATRIX9, matrix, BUTTON_PASSTHRU);
|
|
|
|
|
|
|
|
// another way to get more room for our inputs, this code adds a 16-channel multiplexer.
|
|
|
|
// It only attaches 3 buttons, but could attach up to channel 15.
|
2021-11-24 05:40:28 +00:00
|
|
|
Serial.println("Adding multiplexer.");
|
2021-11-22 20:10:08 +00:00
|
|
|
Mux* mux = new Mux(Pin(MUX_SIG, INPUT_PULLUP, PinType::Digital), Pinset(MUX_ADDR));
|
|
|
|
js.AddMuxButton(MUX_BTN1, mux, BUTTON_PASSTHRU);
|
|
|
|
js.AddMuxButton(MUX_BTN2, mux, BUTTON_PASSTHRU);
|
|
|
|
js.AddMuxButton(MUX_BTN3, mux, BUTTON_PASSTHRU);
|
|
|
|
|
2021-11-24 05:40:28 +00:00
|
|
|
Serial.println("Example joystick fully configured.");
|
2021-11-22 20:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// check all the button states and send any changes
|
|
|
|
js.Update();
|
|
|
|
}
|