Last active
March 15, 2021 18:38
-
-
Save nistvan86/ad3ce33ed2b7b558e4bfdda680960e81 to your computer and use it in GitHub Desktop.
Leaning pedal for PUBG with Arduino (Pro) Micro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Minimal leaning pedal firmware for Arduino (Pro) Micro | |
// | |
// You need two pedals with a 3 connector microswitch: COM, NC, NO | |
// eg: https://www.aliexpress.com/item/4000136261950.html | |
// | |
// Wire it up as follows: | |
// - Arduino input PIN (in code LEFT_PEDAL, RIGHT_PEDAL) -> COM | |
// - VCC -> 10kOhm resistor -> NC | |
// - GND -> NO | |
// | |
// This code is currently configured for the default PUBG leaning keys (q,e) but you can | |
// change it for whatever you want (LEFT_KEY, RIGHT_KEY constants). | |
// | |
// If you use Platform I/O, the following ini file is required, | |
// then you can paste this into main.cpp: | |
// | |
// [env:micro] | |
// platform = atmelavr | |
// board = micro | |
// framework = arduino | |
// lib_deps = | |
// Keyboard | |
#include <Arduino.h> | |
#include <Keyboard.h> | |
char LEFT_KEY = 'Q'; | |
char RIGHT_KEY = 'E'; | |
int DEBOUNCE_DELAY = 50; | |
int LEFT_PEDAL = 15; | |
int RIGHT_PEDAL = 20; | |
unsigned long currentMillis = 0; | |
int leftState = LOW; | |
int rightState = LOW; | |
int leftKeyState = -1; | |
int rightKeyState = -1; | |
unsigned long leftLastDebounceTime = 0; | |
unsigned long rightLastDebounceTime = 0; | |
void setup() { | |
Keyboard.begin(); | |
pinMode(LEFT_PEDAL, INPUT); | |
pinMode(RIGHT_PEDAL, INPUT); | |
} | |
void loop() { | |
currentMillis = millis(); | |
leftState = digitalRead(LEFT_PEDAL); | |
rightState = digitalRead(RIGHT_PEDAL); | |
// Handle left pedal debounced | |
if ((currentMillis - leftLastDebounceTime) > DEBOUNCE_DELAY) { | |
if (leftState == LOW && leftKeyState == -1) { | |
// Left pedal was pressed | |
Keyboard.press(LEFT_KEY); | |
leftLastDebounceTime = currentMillis; | |
leftKeyState = -leftKeyState; | |
} else if (leftState == HIGH && leftKeyState == 1) { | |
// Left pedal was released | |
Keyboard.release(LEFT_KEY); | |
leftLastDebounceTime = currentMillis; | |
leftKeyState = -leftKeyState; | |
} | |
} | |
// Handle right pedal debounced | |
if ((currentMillis - rightLastDebounceTime) > DEBOUNCE_DELAY) { | |
if (rightState == LOW && rightKeyState == -1) { | |
// Right pedal was pressed | |
Keyboard.press(RIGHT_KEY); | |
rightLastDebounceTime = currentMillis; | |
rightKeyState = -rightKeyState; | |
} else if (rightState == HIGH && rightKeyState == 1) { | |
// Right pedal was released | |
Keyboard.release(RIGHT_KEY); | |
rightLastDebounceTime = currentMillis; | |
rightKeyState = -rightKeyState; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment