Help with triggers & Neopixels

Hey guys, please go easy i am very very much a novice to arduino.

A freidn has been helping me a lot with my code and hes been a huge help, but we're both at a point where neither of us know how to fix this issue.

This controller will control tail lights on a car, we have button presses as triggers(5v input on the board) and all the inputs work , indicator displays, the brake lights display etc...the issue is as soon as the 5v is removed (the button is no longer presssed) the lights do random things.

I know why this is, its becuse all the buttons "off" state tell the controller to do different things, and I need a way to tell the controller to just "go back to what it was before" when the button is not pressed.

I really hope I am making sense here.

Thanks

(EDIT Due to code length I took out some of buttons due to posting limits but they are all the same just repeated)

#include <WS2812FX.h>
#include <Arduino.h>

#define LED_COUNT_STRIP_1 54             //Number of LEDs in the strip, strip 1
#define LED_COUNT_STRIP_2 54             //Number of LEDs in the strip, strip 2
#define LED_PIN_STRIP_1 8                  //Which pin are the LEDs connected to, strip 1
#define LED_PIN_STRIP_2 9                  //Which pin are the LEDs connected to, strip 2
#define BUTTON_PIN  PD1           //Button for DRL
#define BUTTON_PIN1 PD2            //Button for  Running/side lights  
#define BUTTON_PIN2 PD3           //Button for  Brake lights
#define BUTTON_PIN3 PD4           //Button for  Fog light            
#define BUTTON_PIN4 PD5            //Button for Reverse light
#define BUTTON_PIN5 PD6          //Button for Left indicator
#define BUTTON_PIN6 PD7          //Button for Right indicator




bool button_pressed_0 = false;     //  Required for different input or buttons and must be added below and must be added after "if(digitalRead(BUTTON_PIN) == 1 &&"
bool button_pressed_1 = false;     //  Required for different input or buttons and must be added below and must be added after "if(digitalRead(BUTTON_PIN) == 1 &&"

WS2812FX ws2812fx_strip1 = WS2812FX(LED_COUNT_STRIP_1, LED_PIN_STRIP_1, NEO_GRB + NEO_KHZ800);
WS2812FX ws2812fx_strip2 = WS2812FX(LED_COUNT_STRIP_2, LED_PIN_STRIP_2, NEO_GRB + NEO_KHZ800);

void setup() {
pinMode(BUTTON_PIN,  INPUT);       //Defines the button type can be INPUT for 5v or INPUT_PULLUP for connecting to ground
pinMode(BUTTON_PIN1, INPUT);       //Defines the button type can be INPUT for 5v or INPUT_PULLUP for connecting to ground
pinMode(BUTTON_PIN2, INPUT);       //Defines the button type can be INPUT for 5v or INPUT_PULLUP for connecting to ground
pinMode(BUTTON_PIN3, INPUT);       //Defines the button type can be INPUT for 5v or INPUT_PULLUP for connecting to ground
pinMode(BUTTON_PIN4, INPUT);       //Defines the button type can be INPUT for 5v or INPUT_PULLUP for connecting to ground
pinMode(BUTTON_PIN5, INPUT);       //Defines the button type can be INPUT for 5v or INPUT_PULLUP for connecting to ground
pinMode(BUTTON_PIN6, INPUT);       //Defines the button type can be INPUT for 5v or INPUT_PULLUP for connecting to ground

      //This will run when the arduino is powered on once 
  ws2812fx_strip1.init();
  ws2812fx_strip2.init();
  
  ws2812fx_strip1.setBrightness(100);
  ws2812fx_strip2.setBrightness(100);

  ws2812fx_strip1.setColor(BLACK);
  ws2812fx_strip2.setColor(BLACK);

  ws2812fx_strip1.setSpeed(1000);
  ws2812fx_strip2.setSpeed(1000);

  ws2812fx_strip1.setMode(FX_MODE_STATIC);
  ws2812fx_strip2.setMode(FX_MODE_STATIC);

  ws2812fx_strip1.start();
  ws2812fx_strip2.start();
}


