Hi all,
I'm new to Arduino and I'm building a DIY gamepad using the code found here: Please help with joystick wasd - Programming Questions - Arduino Forum (thanks rontonomo).
I've modified the code slightly for IDE version 1.8.12. I'm using the Leonardo and an analog stick. I've got everything working as intended using the code below however I need a shift keystroke right after pressing either w,s,a and d (to make my ingame character run).
Here's the code:
#include <Keyboard.h>
int currStateY1;
int prevStateY1;
int currStateY2;
int prevStateY2;
int currStateX1;
int prevStateX1;
int currStateX2;
int prevStateX2;
void setup()
{
Keyboard.begin();
}
void loop()
{
int sensorValueY = analogRead(A0);
int sensorValueX = analogRead(A1);
{ // Y+
currStateY1 = sensorValueY > 404;
if (currStateY1 != prevStateY1)
{
if (currStateY1 == (sensorValueY < 50))
{
Keyboard.press('s');
}
else
{
Keyboard.release('s');
}
}
prevStateY1 = currStateY1;
}
{ //Y-
currStateY2 = sensorValueY < 594;
if (currStateY2 != prevStateY2)
{
if (currStateY2 == (sensorValueY > 973))
{
Keyboard.press('w');
}
else
{
Keyboard.release('w');
}
}
prevStateY2 = currStateY2;
}
{ //X
currStateX1 = sensorValueX > 408;
if (currStateX1 != prevStateX1)
{
if (currStateX1 == (sensorValueX < 50))
{
Keyboard.press('a');
}
else
{
Keyboard.release('a');
}
}
prevStateX1 = currStateX1;
}
{ //X-
currStateX2 = sensorValueX < 598;
if (currStateX2 != prevStateX2)
{
if (currStateX2 == (sensorValueX > 973))
{
Keyboard.press('d');
}
else
{
Keyboard.release('d');
}
}
prevStateX2 = currStateX2;
}
}
I've tried inserting shift as a keypress and as an if statement in various places in the code but I can't figure out what I'm doing wrong (my coding skills are not great). Can anyone point me in the right direction?
Thanks and have a nice day.