FastLed pause with a button?

Hi All,

I am using an Arduino Uno to run LEDs for a diy style Acrylic lamp.

At the moment it simply cycles through colours.

I want to add a physical button on the base that allows me to stop / pause the color at any specific point.

Does anyone know how to code this / what setup I need to add the switch into the Uno?

Thank you

#include <FastLED.h>
#define DATA_PIN 7
#define LED_COUNT 5
#define wait_time 10

#define COLOR_ORDER GRB

CRGB leds[LED_COUNT];

void setup(){
  FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, LED_COUNT);

}

void loop(){

//start from red
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

  int r = 255;
  int g = 0;
  int b = colorStep;

  // Now loop though each of the LEDs and set each one to the current color
  for(int x = 0; x < LED_COUNT; x++){
      leds[x] = CRGB(r,g,b);
  }

  // Display the colors we just set on the actual LEDs
      delay(10); 
      FastLED.show();
      }

    //into blue
      for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = colorStep;
      int g = 0;
      int b = 255;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(10); 
  FastLED.show();
  }

//start from blue
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

      int r = 0;
      int g = colorStep;
      int b = 255; 

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(10); 
  FastLED.show();
  }

//into green
  for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = 0;
      int g = 255;
      int b = colorStep; 

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
  }

//start from green
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

      int r = colorStep;
      int g = 255;
      int b = 0;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
  }

//into yellow
  for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = 255;
      int g = colorStep;
      int b = 0;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
  }


} //end main loop

Hi, have you got a switch? Have you tried to see if you can get a switch working without the leds first?

The best way to wire a switch is between ground and a digital pin, then you use INPUT_PULLUP to enable the arduino internal pull-up resistors.

Here is an example of how to read a button press, though, it doesn’t use input pull up.

You should make use of a state machine to interpret the button presses.

All of this is googlable. If you post code and have specific question about code you have written, i can help you.

Here, try this
I’ve assumed that when the button is pressed it’s state changes to HIGH.(This may vary as per your hardware setup)

See what your button does when pressed.

If this code is doing wiered stuff, just replace HIGH everywhere in the code with LOW.

#include <FastLED.h>
#define DATA_PIN 7
#define LED_COUNT 5
#define wait_time 10

#define COLOR_ORDER GRB

CRGB leds[LED_COUNT];

int buttonPin = 8;  //Push button at digital pin 8

void setup(){
  FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, LED_COUNT);
  pinMode(buttonPin, INPUT);    //Pin 7 as input
}

void loop(){
  
//start from red
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

  int r = 255;
  int g = 0;
  int b = colorStep;

  // Now loop though each of the LEDs and set each one to the current color
  for(int x = 0; x < LED_COUNT; x++){
      leds[x] = CRGB(r,g,b);
  }

    // Display the colors we just set on the actual LEDs
    delay(10); 
    FastLED.show();
    while(digitalRead(7)==HIGH){
    delay(10);         //Wait here unless button is released
    }
  }

    //into blue
      for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = colorStep;
      int g = 0;
      int b = 255;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(10); 
  FastLED.show();
    while(digitalRead(7)==HIGH){
    delay(10);         //Wait here unless button is released
    }
  }

//start from blue
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

      int r = 0;
      int g = colorStep;
      int b = 255; 

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);          
          }

      // Display the colors we just set on the actual LEDs
  delay(10); 
  FastLED.show();
    while(digitalRead(7)==HIGH){
    delay(10);         //Wait here unless button is released
    }
  }

//into green
  for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = 0;
      int g = 255;
      int b = colorStep; 

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
    while(digitalRead(7)==HIGH){
    delay(10);         //Wait here unless button is released
    }
  }

//start from green
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

      int r = colorStep;
      int g = 255;
      int b = 0;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
    while(digitalRead(7)==HIGH){
    delay(10);         //Wait here unless button is released
    }
  }

//into yellow
  for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = 255;
      int g = colorStep;
      int b = 0;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
    while(digitalRead(7)==HIGH){
    delay(10);         //Wait here unless button is released
    }
  
  }
  
} //end main loop

Hope this helps :slight_smile:

Thank you, I’ll give that a shot.