Hello.I want to control my led strip (ws2812b) with a button.For example, when I push the button (a simple click on it) ,I want to change the color,but the problem is that when I push it,the led strip changes 4 to 6 colors.
See below "array_colors".For instance,when it's red and I push it,it goes to DeepSkyBlue or Olive, intead of green (the next one) color.
I print the buttonState variable to serial monitor every time to see what is going on and I understand that when I push it (I mean instantly) ,I see a lot of "1" ,instead of only once (because I press it only once).I tried to solve this problem with some boolean variables in the if statement ,but the problem didn't solve.
What can I do to figure it out?
I hope you understand my problem.
Well, I couldn't smell it either. Either way, putting delays in there doesn't sound like a good idea. It tends to make systems unresponsive to button presses.
Anyway, did my earlier post give you any clues as to what the cause of your problem might be? If not, read it again, and think a little harder.
Do you know how fast is the loop? VEEEEEEEERY FAST! So while you think you pressed the button once the loop went through k++ many times, hence multiple colour change.
Here is one example from Adafruit. A button press will make the pattern change to one of the several options available. I hope it will hep you a little more. If you have specific questions, please ask
How on earth are we supposed to know what you tried and why whatever you tried didn't work?
If you want help, shows us what you're doing.
If you just want people to google stuff for you, I recommend learning to use Google yourself; it's not hard and much quicker than asking someone else to do it.
This is not addressing the problem, which is, you need to allow loop to increase k only once per press, meaning as soon as k is increased by 1 you need to make the loop skip that scope until button is released and pressed again
I tried exactly what you say (StateChangeDetection example) and didn't work.
Code:
int lastButtonState = 0; // previous state of the button
void loop()
{
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
if(buttonState != lastButtonState)
{
if(buttonState == 1)
{
k++;
}
delay(50);
}
lastButtonState = buttonState;
pick_color();
}
I print the buttonState variable to see what happens.
In the beginning, prints always "0" ,then
when I push it (instantly) it goes to "1" for several times (because loop code runs faster than the time of the button) , BUT then it goes from "1" to "0" and "0" to "1" for several times and after a bit ,it goes to "0" ,until I push it again.