Here is the project I am building.
I have an off-road jeep that only see’s trail use. I am building a taillight setup for the Jeep using WS2812B LED strips. I am in the testing phase and I have the Running Lights working when the device powers up. It stays with the red running lights until it detects that the brake pedal has been pressed.
Once it detects the pedal has been pressed, then it will increase the LED brightness in a sequence style. I want the sequence to only happen once when it detects pin 7 changed from LOW to HIGH.
The problem I am having is that when I press the button, it reacts very quickly to activate the brake lights, however when I let off the button, at times, the brightness stays increased for up to 5+ seconds before they dim back to the running lights.
I have tried this code using a Mega, and Nano, and both react the same way. I don’t see anything in the code that jumps out at me, but I am still learning the programming side arduino.
Attached is a picture of how I have the hardware setup.
#include <FastLED.h>
#define NUM_LEDS 54
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
int pedalState = LOW;
const int pedalPin = 7;
unsigned long previousMillis = 0; // will store last time the pedlal position was checked
const long interval = 500; // interval at which to check the pedal (milliseconds)
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
pinMode(pedalPin, INPUT);
// Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you checked the Brakes
previousMillis = currentMillis;
pedalState = digitalRead(pedalPin);
if (pedalState == HIGH) {
// for(int dot = 0; dot < NUM_LEDS; dot++)
for (int dot = NUM_LEDS - 1; dot >= 0; dot--) {
leds[dot] = CRGB::White;
FastLED.setBrightness(250);
FastLED.show();
// clear this led for the next time around the loop
//leds[dot] = CRGB::Black;
FastLED.delay(20);
}
}
else
for (int dot = NUM_LEDS - 1; dot >= 0; dot--) {
leds[dot] = CRGB::Red;
FastLED.setBrightness(120);
FastLED.show();
// clear this led for the next time around the loop
//leds[dot] = CRGB::Black;
//FastLED.delay(10);
}
//leds[dot] = CRGB::Black;
//Serial.print("pedalState=");
//Serial.println(pedalState);
}
}