Go Down

Topic: WS2812B_Toggle button to turn on/off 144 LED Strip (Read 462 times) previous topic - next topic


Hello Everyone,
I am new to arduino community and I have this project called FastLED.
I downloaded the code from Arduino website for FastLED.

I modified the code to run only one color pallette, white and black pattern.

Now I wanted to use a switch (Pin 2) to toggle the LEDs : Turn off when Pin 2 is pulled low and turn back on when Pin 2 is high or not low. I am using ISR to check the interrupt at pin 2 and when pulled low the LEDs should turn off and color pattern should come back on when pin 2 is NOT LOW.

The problem I am facing is when pin 2 is pulled low, the LEDs turn off and when it is pulled high it color pattern doesn't come back on. The LEDs stay low once it sees the interrupt.

Can you please refer to my code and help me with correcting the error. I would really appreciate it.

Thanks and Regards,


This is how you should post code on this forum:
Code: [Select]
#include <FastLED.h>

#define LED_PIN     5
#define LED_PIN_strip2     6
#define NUM_LEDS    144
#define BRIGHTNESS  64
#define BUTTON 2
#define LED_TYPE    WS2811


// This example shows several ways to set up and use 'palettes' of colors
// with FastLED.
// These compact palettes provide an easy way to re-colorize your
// animation on the fly, quickly, easily, and with low overhead.
// USING palettes is MUCH simpler in practice than in theory, so first just
// run this sketch, and watch the pretty lights as you then read through
// the code.  Although this sketch has eight (or more) different color schemes,
// the entire sketch compiles down to about 6.5K on AVR.
// FastLED provides a few pre-configured color palettes, and makes it
// extremely easy to make up your own color schemes with palettes.
// Some notes on the more abstract 'theory and practice' of
// FastLED compact palettes are at the bottom of this file.

CRGBPalette16 currentPalette;
TBlendType    currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;

volatile uint8_t enable;

/*// Interrupt Service Routine (ISR)*/
void switchPressed()
  switch (digitalRead (enable)){
    case LOW : leds[LED_PIN] = CRGB::Black;
    case HIGH: FastLED.show();
}  /*// end of switchPressed*/

void setup() {
    pinMode (LED_PIN, OUTPUT);   /*// so we can update the LED*/
    digitalWrite (BUTTON, HIGH);  // internal pull-up resistor
    attachInterrupt (digitalPinToInterrupt (BUTTON), switchPressed, CHANGE);  /*//* attach interrupt handler*/
    delay( 3000 ); // power-up safety delay
    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.addLeds<LED_TYPE, LED_PIN_strip2, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );
    currentPalette = CloudColors_p;
    currentBlending = LINEARBLEND;

void loop()
    static uint8_t startIndex = 0;
    startIndex = startIndex + 8; /* motion speed */
    FillLEDsFromPaletteColors( startIndex);
    FastLED.delay(1000 / UPDATES_PER_SECOND);

void FillLEDsFromPaletteColors( uint8_t colorIndex)
    uint8_t brightness = 255;
    for( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
        colorIndex += 3;

// There are several different palettes of colors demonstrated here.
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly.  All are shown here.

void ChangePalettePeriodically()
    uint8_t secondHand = (millis() / 1000) % 60;
    static uint8_t lastSecond = 99;
    if( lastSecond != secondHand) {
        lastSecond = secondHand;
        if( secondHand ==  0)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 10)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND;  }
        if( secondHand == 15)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 20)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 25)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 30)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 35)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 40)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 45)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 50)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND;  }
        if( secondHand == 55)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }

// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
    for( int i = 0; i < 16; i++) {
        currentPalette[i] = CHSV( random8(), 255, random8());

// This function sets up a palette of black and white stripes,
// using code.  Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
    // 'black out' all 16 palette entries...
    fill_solid( currentPalette, 16, CRGB::Black);
    // and set every fourth one to white.
    currentPalette[0] = CRGB::White;
    currentPalette[4] = CRGB::White;
    currentPalette[8] = CRGB::White;
    currentPalette[12] = CRGB::White;

It is better not use interrupts for detecting button presses. Simply read the button using digitalRead() from inside loop(). If you must use interrupts, you should keep the ISR as short and fast executing as possible. No calls to complex or slow functions like Serial.print() or FastLED.show(). Better to simply set the value of a volatile global variable in the ISR, and then check that variable inside loop().


First problem (besides failing to read the forum instructions) - not knowing what an interrupt is:smiley-eek:

If you forget about interrupts until you have about 30 times as much experience as you have now, you can start again to re-write your code properly:smiley-lol:

Go Up