Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LIBRARY HERE: https://github.com/MHeironimus/ArduinoJoystickLibrary
- // Code by Shoyun, 2020-08-17
- #include <Joystick.h>
- // Create the Joystick
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
- JOYSTICK_TYPE_MULTI_AXIS, 4, 0,
- false, false, true, false, false, false,
- false, true, false, false, false);
- // Constant that maps the physical pin to the joystick button.
- // Pin 2 = Button 1
- // Pin 3 = Button 2
- // Pin 4 = Button 3
- // Pin 5 = Button 4
- // Last state of the buttons
- int lastButtonState[4] = {0,0,0,0};
- void setup() {
- Joystick.setThrottleRange(0, 1023);
- Joystick.setZAxisRange(0, 1023);
- // Initialize Button Pins
- pinMode(2, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- pinMode(5, INPUT_PULLUP);
- // Initialize Joystick Library
- Joystick.begin();
- }
- void loop() {
- // Read pin values
- for (int index = 0; index < 4; index++)
- {
- int currentButtonState = !digitalRead(index + 2);
- if (currentButtonState != lastButtonState[index])
- {
- Joystick.setButton(index, currentButtonState);
- lastButtonState[index] = currentButtonState;
- }
- lastButtonState[index] = currentButtonState;
- }
- Joystick.setThrottle(analogRead(A0));
- Joystick.setZAxis(analogRead(A1));
- delay(50);
- }
Add Comment
Please, Sign In to add comment