Push button to control led strip

Push button to control led strip

Hello,
I’m working on a project. But I can’t make it work and after many try I didn’t find where I made mistake

My project is quite simple. I want to change color of led strip with push buttons

I try to made this with a

arduino
ws2812b led strip (118 leds in zigzag)
Two push button
Breadbord

The Idea is :

When no button press : all the leds are yellow

When button 1 is press : Set green, yellow, green yellow

When button 2 is press :all the leds are Pink

I try different setup for the button and no one work
I don’t know if It’s my code or my setup who is wrong

That why I need your help.

Here my code :

    #include <FastLED.h>
    #define LED_PIN 2
    #define BUT_PIN 6
    #define BUT_PIN2 8
    #define NUM_LEDS 118

   byte buttonState, lastButtonState = HIGH; //Unpressed
   byte buttonState2, lastButtonState2 = HIGH; //Unpressed
    CRGB leds[NUM_LEDS];

    void setup() {
     FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
     FastLED.setBrightness(170);
     pinMode(LED_PIN, OUTPUT); 
     pinMode(BUT_PIN, INPUT);
     pinMode(BUT_PIN2, INPUT);
      
    }
void loop()
{
  buttonState = digitalRead(BUT_PIN);
  if (buttonState != lastButtonState)
  {
    lastButtonState = buttonState;
    delay(10); //Blocking debounce

    if( buttonState == LOW)
      {
      digitalWrite(LED_PIN, HIGH);
     
      for (int i = 0; i <= 26; i++) {
        leds[i] = CRGB ( 255,255,0);
      }   
      for (int i = 26; i <= 40; i++) {
         leds[i] = CRGB (0,255,0);
      }
      for (int i = 40; i <= 66; i++) {
        leds[i] = CRGB ( 255,255,0);
      }
      for (int i = 66; i <= 92; i++) {
        leds[i] = CRGB (0,255,0);
      }
      for (int i = 92; i <= 118; i++) {
        leds[i] = CRGB ( 255,255,0);

      }
      FastLED.show();
      }

      else if( buttonState == HIGH)
      {
      digitalWrite(LED_PIN, HIGH);
     
      for (int i = 0; i <= 118; i++) {
        leds[i] = CRGB ( 255,255,0);

      }
      FastLED.show();
      }
  }
  
    buttonState2 = digitalRead(BUT_PIN2);
  if (buttonState2 != lastButtonState2)
  {
    lastButtonState2 = buttonState2;
    delay(10); //Blocking debounce

    if( buttonState == LOW)
      {
      digitalWrite(LED_PIN, HIGH);
     
      for (int i = 0; i <= 118; i++) {
        leds[i] = CRGB ( 255,0,0);
     
      }
      FastLED.show();
      }

      else if( buttonState2 == HIGH)
      {
      digitalWrite(LED_PIN, HIGH);
     
      for (int i = 0; i <= 118; i++) {
        leds[i] = CRGB ( 255,255,0);

      }
      FastLED.show();
      }
  }
}

And the setup

Thank you for help :slight_smile:

Your switches appear they are across the battery, S H O R T I N G the battery terminals !

Pick one of these switch circuits.

Proper LED strip wiring.

hey are you solved it yet? can you help me doing my project I am new to arduino, also interested this, i want to make default effect is rainbow fade and button to change it to single colors or may be brightness too. thank you!

chinhnguyen:
hey are you solved it yet? can you help me doing my project I am new to arduino, also interested this, i want to make default effect is rainbow fade and button to change it to single colors or may be brightness too. thank you!

Please start your own thread.

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components. Posting images:

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]