Devices & Components
Grove - Thumb Joystick
Arduino Leonardo with Headers
40 colored male-female jumper wires
Software & Tools
Arduino IDE
poki car game(for testing)
Project description
Code
code
1#include <Keyboard.h> 2 3const int joystickX = A0; // horizontal 4const int joystickY = A1; // vertical 5const int buttonPin = 2; // joystick button for ESC 6const int threshold = 200; // sensitivity around center 7 8void setup() { 9 Serial.begin(9600); 10 Keyboard.begin(); 11 pinMode(buttonPin, INPUT_PULLUP); // button uses pull-up 12} 13 14void loop() { 15 int xValue = analogRead(joystickX); 16 int yValue = analogRead(joystickY); 17 bool buttonPressed = digitalRead(buttonPin) == LOW; // pressed = LOW 18 19 Serial.print("X: "); Serial.print(xValue); 20 Serial.print(" Y: "); Serial.print(yValue); 21 Serial.print(" Button: "); Serial.println(buttonPressed); 22 23 // LEFT / RIGHT → A / D 24 if (xValue < 512 - threshold) { // left 25 Keyboard.press('a'); 26 } else if (xValue > 512 + threshold) { // right 27 Keyboard.press('d'); 28 } else { 29 Keyboard.release('a'); 30 Keyboard.release('d'); 31 } 32 33 // UP / DOWN → W / S 34 if (yValue < 512 - threshold) { // up 35 Keyboard.press('w'); 36 } else if (yValue > 512 + threshold) { // down 37 Keyboard.press('s'); 38 } else { 39 Keyboard.release('w'); 40 Keyboard.release('s'); 41 } 42 43 // Button → ESC 44 if (buttonPressed) { 45 Keyboard.press(KEY_ESC); 46 } else { 47 Keyboard.release(KEY_ESC); 48 } 49 50 delay(20); // small delay for stability 51}
Downloadable files
code file(ino)
remember to make it a folder so its protected
game_stick_lenardo.ino
Comments
Only logged in users can leave comments