Added brake light animation on pedal release.

This commit is contained in:
ThePendulum
2026-05-01 05:39:36 +02:00
parent d6f32a03fd
commit abea37e15e

View File

@@ -92,6 +92,7 @@ const uint32_t COLOR_OFF = ledsTail.Color(0, 0, 0, 0);
const uint32_t COLOR_WHITE = ledsTail.Color(0, 0, 0, 255);
const uint32_t COLOR_WHITE_FULL = ledsTail.Color(255, 255, 255, 255);
const uint32_t COLOR_WHITE_DIM = ledsTail.Color(0, 0, 0, 100);
const uint32_t COLOR_RED_DIM = ledsTail.Color(150, 0, 0, 0);
const uint32_t COLOR_RED = ledsTail.Color(255, 0, 0, 0);
const uint32_t COLOR_AMBER = colorHSV(10); // easier value fade
const uint32_t COLOR_POLICE_BLUE = ledsTail.Color(0, 0, 255, 0);
@@ -101,6 +102,7 @@ unsigned long animationFrame = 0;
unsigned long lastFrame = 0;
unsigned long lastPedalPress = millis();
unsigned long lastPedalRelease = 0;
bool isReversing = false;
unsigned long indicatorLeftStart = 0;
@@ -161,17 +163,24 @@ void animateHeadlights() {
}
void animateTailLight() {
ledsTail.fill(COLOR_RED);
ledsTail.fill(COLOR_OFF, 0, LEDS_TAIL_LENGTH);
ledsTail.fill(COLOR_RED, LEDS_TAIL_SIDE_LENGTH, LEDS_TAIL_LENGTH - LEDS_TAIL_SIDE_LENGTH * 2);
}
void animateRightTailSide(uint32_t color) {
void animateRightTailSide(uint32_t color, bool withMargin = true) {
ledsTail.fill(color, 0, LEDS_TAIL_SIDE_LENGTH);
ledsTail.fill(COLOR_OFF, LEDS_TAIL_SIDE_LENGTH, LEDS_TAIL_SIDE_MARGIN);
if (withMargin) {
ledsTail.fill(COLOR_OFF, LEDS_TAIL_SIDE_LENGTH, LEDS_TAIL_SIDE_MARGIN);
}
}
void animateLeftTailSide(uint32_t color) {
void animateLeftTailSide(uint32_t color, bool withMargin = true) {
ledsTail.fill(color, LEDS_TAIL_LENGTH - LEDS_TAIL_SIDE_LENGTH, LEDS_TAIL_SIDE_LENGTH);
ledsTail.fill(COLOR_OFF, LEDS_TAIL_LENGTH - LEDS_TAIL_SIDE_LENGTH - LEDS_TAIL_SIDE_MARGIN, LEDS_TAIL_SIDE_MARGIN);
if (withMargin) {
ledsTail.fill(COLOR_OFF, LEDS_TAIL_LENGTH - LEDS_TAIL_SIDE_LENGTH - LEDS_TAIL_SIDE_MARGIN, LEDS_TAIL_SIDE_MARGIN);
}
}
void animateDash() {
@@ -182,6 +191,11 @@ void animateReverse() {
ledsTail.fill(COLOR_WHITE);
}
void animateBrake() {
animateLeftTailSide(COLOR_RED, false);
animateRightTailSide(COLOR_RED, false);
}
constexpr int INDICATOR_DURATION = 3200; // total ms
constexpr int INDICATOR_HOLD_FRAMES = 15;
constexpr int INDICATOR_OFF_FRAMES = 20;
@@ -224,7 +238,7 @@ void animateIndicator(Direction direction) {
? LEDS_TAIL_LEFT_RANGE
: LEDS_TAIL_RIGHT_RANGE;
void (*animateTailSide)(uint32_t) = direction == LEFT
void (*animateTailSide)(uint32_t, bool) = direction == LEFT
? animateLeftTailSide
: animateRightTailSide;
@@ -235,13 +249,13 @@ void animateIndicator(Direction direction) {
if (indicatorFrame < sweepEnd) {
const int colorAmberFaded = colorHSV(10, 1, (float)indicatorFrame / sweepEnd);
animateTailSide(colorAmberFaded);
animateTailSide(colorAmberFaded, true);
ledsDash.setPixelColor(dashIndex, colorAmberFaded);
} else if (indicatorFrame < holdEnd) {
animateTailSide(COLOR_AMBER);
animateTailSide(COLOR_AMBER, true);
ledsDash.setPixelColor(dashIndex, COLOR_AMBER);
} else {
animateTailSide(COLOR_OFF);
animateTailSide(COLOR_OFF, true);
ledsDash.setPixelColor(dashIndex, COLOR_OFF);
}
}
@@ -355,6 +369,10 @@ void animate(long now) {
animateIndicator(RIGHT);
}
if (now - lastPedalRelease < 1500) {
animateBrake();
}
if (activeMode == MODE_RAINBOW) {
animateRainbow();
}
@@ -475,6 +493,10 @@ void loop() {
Serial.println("PEDAL FORWARD");
}
if (pedal.released()) {
lastPedalRelease = now;
}
if (!isOff && now - lastFrame >= ANIMATION_INTERVAL) {
lastFrame += ANIMATION_INTERVAL;
animationFrame += 1;