NeoPixel with button

My code for NeoPixel led isn’t working well. I want to write a code which initialize all the leds off and start them when I’m prassing on the button.

please help! Thank you.

neonLed_button_try.ino (2.34 KB)

isn't working well

Not a very good description of the problem.

You are setting the button pinMode to INPUT, do you have an external pullup (or pulldown) resistor to keep the pin from floating when the switch is open?

Please read the "how to use the forum" stickies to see how to format and post code. There is also information on how to ask an effective question and what we need to know to help you.

My code for NeoPixel led isn't working well

What should it do ? What does it do ? Please attach your code to a post to avoid the need for multiple people to download it.

#include <Adafruit_NeoPixel.h>

// Define pin to connect Neopixels to
#define PIN 13

//Define pin for pushbutton to connect to
const int BUTTON = 4;

//Initialize value to determine button press HIGH or LOW
int val = 0;

//Initilize counter
long count = 0;

//Define variable to read millis()
unsigned long start;

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:

Adafruit_NeoPixel strip = Adafruit_NeoPixel(27, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  pinMode(BUTTON, INPUT);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  Serial.begin(9600);
}

void loop() {
  
  val = digitalRead(BUTTON);
      Serial.println(val);

  unsigned long time;
  
  if (val == LOW) {
    
    if (count == 0) {
      start = millis();
    }
    Serial.println(val);
    Serial.println("if");
    colorWipe2(strip.Color(139, 0, 0), 50); // Display all Neopixels as red
    
    
    Serial.println("stop here 1");
    clearStrip();
    count++;
    Serial.println("stop here 2");
    
    if ((millis() - start) >= 10000) {
      time = (millis() - start);
      do {
      colorWipe2(strip.Color(255, 255, 255), 50); // Display all Neopixels as white
      Serial.println("stop here 3");
      clearStrip();
      val = digitalRead(BUTTON);
      } while (val == LOW && time < 10000);
    }
  }else {
    Serial.println("stop here 4");
    colorWipe(strip.Color(0, 0, 255), 50); // Display all Neopixels as blue
      colorWipe(strip.Color(255, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 255, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 255), 50); // Blue

    clearStrip();
  }
  
}

// Fill the dots one after the other with a color at speed 1
void colorWipe(uint32_t c, uint8_t wait) {
  Serial.println("stop here 5");
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(35);
  }
}

// Fill the dots one after the other with a color at speed 2
void colorWipe2(uint32_t c, uint8_t wait) {
  Serial.println("stop here 6");
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(25);
  }
}

// Turn off all pixels
void clearStrip() {
  Serial.println("stop here 7");
  for( int i = 0; i<16; i++){
    strip.setPixelColor(i, 0x000000); strip.show();
  }
}

Thanks for using code tags (+1). Can you answer the questions posed by UKHeliBob and myself?

What should it do ? What does it do ?

You are setting the button pinMode to INPUT, do you have an external pullup (or pulldown) resistor to keep the pin from floating when the switch is open?