I am a beginner in Arduino programming. I would like to ask you about NeoPixle LED. I am working on project where I require to change color or neo pixle LED as per I press button.
For example,
I press first time show me Blue color.
If i press it second time, it shows me a Red color,
And at last when i click it 4th time It shows me a green color and it is stay green until cycle gone reset. Than its start from first click.
Can you guys give me a idea how can i deal with it?
I already checked it, but it is not what I am looking for. It define only on and off state of button and change color according that.
While I need a code where i press button and it change color. I try to use if else statement but how can i set a button click to counter? As well after 4 click counter need to be reset.
There's just a lot of other stuff in there you don't need. The void loop() monitors for button presses and advances case when pressed.
case 0: colorWipe(strip.Color(255, 0, 0), 1); // Red
case 1: colorWipe(strip.Color(0, 0, 255), 1); // Blue
case 2: colorWipe(strip.Color(0, 255, 0), 1); // Green
case 3: colorWipe(strip.Color(0,0,0), 1); // Black / Off
The button cycles through the different cases: 0,1,2,3,0,1,2,3... In this example it's set at 1ms intervals.
What type of button are you using? Momentary or seal-in? The program is written for momentary (push down, button LOW -- let go, button HIGH). The pin that is attached to the button should be pulled HIGH with either internal pull-up resistor or external pull-up resistor.
Marky63:
Can you tell me what is wrong in this program?
Not exactly. Check out the How to use this forum for ways to post code and pictures (starting at # 7). Posting it with code tags makes it easier for people to read the code without having to download your file & open it up on their IDE. This is especially true for people using their phone. Some of these guys get pretty cranky when they see that someone didn't read the "How to use..." because they see it a lot. They (and I) want to help so help us help you.
A picture or drawing of your setup would also be helpful. Websites like TinkerCAD will let you "build" a circuit. I use Fritzing for simple setups, like an Arduino, LED, and button. However, the picture quality is low so it's better to draw out the circuit or use a different program. Sometimes a drawing is the quickest way.
The Arduino Reference page has helped me to understand and follow code.
Marky63:
it is compiling without error but not working on circuit.
"not working" is not very descriptive.
Much better to describe what you expected it to do, what it actually did and how those two differ.
Also, when you have your code in the IDE, autoformat it. CTRL-T
This lines things up in a logical way, making it easier for us to read and much easier to find mismatched curly brackets.
Marky63:
sorry for that. i post my code here and you will help me out with my code. i am not sure it is compiling without error but not working on circuit.
This is where a drawing/picture of the circuit would help. What type of microcontroller are you using?
In the NeoPixel Examples in the IDE, it shows to use the wait from the uint8_t wait in the delay(). You put 1000 in the delay, but also put a value in the call to the colorWipe function.
Example: colorWipe(strip.Color(0,255, 255), 50) is (R=0, G=255, B=255), wait=50ms)
You 'int b = 0' in the loop(), but I didn't see anything that incremented (b++) when a button is pressed --or-- that 'b' is even referenced anywhere else in the code.
Use if / else if / else statements --or-- switch / case
Why do you digitalWrite(ledPin1, ledVal1) if this is a NeoPixel? NeoPixels use 'strip.show()' as shown in the colorWipe() function?
If you want the microcontroller to detect number of pushes and length of pushes, then you need to create time measurements along with increment (b++) statements.