Go Down

Topic: Push Button on/off toggle for W2812B LEDSTRIP (Read 226 times) previous topic - next topic


I'm trying to get a push-button to turn on and off my W2812B LEDstrip connected to an Arduino Uno. Basically, if you press the button once it will turn on or off depending on its current state. To clarify, I do not want to have to hold the button down. Simply press it once and the Lights will come on in a specified pattern and if pressed a second time will turn off the lights. The code I've attempted to use and create has not worked however. I'm not sure how to fix it. I'm very new to ardunio coding and could very easily be making mistakes. The code I have is as follows
Code: [Select]
#include <bitswap.h>
#include <chipsets.h>
#include <color.h>
#include <colorpalettes.h>
#include <colorutils.h>
#include <controller.h>
#include <cpp_compat.h>
#include <dmx.h>
#include <FastLED.h>
#include <fastled_config.h>
#include <fastled_delay.h>
#include <fastled_progmem.h>
#include <fastpin.h>
#include <fastspi.h>
#include <fastspi_bitbang.h>
#include <fastspi_dma.h>
#include <fastspi_nop.h>
#include <fastspi_ref.h>
#include <fastspi_types.h>
#include <hsv2rgb.h>
#include <led_sysdefs.h>
#include <lib8tion.h>
#include <noise.h>
#include <pixelset.h>
#include <pixeltypes.h>
#include <platforms.h>
#include <power_mgt.h>
#define LED_PIN     6
#define NUM_LEDS    12
#define BUTTON_PIN 2
int myLEDs0[ ]={0,1,2};
int myLEDs1[ ]={3,4,5};
int myLEDs2[ ]={6,7,8};
int myLEDs3[ ]={9,10,11};
bool showLights = true;
bool lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;   
void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
void loop() {
  static bool currentButtonState = digitalRead(BUTTON_PIN);
  // you may want to add some debouncing to the line above.
  if (currentButtonState != lastButtonState) {
    if (currentButtonState) {
      showLights = !showLights;
      lastDebounceTime = millis();
    if ((millis() - lastDebounceTime) > debounceDelay) {
    lastButtonState = currentButtonState;
  if (showLights) {
      for(int i=0;i<3;i++)
      {leds[myLEDs0[i]]= CRGB(0,0,200);
      for(int i=0;i<3;i++)
      {leds[myLEDs1[i]]= CRGB(50,75,100);
       for(int i=0;i<3;i++)
      {leds[myLEDs2[i]]= CRGB(255,150,0);
       for(int i=0;i<3;i++)
      {leds[myLEDs3[i]]= CRGB(255,0,255);}
  }else {
    for (int i = 0; i <= 12; i++) {
    leds[i] = CRGB ( 0, 0, 0);
  }   }


Dec 11, 2019, 05:00 am Last Edit: Dec 20, 2019, 06:18 am by IoT_hobbyist
Firstly, your debouncing function is incorrect. You can refer to the deboucing example.

I would like to recommend using this library to make it easier. Code for debouncing and detecting the event is very simple

Code: [Select]
#include <ezButton.h>

ezButton button(7);  // create Button object that attach to pin 7;

bool showLights = true;

void setup() {
  button.setDebounceTime(50); // set debounce time to 50 milliseconds

void loop() {
  button.loop(); // MUST call the loop() function first

    showLights = !showLights;

  // add your code to control led here

Secondly, you are using delay() function. This will make Arduino miss some pressing events. I recommend to use timestamp instead, see BlinkWithoutDelay example

Go Up