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