void processButton() {        //Button processing down below, insert code in this area for button presses

//---------------------------------------------------------------------------------------------------

//----------This is when the button 0 DRL IS pressed -----------
  
if(digitalRead(BUTTON_PIN) == 1 && button_pressed_0 == false) {

        // parameters: index, start, stop, mode, color, speed, reverse
    ws2812fx_strip1.setBrightness (100);
    ws2812fx_strip1.setSegment(0, 0, 54, FX_MODE_STATIC, WHITE, 1000, false);
    ws2812fx_strip1.resetSegmentRuntimes();      // This will make sure the segment always starts at the beginning

    button_pressed_0 = true;

//----------This is when the button 0 DRL IS NOT pressed -----------

} else if(digitalRead(BUTTON_PIN) == 0 && button_pressed_0 == true) {

        // parameters: index, start, stop, mode, color, speed, reverse
    ws2812fx_strip1.setBrightness (50);    
    ws2812fx_strip1.setSegment(0, 0, 54, FX_MODE_STATIC, WHITE, 1000, false);
    ws2812fx_strip1.resetSegmentRuntimes();       // This will make sure the segment always starts at the beginning



    button_pressed_0 = false;

}

//---------------------------------------------------------------------------------------------------

//----------This is when the button 1 RUNNING LIGHT IS pressed -----------


if(digitalRead(BUTTON_PIN1) == 1 && button_pressed_1 == false) { 

        // parameters: index, start, stop, mode, color, speed, reverse
    ws2812fx_strip2.setSegment(0, 0, 54, FX_MODE_CHASE_WHITE, RED, 1000, true);
    ws2812fx_strip2.resetSegmentRuntimes();      // This will make sure the segment always starts at the beginning





    button_pressed_1 = true;


//----------This is when the button 1 RUNNING LIGHT IS NOT pressed -----------

} else if(digitalRead(BUTTON_PIN1) == 0 && button_pressed_1 == true) { 

            // parameters: index, start, stop, mode, color, speed, reverse
    ws2812fx_strip2.setSegment(0, 0, 54, FX_MODE_STATIC, WHITE, 1000, true);
    ws2812fx_strip2.resetSegmentRuntimes();       // This will make sure the segment always starts at the beginning


    button_pressed_1 = false;

}


//----------This is when the button 2 BRAKE LIGHT IS pressed -----------
  
if(digitalRead(BUTTON_PIN2) == 1 && button_pressed_0 == false) {

        // parameters: index, start, stop, mode, color, speed, reverse
    ws2812fx_strip1.setBrightness (100);
    ws2812fx_strip1.setSegment(0, 0, 54, FX_MODE_STATIC, RED, 1000, false);
    ws2812fx_strip1.resetSegmentRuntimes();      // This will make sure the segment always starts at the beginning

    button_pressed_0 = true;

//----------This is when the button 2 BRAKE LIGHT IS NOT pressed -----------

} else if(digitalRead(BUTTON_PIN2) == 0 && button_pressed_0 == true) {

        // parameters: index, start, stop, mode, color, speed, reverse
    ws2812fx_strip1.setBrightness (50);    
    ws2812fx_strip1.setSegment(0, 0, 54, FX_MODE_STATIC, RED, 1000, false);
    ws2812fx_strip1.resetSegmentRuntimes();       // This will make sure the segment always starts at the beginning



    button_pressed_0 = false;

}




}
}

//---------------------------------------------------------------------------------------------------
//Button Number *please add*


  }
void loop() {
    processButton();
    ws2812fx_strip1.service();
    ws2812fx_strip2.service();
}

as soon as the 5v is removed (the button is no longer presssed) the lights do random things.
I know why this is...

Are you sure? Could it be that when buttons are not pressed, the input pins are floating? That can give random results. If you don't think they are floating, please explain why.

PaulRB:
Are you sure? Could it be that when buttons are not pressed, the input pins are floating? That can give random results. If you don't think they are floating, please explain why.

think I may have worded it badly I'm not sure you understand my question but to answer yours I use resistors to pull down that's not an issue.

So I'll try to explain better

In my code I have it when button 0 is pressed go white and when it's released go white

But when button 2 is pressed it goes red then when released goes red lower brightness.

This isn't what I need to happen

So let's say currently the strip is RED from the running light input (the entire strip)

Then the first 20 of those led change to orange for the indicator, I have indicator to return to RED on button release.

However say I have the running lights off so the strip isn't lit up at all, when I indicator the same 20 leds will light orange however when I stop indicating that 20led part of the strip will go RED instead of switching off again.

When I couple that with all the buttons, DRL, running light, brake light, reverse light etc it becomes an issue as the controller does not the correct "button off" state

I hope I make sense this time

I use resistors to pull down that's not an issue

Yes, that was my concern. You did not include a schematic showing how the buttons are wired, and your code does not use INPUT_PULLUP, plus you said

we have button presses as triggers(5v input on the board) and all the inputs work...the issue is as soon as the 5v is removed (the button is no longer presssed) the lights do random things

which, as a long-time contributor to the forum, immediately sounded alarms in my mind that you might not be aware of the problem of floating inputs, as is the case with many beginners. Often beginners will blame their code for what is in fact a problem of incorrectly wired buttons.

What I would suggest to correct your problems is to spend a little time designing a "finite state machine". Its a slightly scary sounding name for something that is quite simple to understand. Start with a large sheet of blank paper and draw a small circle in the centre and write in it the name of the most basic 'state' of your circuit, such as "normal" or "idle". Then draw other circles representing all the other states, such as "turning", "braking", "turning and braking" and so on. Then draw arrows between the states that represent 'events' that would cause that change of state. Events would be things like the press or release of a particular button, or a specific change in the reading from a sensor, or even a timeout, having been in a state for a specific length of time.

When you have your "state change diagram" done, you can implement that as code using a variable to represent the current state. A switch/case statement can be used to control which lines of code get executed in each state. The lines of code for each state can detect the relavent events and update the state accordingly. Also have a variable representing the previous state, so that when the state has just changed, the code for the new state can detect this and set outputs such as LEDs as needed.