Turn Nexpixels on and off with a button (toggle)

Dear commmunity,

I am new to Arduino programming and have Neopixel LED stripe which I want to control via a button. The button is of this kind: https://fablab.ruc.dk/content/images/2017/02/IMG_9518.JPG

My goal is very basic: I simply want one push to turn on the LEDs and another push to turn it off again. I think this is called toggle. Unfortunatelly, all the tutorials with FastLED, which is the libary I would like to use, only explain how to turn the Neopixels on as long as the button is pressed. But if preassure on the button is loosened, the LEDs turn off again. This is not what I want, I would like to have a typical "light switch" effect.

So, I have been modifying the code from here: https://www.martyncurrey.com/switching-things-on-and-off-with-an-arduino/ (SwitchingThings_03a: Toggle function with simple debounce) together with the FastLED code from here: Maker kit tutorial. But I dont manage to "connect" the two features, the Neopixels and the buttons. With my code, the LEDs are initially red and flickering and while the button is pressed they turn either red without flickering or turn off. But if the button is released again, the red flickering reoccurs.

I know that this is a very basic question :o I coud manage it, if there was a proper example code for this topic, but I could not find it anywhere in the internet. This is what I programmed:

// Define the pins being used

#include "FastLED.h"
#define NUM_LEDS 10
#define pin_LED 4
CRGB leds[NUM_LEDS];

//int pin_LED = 4;
int pin_switch = 3;
 
// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;
 
boolean LEDstatus = LOW;
 
 
void setup() 
{
    FastLED.addLeds<NEOPIXEL, pin_LED>(leds, NUM_LEDS);
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");
 
    pinMode(pin_LED, OUTPUT);  
    digitalWrite(pin_LED,LOW); 
 
    pinMode(pin_switch, INPUT); 

}
 
void loop()
{

    newSwitchState1 = digitalRead(pin_switch);
    delay(1);
    newSwitchState2 = digitalRead(pin_switch);
    delay(1);
    newSwitchState3 = digitalRead(pin_switch);
 
    // if all 3 values are the same we can continue
    if (  (newSwitchState1==newSwitchState2) && (newSwitchState1==newSwitchState3) )
    {
 
        if ( newSwitchState1 != oldSwitchState ) 
        {
 
           // has the button switch been closed?
           if ( newSwitchState1 == HIGH )
           {
               if ( LEDstatus == LOW ) {   for (int i = 0; i < NUM_LEDS; i = i + 1)
                                       {
                                           leds[i] = CRGB( 255, 0, 0); 
                                           //FastLED.show();
                                        };  LEDstatus = HIGH; }
               else                    { {   for (int i = 0; i < NUM_LEDS; i = i + 1)
                                       {
                                           leds[i] = CRGB( 0, 0, 0); 
                                           //FastLED.show();
                                        };  LEDstatus = HIGH; };   LEDstatus = LOW;  }
                      
           }
           oldSwitchState = newSwitchState1;
        FastLED.show();
        }  
    }
}

Maybe someone of you knows a solution to it.

You need to separate the reading of the switches from the action; below loop demonstrates the idea ( and it might even work :wink: )

void loop()
{

  newSwitchState1 = digitalRead(pin_switch);
  delay(1);
  newSwitchState2 = digitalRead(pin_switch);
  delay(1);
  newSwitchState3 = digitalRead(pin_switch);

  // if all 3 values are the same we can continue
  if (  (newSwitchState1 == newSwitchState2) && (newSwitchState1 == newSwitchState3) )
  {

    if ( newSwitchState1 != oldSwitchState )
    {
      LEDstatus = !LEDstatus;
      oldSwitchState = newSwitchState1;
    }
  }

  // now check ledStatus and act on it
  if ( LEDstatus == false )
  {
    for (int i = 0; i < NUM_LEDS; i = i + 1)
    {
      leds[i] = CRGB( 255, 0, 0);
    }
  }
  else
  {
    for (int i = 0; i < NUM_LEDS; i = i + 1)
    {
      leds[i] = CRGB( 0, 0, 0);
    }
  }
  FastLED.show();
}

Note: boolean type variables usually have values true and false, not HIGH and LOW.

Hello sterretje,

thank you very much, this helped!

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