Problem with my Neopixel-Led code

Hi guys,

I have a problem with my code. If you look at it i have an built in led-effect, that turns on when i press the button. But i just dont get it how i should turn it off when i press the button again.

Do you have any idea?

-Jakob

* Listenelement

`

Your code looks like it needs the button "debounced" to filter the ringing that occurs during the physical contact of the button. The 1000 milliseconds delay you have is a type of debounce, but actually hinders reading the button press.

The preferred method of debouncing is: 1. read the button, 2. compare the button press to a previous "state" 3. if "is pressed" is different from "was pressed" 4. compare how long ago was the button pressed 5. if the length of time is long enough and the button is still in the "is pressed" state, 6. change the state.

https://docs.arduino.cc/built-in-examples/digital/Debounce/

Or, you could use a button-debounce library which does the reading and timing, and you write a function call for your button.

Also, please, do not post images of your code text. Please, copy your formatted code from your IDE, edit your post, delete the imaage, click the < CODE > button in the message box, then paste your code where you see '''type or paste code here''' This makes for batter reading and analyzing.

1 Like

Look at the state change example in the IDE. The trick is to have a variable that remembers the last state of the button. You compare that with the current state and take action if they differ. In your case, you first want to react on the button going LOW, later to you want to react again on the button going LOW.

You will need an additional variable to keep track of what your program is doing; e.g. call it effectRunning and make it of type bool. When you detect the change, you can check the variable; if it's false, set it to true, else set it to false.

If the effectRunning variable is set to true, you run the effect, else you stop it.

Ok thx for the tip and the next time i will send my stuff right. Also i allready fixed my code the day later and now it works but thx for your help :smile: .

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.