Hello - I am trying to use FastLED to control brake lights for my

Hello - I am trying to use FastLED to control brake lights for my motorcycle. I cannot figure out how to make the lights loop once and stay lit when I hold down a button to act as a brake light. It keeps looping…

#include "FastLED.h"

// fast led constants
#define DATA_PIN    3        // change to your data pin
#define COLOR_ORDER GRB      // if colors are mismatched; change this
#define NUM_LEDS    256          // change to the number of LEDs in your strip
#define FRAMES_PER_SECOND  120
#define LED_TYPE    WS2812B
#define BRIGHTNESS          200
#define DIM         10


// constant variables
const int buttonPin1 = 5;     // the number of the pushbutton pin
const int buttonPin2 = 6;
const int buttonPin3 = 7;
const int buttonPin4 = 10;



// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int oldButtonState3 = 0;

// this creates an LED array to hold the values for each led in your strip
CRGB leds[NUM_LEDS];

void setup()
{
    delay(3000); // 3 second delay for recovery

  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds,NUM_LEDS).setCorrection(TypicalLEDStrip);


  // the wiki features a much more basic setup line:
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);

Serial.begin(9600);

// initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);


}

 void loop()



{
 

// read the state of the pushbutton value:
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
    buttonState3 = digitalRead(buttonPin3);
    buttonState4 = digitalRead(buttonPin4);





 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
          if (buttonState1 == HIGH) {


//LEFT TURN SIGNAL

 FastLED.setBrightness(BRIGHTNESS);

// left tail light black
        for (int f = 192; f <= 255; f++)
              leds[f] = CRGB::Black;


       


// Set the first led back to black
        for (int a = 192; a <= 220; a++)
        leds[a] = CRGB::Black;
        FastLED.show();
        delay(200);


// FIRST ARROW RED

byte firstArrow20[] =
{192,193,194,195,196,197,198,199,201,202,203,204,205,206,210,211,212,213,219,220};

for (int g = 0; g<20; g++) {
 leds[firstArrow20[g]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// FIRST ARROW BLACK

for (int g = 0; g<20; g++) {
 leds[firstArrow20[g]] = CRGB::Black;
}
FastLED.show();
 delay (30);



// SECOND ARROW RED

byte secondArrow8[] = {207,209,221,227,228,218,214,200};

for (int h = 0; h<8; h++) {
 leds[secondArrow8[h]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// SECOND ARROW BLACK

for (int h = 0; h<8; h++) {
 leds[secondArrow8[h]] = CRGB::Black;
}
FastLED.show();
 delay (30);


// THIRD ARROW RED

byte thirdArrow8[] = {208,222,226,236,235,229,217,215};

for (int k = 0; k<8; k++) {
 leds[thirdArrow8[k]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// THIRD ARROW BLACK

for (int k = 0; k<8; k++) {
 leds[thirdArrow8[k]] = CRGB::Black;
}
FastLED.show();
 delay (30);



 // FOURTH ARROW RED

byte fourthArrow8[] = {223,225,237,243,244,234,230,216};

for (int l = 0; l<8; l++) {
 leds[fourthArrow8[l]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// FOURTH ARROW BLACK

for (int l = 0; l<8; l++) {
 leds[fourthArrow8[l]] = CRGB::Black;
}
FastLED.show();
 delay (30);



 // FIFTH ARROW RED

byte fifthArrow8[] = {224,238,242,252,251,245,233,231};

for (int m = 0; m<8; m++) {
 leds[fifthArrow8[m]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// FIFTH ARROW BLACK

for (int m = 0; m<8; m++) {
 leds[fifthArrow8[m]] = CRGB::Black;
}
FastLED.show();
 delay (30);


  }

// RIGHT TURN SIGNAL

if (buttonState2 == HIGH) {

FastLED.setBrightness(BRIGHTNESS);

// Set the first led back to black for 1 second
        for (int i = 0; i <= 63; i++)
            leds[i] = CRGB::Black;

FastLED.show();
        delay(30);


// SIXTH ARROW RED

byte sixthArrow20[] =
{56,57,58,59,60,61,62,63,49,50,51,52,53,54,42,43,44,45,35,36};

for (int n = 0; n<20; n++) {
 leds[sixthArrow20[n]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// SIXTH ARROW BLACK

for (int n = 0; n<20; n++) {
 leds[sixthArrow20[n]] = CRGB::Black;
}
FastLED.show();
 delay (30);



// SEVENTH ARROW RED

byte seventhArrow8[] = {48,55,46,41,37,34,28,27};

for (int o = 0; o<8; o++) {
 leds[seventhArrow8[o]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// SEVENTH ARROW BLACK

for (int o = 0; o<8; o++) {
 leds[seventhArrow8[o]] = CRGB::Black;
}
FastLED.show();
 delay (30);


// EIGTH ARROW RED

byte eigthArrow8[] = {47,40,33,38,26,29,19,20};

for (int p = 0; p<8; p++) {
 leds[eigthArrow8[p]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// EIGHT ARROW BLACK

for (int p = 0; p<8; p++) {
 leds[eigthArrow8[p]] = CRGB::Black;
}
FastLED.show();
 delay (30);



 // NINTH ARROW RED

byte ninthArrow8[] = {39,32,25,30,21,18,12,11};

for (int q = 0; q<8; q++) {
 leds[ninthArrow8[q]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// NINTH ARROW BLACK

for (int q = 0; q<8; q++) {
 leds[ninthArrow8[q]] = CRGB::Black;
}
FastLED.show();
 delay (30);



 // TENTH ARROW RED

byte tenthArrow8[] = {31,24,22,17,13,10,4,3};

for (int r = 0; r<8; r++) {
 leds[tenthArrow8[r]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// TENTH ARROW BLACK

for (int r = 0; r<8; r++) {
 leds[tenthArrow8[r]] = CRGB::Black;
}

 delay (30);

  }





//BRAKE LIGHT
       FastLED.setBrightness(BRIGHTNESS);
      if(buttonState3 == HIGH){


         // Set the right tail light black
        for (int i = 0; i <= 63; i++)
              leds[i] = CRGB::Black;

// left tail light black
        for (int j = 192; j <= 255; j++)
            leds[j] = CRGB::Black;


//CENTER 4
byte center4[] = {36,35,28,27,227,228,219,220};

for (int b = 0; b<8; b++) {
 leds[center4[b]] = CRGB::Blue;
}
FastLED.show();
delay(200);

// FIRST RING
byte firstRing12[] =
{42,43,44,45,37,29,21,20,19,18,26,34,234,235,236,237,226,221,210,211,212,213,218,229};

for (int c = 0; c<24; c++) {
 leds[firstRing12[c]] = CRGB::Red;
}
FastLED.show();
delay(200);


// SECOND RING
byte secondRing20[] =
{49,50,51,52,53,46,38,30,22,14,13,12,11,10,9,17,25,33,41,54,241,242,243,244,245,246,233,230,217,214,201,202,203,204,205,206,209,222,225,238};

for (int d = 0; d<40; d++) {
 leds[secondRing20[d]] = CRGB::Red;
}
 delay (200);


// THIRD RING
byte thirdRing28[] =
{63,62,61,60,59,58,57,56,55,47,39,31,23,15,7,6,5,4,3,2,1,0,8,16,24,32,40,48,255,254,253,252,251,250,249,248,247,232,231,216,
215,200,199,198,197,196,195,194,193,192,207,208,223,224,239,240};

for (int e = 0; e<56; e++) {
 leds[thirdRing28[e]] = CRGB::Red;
}
              delay(200);



              
oldButtonState3 = HIGH; // so this block can only run once

}





// "stop braking" code here
// turn LED off:
if(buttonState3 == LOW && oldButtonState3 == HIGH) {


    FastLED.setBrightness(DIM);

// Set the right tail light to black for 1 second
      for (int i = 0; i <= 63; i++)
              leds[i] = CRGB::Red;

// Set the left tail light to black for 1 second
      for (int j = 192; j <= 255; j++)
             leds[j] = CRGB::Red;
              FastLED.show();

          oldButtonState3 == LOW;
          FastLED.setBrightness(BRIGHTNESS);
     

  }
if (buttonState4 == HIGH) {

  //// Set the right tail light to black for 1 second
      for (int i = 0; i <= 63; i++)
              leds[i] = CRGB::Red;

// Set the left tail light to black for 1 second
      for (int j = 192; j <= 255; j++)
             leds[j] = CRGB::Blue;
              FastLED.show();
delay (100);
// Set the right tail light to black for 1 second
      for (int i = 0; i <= 63; i++)
              leds[i] = CRGB::Blue;

// Set the left tail light to black for 1 second
      for (int j = 192; j <= 255; j++)
             leds[j] = CRGB::Red;
              FastLED.show();
           delay (100) ;  
}

}             

Hello @Nicholas_Weir . As you probably noticed, G+ doesn’t display code properly. Please post your code to http://gist.github.com so it’s more readable and also line numbers can be referred to.

Make functions to light each lamp. Don’t do it all in the loop.

Make a function to clear the lights.

Check the button state in loop and call the wanted function.

Light left{}

Light Right{}

Clear Left{}

Clear Right{}

That’s the way I would handle it.

Is this related to putting things on different “tabs” of the IDE software?

Or do I put them at the bottom?

Would I need to say

void Light Left() ?

I don’t think it really matters they will only get called explicitly .

Okay I figured out how to write the code and it compiled okay. I will hook it up to the arduino after work and see if it runs the code once and stays lit until I release.

It is still looping… Can you look at this portion of the code and tell me why??

You might need a return statement somewhere

if(lightOn ==TRUE)
{
return;
}

@Nicholas_Weir Here’s an example that will hopefully give you some ideas toward sorting things out.

Wow that is a clean code - I am going to try a couple things on mine right now and if they don’t work I will be going to that style. I will post my results soon!

thanks again - very awesome that you added the video!

Okay I am trying to work with your code now - can I set my buttons up like I had them to read HIGH/LOW? Or do I need to set them up like the image you included? I don’t have those resistors thats why I ask.

You don’t have to use the resistors, but they can make the debouncing more reliable in some ways. If you don’t use your own resistors make sure you have setup the digital input pin to use the internal pullup resistor so you don’t damage the pin. Try it and test the results. Only you can determine if you’re happy with how your setup is working. And if this is for a brake display on a motorcycle you better be happy with it. :wink:

10K ohm is a commonly used resistor value you should add some to your collection. As for the lower value resistor, it doesn’t have to be that exact value. Use 120-300 ohm and it should work just fine.

https://www.arduino.cc/en/Tutorial/DigitalPins

I have some 1k ohm resistors. I will see if I can wrap my head around using the internal one.

This code is working like this - I cannot figure out how to change to use the resistors on an uno - it keeps saying the code won’t work for an uno.

I am going to post my code - then i will show a video of both how they work.

Here is the video - your code is working backwards and thats probably because my button is wired wrong.

My code works except the looping of the brakes and i don’t like that the non signaling light gets bright.

If the button response is backwards you should be able to change this line:
Button brakeButton(brakeButtonPin, true, true, 50);
to
Button brakeButton(brakeButtonPin, true, false, 50);

Switching that true/false variable inverts the button response.