Closed hapklaar closed 1 year ago
I have not had a chance to test this theory, but it appears you are sending a message to the host computer on every loop, even when the state of the joystick has not changed. To fix this you will need to only call setXAxis
(or any other library function) when the value is different from the last time you called the function. The FunduinoJoystickShield.ino example shows how this can be done.
Thanks for following up! I added a value check to only send updates when the new value was at least 8 higher or lower than previous (the value jumps around a bit) and can see it now doesn't send updates anymore when the pot is not touched:
...
void loop()
{
bool sendUpdate = false;
const int currentXAxisValue = analogRead(X1);
int difference = lastXAxisValue - currentXAxisValue;
Serial.println(difference);
if (difference > 8 || difference < -8)
{
controller.setXAxis(currentXAxisValue - 512);
lastXAxisValue = currentXAxisValue;
sendUpdate = true;
Serial.println("Sending update!");
}
if (sendUpdate)
{
controller.sendState();
}
...
But unfortunately, the device still immediately wakes the PC when connected :(
If you just load one of the sample applications to the device (like the blink example or the Keyboard Message example) does the device keep the PC awake or only when you have a program that uses the Joystick library?
or only when you have a program that uses the Joystick library?
This was it! I still had the 'Game Controllers' app open. After closing that the PC no longer woke up.
The same is happening when I leave the serial monitor open and have constant serial output from the Arduino. Didn't know this was a thing :)
Thanks!
Better late than never. I had the same problem when I wrote some joystick software for my own project. Never could figure out a solution. Now several years later I decided to update the code a bit after some modifications to the hardware and I stumble across this post. I feel like an idiot. 3 lines of extra code to check if the value has actually changed solves this issue. And to think I spent almost a week on debugging this all those years ago. Thanks for posting for me to find!
Description of Issue
I built a simple 1 axis controller using your library and it works great. Problem is that when it is connected to USB and I put my PC to sleep, it wakes my computer after a couple of seconds.
Technical Details
Sketch File that Reproduces Issue
Wiring Details
Probably not relevant.
Additional context