NeoPixle LED changes color as per button click.

Hey guys,

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?

Thank you so much...........

I assume you have downloaded and installed the Adafruit_Neopixel library to the Arduino IDE.

  1. Open IDE
  2. Goto: File -> Examples -> Adafruit Neopixel -> buttoncycler
  3. Edit as needed and Save As

It sounds like you only want the colorWipe function.

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.

Thank you for your feedback.

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.

I am using momentary push button. Thanks for your help.

Can you tell me what is wrong in this program?

I try to set button for one click, double click, hold button kind of stages.

neopixle.ino (2.68 KB)

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.

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.

#include <Adafruit_NeoPixel.h>

#define buttonPin   2    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define ledPin1    6    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 7

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, ledPin1, NEO_GRB + NEO_KHZ800);


// LED variables
boolean ledVal1 = false;    // state of LED 1

//=================================================

void setup() {
   // Set button input pin
   pinMode(buttonPin, INPUT);
   digitalWrite(buttonPin, HIGH );
   // Set LED output pins
   pinMode(ledPin1, OUTPUT);
   digitalWrite(ledPin1, ledVal1);  
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(1000);
  }
} 
void loop() {
   // Get button event and act accordingly
   int b = 0;
   if (b ==0) normalState();
   if (b == 1) clickEvent();
   if (b == 2) doubleClickEvent();
   if (b == 3) holdEvent();
   if (b == 4) longHoldEvent();
}

//=================================================
// Events to trigger


void normalState() {
   ledVal1 = !ledVal1;
  if (!ledVal1) {
    colorWipe(strip.Color(0, 255, 0), 50); // Green
    delay (5000);
   digitalWrite(ledPin1, ledVal1);
  }
}

void clickEvent() {
   ledVal1 = !ledVal1;
  if (!ledVal1) {
    colorWipe(strip.Color(0, 0, 255), 50); // Blue
    delay (5000);
   digitalWrite(ledPin1, ledVal1);
  }
}

void doubleClickEvent() {
  ledVal1 = !ledVal1;
  if (!ledVal1) {
    colorWipe(strip.Color(255, 0, 0), 50); // Red
 delay (5000);
   digitalWrite(ledPin1, ledVal1);
}
}

void holdEvent() {
  ledVal1 = !ledVal1;
  if (!ledVal1) {
     colorWipe(strip.Color(0, 213, 255), 50);  // Turquoise
     delay (5000);
   digitalWrite(ledPin1, ledVal1);
}
}

void longHoldEvent() {
   ledVal1 = !ledVal1;
  if (!ledVal1) {
    colorWipe(strip.Color(0,255, 255), 50); // Aqua
 delay (5000);
   digitalWrite(ledPin1, ledVal1);
}
}

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?

...and please show the circuit.

Marky63:

 void colorWipe(uint32_t c, uint8_t wait) {

for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(1000);

A few things jump out at me:

  1. 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)

  2. 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.

  3. Use if / else if / else statements --or-- switch / case

  4. Why do you digitalWrite(ledPin1, ledVal1) if this is a NeoPixel? NeoPixels use 'strip.show()' as shown in the colorWipe() function?

  5. 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.

If you're trying to adapt another Button Function type sketch like

this

or this...

then it won't work because those are for regular LEDs. NeoPixels / WS28xx LEDs are different.

Hey guys,

Thank you for your help for project. As surveyranger mentioned in post I tried it and it is working.

You guys are Great. Working on next step of project so, if i need any help I post a new problem there.

Once again thank you so much...