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();
}