|
| 1 | +// Simple example application that shows how to read four Arduino |
| 2 | +// digital pins and map them to the USB Joystick library. |
| 3 | +// |
| 4 | +// The digital pins 9, 10, 11, and 12 are grounded when they are pressed. |
| 5 | +// |
| 6 | +// NOTE: This sketch file is for use with Arduino Leonardo and |
| 7 | +// Arduino Micro only. |
| 8 | +// |
| 9 | +// by Matthew Heironimus |
| 10 | +// 2015-11-20 |
| 11 | +//-------------------------------------------------------------------- |
| 12 | + |
| 13 | +#include <Joystick.h> |
| 14 | + |
| 15 | +void setup() { |
| 16 | + // Initialize Button Pins |
| 17 | + pinMode(9, INPUT_PULLUP); |
| 18 | + pinMode(10, INPUT_PULLUP); |
| 19 | + pinMode(11, INPUT_PULLUP); |
| 20 | + pinMode(12, INPUT_PULLUP); |
| 21 | + |
| 22 | + // Initialize Joystick Library |
| 23 | + Joystick.begin(); |
| 24 | +} |
| 25 | + |
| 26 | +// Constant that maps the phyical pin to the joystick button. |
| 27 | +const int pinToButtonMap = 9; |
| 28 | + |
| 29 | +// Last state of the button |
| 30 | +int lastButtonState[4] = {0,0,0,0}; |
| 31 | + |
| 32 | +void loop() { |
| 33 | + |
| 34 | + // Read pin values |
| 35 | + for (int index = 0; index < 4; index++) |
| 36 | + { |
| 37 | + int currentButtonState = !digitalRead(index + pinToButtonMap); |
| 38 | + if (currentButtonState != lastButtonState[index]) |
| 39 | + { |
| 40 | + Joystick.setButton(index, currentButtonState); |
| 41 | + lastButtonState[index] = currentButtonState; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + delay(50); |
| 46 | +} |
| 47 | + |
0 commit comments