For my project, I want any movement detected by a joystick to cause the built in LED to illuminate, and for it to de-illuminate only after another movement has been detected. (Toggling the LED by moving a joystick). The code currently lights up the LED when the joystick is moved and prints a value corresponding to the LED state, but I have no clue how to toggle it on and off. Any help would be very appreciated!
on a higher level, What you need to do, is set a threshold value which identifies ‘movement’ in whichever axis you’re working.
When that delta value from the previous joystick value is exceeded, turn the LED on, start a millis() timer and re-set the previous threshold ‘old’ value, then after the desired period (2-secs), turn the LED off… of course if the joystick value keeps moving/triggering, you’d restart the LED timer, so it only goes off 2 seconds after the last movement.
It works for my purposes, though, I noticed that when the joystick is moved, the LED only toggles when ledState = !ledState. This means that since the value of ledState is switching constantly (010101010), you essentially have to release from the joystick exactly when the code lands on ledState = !ledState for the LED to toggle. (Sometimes it does not and ledState lands on it's current value) I've increased the delay value so that it works for my project, yet I'm stull curious if there is a way to toggle the LED using a joystick that is completely independent of time.