MHeironimus / ArduinoJoystickLibrary

An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.
GNU Lesser General Public License v3.0
2.06k stars 403 forks source link

Lags and missing events on Raspberry Pi Zero W #269

Closed Pilou44 closed 1 year ago

Pilou44 commented 1 year ago

Description of Issue

I've created a arcade stick using this library version 2.1.1 and an Arduino Micro. It works great on my Windows 11 PC and on a Raspberry Pi 3. But if I connect it to my Raspberry Pi Zero, I have input lags and missing events if button are not pressed long enough. I can see it just by running cat /dev/input/event0

Technical Details

Code available here : https://github.com/Pilou44/ArduinoJoystick/blob/main/stick_arcade_1_player/stick_arcade_1_player.ino

Wiring Details

Schematic_stick arcade_2023-02-04

MHeironimus commented 1 year ago

In looking at stick_arcade_1_player.ino, my guess is the Arduino is sending too much data to the Raspberry Pi Zero. I would recommend increasing the delay to 50 milliseconds (i.e., delay(50); and only calling pressButton or releaseButton when the value actually changes. You will want to keep track of the last know state of each button or axis and only call pressButton or releaseButton when the state changes.

Pilou44 commented 1 year ago

That's it! Thanks a lot. Now I save button states and use setButton only when necesary and it works great. I've increased delay to 30ms, I was afraid of missing frames with 50ms (even if I'm not sure difference would be humanly perceptible). Thank you very much.