Hello, I try to emulate WASD keys with a joystick and an Arduino Leonardo Board. I managed to find a little tutorial and make it work, however it had high latency.
So I have searched for other solutions and uploaded several codes to the board (like trying the joystick ) but none succeeded.
So I went back to the first code I found, uploaded it and no keyboard function doesn't work anymore. Here is the code , it's a little different from the one I found because I wanted to check what's wrong:
#include<Keyboard.h>
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output
int x, y;
void setup()
{
pinMode(SW_pin, INPUT); // SW pin set as input
digitalWrite(SW_pin, HIGH); // A high value is written to SW pin
Serial.begin(115200);
Keyboard.begin();
}
void loop()
{
x = analogRead(X_pin); // output of X_pin is read
if (x > 1000) // check whether the value of x = 1023
{
Keyboard.press(KEY_LEFT_ARROW);
Keyboard.print("www.deagware.xyz");
Keyboard.write(65);
Serial.println("Left:");
// key moves left 215
}
else
{
Keyboard.release(KEY_LEFT_ARROW); // release the key
}
x = analogRead(X_pin); // output of X_pin is read
if (x == 0) // check whether the value of x = 0
{
Serial.println("Right:");
Keyboard.press(KEY_RIGHT_ARROW); // key moves right 216
}
else
{
Keyboard.release(216); // release the key
}
y = analogRead(Y_pin); // output of Y_pin is read
if (y == 1023) // check whether the value of y = 1023
{
Serial.println("Down:");
Keyboard.press(KEY_DOWN_ARROW); // key moves down 217
}
else
{
Keyboard.release(217); // release the key
}
y = analogRead(Y_pin); // output of Y_pin is read
if (y == 0) // check whether the value of y = 0
{
Serial.println("Up:");
Keyboard.press(KEY_UP_ARROW); // key moves up 218
}
else
{
Keyboard.release(218); // release the key
}
int z = digitalRead(SW_pin); // read the value of SW pin
if (z == 0) //check whether the value of z = 0
{
Serial.println("Enter:");
Keyboard.println(); //enter key is pressed
}
//delay(500);
}