Control a FastLed WS2811 LED Strip with 1 Push Button

Good Day,

Just to clarify, I'm new with the Arduino programming... I'm still studying, trying to learn and I'm trying to control a WS2811 Led strip with 1 push button. The idea is to press once the button to switch the Leds on, starting from led 0 to the last one (creating a chasing effect) and then pressing again the same button I would like all the Leds to go off from the last to led 0 (opposite chasing effect).

Could anyone please help me.

Attached is my hardware connection and my software configuration (which i tried to modify from a library.

#include "FastLED.h"
#include <EEPROM.h>
#define NUM_LEDS 60 
CRGB leds[NUM_LEDS];
#define PIN 6 
#define BUTTON 2

int buttonState = 0;
void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS);
  pinMode(BUTTON, INPUT);
}
void loop() { 
  buttonState = digitalRead(BUTTON);
  if (buttonState == HIGH) {
    
      for(int i=0; i<NUM_LEDS; i++) {
        leds[i] = CRGB(255,255,255);
        FastLED.show();
        delay(30);
      }
    }
  
  else {
    for(int i=0; i<NUM_LEDS; i++) {
        leds[i] = CRGB(0,0,0);
        FastLED.show();
        delay(30);
    
    }  }
}


I've never actually used WS2812's...

Your basic logic should be...

If clockwise and button pressed
Chase counterclockwise

If Counterclockwise and button pressed
Chase clockwise.

i.e. You'll need a variable to keep track of direction. That can be a type bool (1 or 0).

You might need a little delay/debouncing after the button press is detected so it doesn't get mixed up from button bounce, or if you hold the button too long.

An interrupt would be ideal because you wouldn't have to wait for the program to loop-around and read the button... But you can leave that for later, if you want to do it at all.

A toggle switch or an on/off pushbutton would simplify things. :wink: Then the switch is always telling it which way to go.

Do you have the code to reverse the chase? That might be tricky (depending on how the "forward" chase works. The data always goes through all of the LEDs in series. So if you want to chase backwards you have to keep track of the state of all the LEDs and re-write all of the LEDs. The library might do that, even if you are chasing forward so it may not even be an issue.

P.S.
YOUR BUTTON WIRING IS WRONG... You need a pull-up or pull-down resistor. The resistor is wired to pull the input low and the button is wired between the Arduino input and 5V. When the button is pressed it "overpowers" the resistor forcing the input high. (As a "side effect" current flows through the resistor.) when the button is pressed.

Or there is a built-in pull-up resistor that you can enable so you don't have to add a resistor at all. I don't have a handy schematic, but the button is wired between the input and ground so when the button is pressed the input is pulled-low. (Then you usually need to reverse your software logic to recognize a low as a button-press.)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.