Debounce keyboard write in games

Hello!

I have some troubles. I want to press button on my little device that simulate a keyboard presses. In a notepad++ document everything is fine, but two out of three games it doesn’t work very well. It runs very unsteady, sometimes there is no input and sometimes there a two (in this case I want to navigate through menu). I have tested some debounce methods, but nothing work at the moment.

#include "Keyboard.h"

bool pressState = false;

const int joySW_pin = 10; // digital pin connected to switch output
const int joyX_pin = 8; // analog pin connected to X output
const int joyY_pin = 9; // analog pin connected to Y output

void setup() {
	Serial.begin(9600);
}

void loop() {
	if (analogRead(joyY_pin) > 1000 && !pressState) {
		pressState = true;
		while (pressState)
		{
			if (analogRead(joyY_pin) < 700)
				pressState = false;
		}
		Serial.println(analogRead(joyY_pin));
		Keyboard.write(0xD9);
	}
}

pressState is useless in your code since you enter and leave the loop with it being false and have an active release wait

Your loop at the moment is the same as

void loop() {
    if (analogRead(joyY_pin) > 1000) {  
        while (analogRead(joyY_pin) >= 700) ; // wait 
        // Serial.println(analogRead(joyY_pin)); // comment this out
        Keyboard.write(0xD9);
    }
}

Try by commenting your serial print out

Hi jml,

thank you for your post. I will try it without the Serial lines, but I think I have try it without it and there I have also problems. What i forgot to mention is that I use a joystick in this example (maybe you have recognized it). Maybe a video will show my problem better.

if I understand well you want to send a key after the joystick (yes I recognized that) goes all the way up or right and had come back somewhat to the center

So you are sending KEY_DOWN_ARROW which is not a typical ASCII character. I would try without Keyboard.write() and use Keyboard.press() and Keyboard.release() instead to see if you get better results.

Also if you don't have an American keyboard, the mapping might be off for some keys.