Compare commits
6 Commits
899ffbdd51
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9a6fecae7 | ||
|
|
dd1a90f437 | ||
|
|
65ef0734f6 | ||
|
|
abea37e15e | ||
|
|
d6f32a03fd | ||
|
|
7e10f2a797 |
254
QuadLightsV3.ino
254
QuadLightsV3.ino
@@ -1,13 +1,16 @@
|
||||
#include <Button.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
|
||||
constexpr bool KEEPALIVE_ENABLE = false;
|
||||
constexpr int KEEPALIVE_TIMEOUT = 5000; // ms
|
||||
constexpr bool KEEPALIVE_ENABLE = true;
|
||||
constexpr int KEEPALIVE_TIMEOUT = 30000; // ms
|
||||
|
||||
constexpr int LEDS_TAIL_PIN = PA6;
|
||||
constexpr int LEDS_TAIL_LENGTH = 10;
|
||||
constexpr int LEDS_TAIL_BRIGHTNESS = 255;
|
||||
constexpr int LEDS_TAIL_SIDE_LENGTH = 3;
|
||||
constexpr int LEDS_TAIL_SIDE_LENGTH = 2;
|
||||
constexpr int LEDS_TAIL_SIDE_MARGIN = 2;
|
||||
constexpr int LEDS_TAIL_RIGHT_RANGE[] = {0, LEDS_TAIL_SIDE_LENGTH};
|
||||
constexpr int LEDS_TAIL_LEFT_RANGE[] = {LEDS_TAIL_LENGTH - LEDS_TAIL_SIDE_LENGTH, LEDS_TAIL_SIDE_LENGTH};
|
||||
|
||||
constexpr int LEDS_LEFT_PIN = PA5;
|
||||
constexpr int LEDS_RIGHT_PIN = PA7;
|
||||
@@ -64,32 +67,48 @@ Adafruit_NeoPixel ledsHeadLeft(LEDS_HEAD_LENGTH, LEDS_LEFT_PIN, NEO_GRBW + NEO_K
|
||||
Adafruit_NeoPixel ledsHeadRight(LEDS_HEAD_LENGTH, LEDS_RIGHT_PIN, NEO_GRBW + NEO_KHZ800);
|
||||
Adafruit_NeoPixel ledsDash(LEDS_DASH_LENGTH, LEDS_DASH_PIN, NEO_GRBW + NEO_KHZ800);
|
||||
|
||||
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 = ledsTail.Color(255, 0, 0, 0);
|
||||
const uint32_t COLOR_AMBER = ledsTail.Color(255, 50, 0, 0);
|
||||
const uint32_t COLOR_POLICE_BLUE = ledsTail.Color(0, 0, 255, 0);
|
||||
const uint32_t COLOR_POLICE_RED = ledsTail.Color(255, 0, 0, 0);
|
||||
|
||||
enum Direction { LEFT, RIGHT };
|
||||
|
||||
enum Mode {
|
||||
MODE_HEAD,
|
||||
MODE_POLICE,
|
||||
MODE_HAZARD,
|
||||
MODE_POLICE,
|
||||
MODE_RAINBOW,
|
||||
MODE_KITT,
|
||||
MODE_COUNT,
|
||||
};
|
||||
|
||||
// Mode activeMode = MODE_HEAD;
|
||||
Mode activeMode = MODE_HAZARD;
|
||||
Mode activeMode = MODE_HEAD;
|
||||
// Mode activeMode = MODE_KITT;
|
||||
|
||||
uint32_t colorHSV(float hue, float sat = 1.0, float val = 1.0) {
|
||||
return Adafruit_NeoPixel::ColorHSV(
|
||||
(uint16_t)(hue / 360.0 * 65535),
|
||||
(uint8_t)(sat * 255),
|
||||
(uint8_t)(val * 255)
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
const uint32_t COLOR_POLICE_RED = ledsTail.Color(255, 0, 0, 0);
|
||||
const uint32_t COLOR_GREEN = ledsTail.Color(0, 255, 0, 0);
|
||||
|
||||
unsigned long animationFrame = 0;
|
||||
unsigned long stableAnimationFrame = 0;
|
||||
unsigned long lastFrame = 0;
|
||||
|
||||
unsigned long lastPedalPress = millis();
|
||||
unsigned long lastPedalRelease = 0;
|
||||
|
||||
unsigned long lastButtonPress = 0;
|
||||
|
||||
bool isReversing = false;
|
||||
|
||||
unsigned long indicatorLeftStart = 0;
|
||||
@@ -150,23 +169,37 @@ 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, bool withMargin = true) {
|
||||
ledsTail.fill(color, 0, LEDS_TAIL_SIDE_LENGTH);
|
||||
|
||||
if (withMargin) {
|
||||
ledsTail.fill(COLOR_OFF, LEDS_TAIL_SIDE_LENGTH, LEDS_TAIL_SIDE_MARGIN);
|
||||
}
|
||||
}
|
||||
|
||||
void animateLeftTailSide(uint32_t color, bool withMargin = true) {
|
||||
ledsTail.fill(color, LEDS_TAIL_LENGTH - LEDS_TAIL_SIDE_LENGTH, LEDS_TAIL_SIDE_LENGTH);
|
||||
|
||||
if (withMargin) {
|
||||
ledsTail.fill(COLOR_OFF, LEDS_TAIL_LENGTH - LEDS_TAIL_SIDE_LENGTH - LEDS_TAIL_SIDE_MARGIN, LEDS_TAIL_SIDE_MARGIN);
|
||||
}
|
||||
}
|
||||
|
||||
void animateDash() {
|
||||
ledsDash.clear();
|
||||
ledsDash.fill(COLOR_GREEN);
|
||||
}
|
||||
|
||||
void animateReverse() {
|
||||
ledsTail.fill(COLOR_WHITE);
|
||||
}
|
||||
|
||||
uint32_t colorHSV(float hue, float sat = 1.0, float val = 1.0) {
|
||||
return Adafruit_NeoPixel::ColorHSV(
|
||||
(uint16_t)(hue / 360.0 * 65535),
|
||||
(uint8_t)(sat * 255),
|
||||
(uint8_t)(val * 255)
|
||||
);
|
||||
void animateBrake() {
|
||||
animateLeftTailSide(COLOR_RED, false);
|
||||
animateRightTailSide(COLOR_RED, false);
|
||||
}
|
||||
|
||||
constexpr int INDICATOR_DURATION = 3200; // total ms
|
||||
@@ -175,14 +208,10 @@ constexpr int INDICATOR_OFF_FRAMES = 20;
|
||||
constexpr float INDICATOR_SPEED = 2;
|
||||
|
||||
void animateIndicator(Direction direction) {
|
||||
Adafruit_NeoPixel& ledsTarget = direction == LEFT // reference, not pointer
|
||||
Adafruit_NeoPixel& ledsTarget = direction == LEFT
|
||||
? ledsHeadLeft
|
||||
: ledsHeadRight;
|
||||
|
||||
int dashTargetIndex = direction == LEFT
|
||||
? 1
|
||||
: 0;
|
||||
|
||||
const long indicatorFrame = lround(animationFrame * INDICATOR_SPEED) % (LEDS_EDGE_LENGTH + INDICATOR_HOLD_FRAMES + INDICATOR_OFF_FRAMES);
|
||||
|
||||
const long sweepEnd = LEDS_TOP_LENGTH;
|
||||
@@ -210,6 +239,27 @@ void animateIndicator(Direction direction) {
|
||||
ledsTarget.fill(COLOR_OFF, 0, LEDS_EDGE_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
void (*animateTailSide)(uint32_t, bool) = direction == LEFT
|
||||
? animateLeftTailSide
|
||||
: animateRightTailSide;
|
||||
|
||||
const int dashIndex = direction == LEFT
|
||||
? 1
|
||||
: 0;
|
||||
|
||||
if (indicatorFrame < sweepEnd) {
|
||||
const int colorAmberFaded = colorHSV(10, 1, (float)indicatorFrame / sweepEnd);
|
||||
|
||||
animateTailSide(colorAmberFaded, true);
|
||||
ledsDash.setPixelColor(dashIndex, colorAmberFaded);
|
||||
} else if (indicatorFrame < holdEnd) {
|
||||
animateTailSide(COLOR_AMBER, true);
|
||||
ledsDash.setPixelColor(dashIndex, COLOR_AMBER);
|
||||
} else {
|
||||
animateTailSide(COLOR_OFF, true);
|
||||
ledsDash.setPixelColor(dashIndex, COLOR_OFF);
|
||||
}
|
||||
}
|
||||
|
||||
constexpr float POLICE_SPEED = 0.75;
|
||||
@@ -238,18 +288,28 @@ void animatePolice() {
|
||||
ledsHeadLeft.fill(COLOR_POLICE_BLUE, 0, LEDS_EDGE_LENGTH);
|
||||
ledsHeadRight.fill(COLOR_OFF, 0, LEDS_EDGE_LENGTH);
|
||||
|
||||
// deliberately inverted from edge lights for a more alternating effect
|
||||
animateLeftTailSide(COLOR_OFF);
|
||||
animateRightTailSide(COLOR_POLICE_BLUE);
|
||||
|
||||
ledsDash.setPixelColor(1, COLOR_POLICE_BLUE);
|
||||
ledsDash.setPixelColor(0, COLOR_OFF);
|
||||
} else if (policeBeaconFrames[policeFrame] == POLICE_RIGHT) {
|
||||
ledsHeadLeft.fill(COLOR_OFF, 0, LEDS_EDGE_LENGTH);
|
||||
ledsHeadRight.fill(COLOR_POLICE_BLUE, 0, LEDS_EDGE_LENGTH);
|
||||
|
||||
animateLeftTailSide(COLOR_POLICE_BLUE);
|
||||
animateRightTailSide(COLOR_OFF);
|
||||
|
||||
ledsDash.setPixelColor(1, COLOR_OFF);
|
||||
ledsDash.setPixelColor(0, COLOR_POLICE_BLUE);
|
||||
} else {
|
||||
ledsHeadLeft.fill(COLOR_OFF, 0, LEDS_EDGE_LENGTH);
|
||||
ledsHeadRight.fill(COLOR_OFF, 0, LEDS_EDGE_LENGTH);
|
||||
|
||||
animateLeftTailSide(COLOR_OFF);
|
||||
animateRightTailSide(COLOR_OFF);
|
||||
|
||||
ledsDash.setPixelColor(1, COLOR_OFF);
|
||||
ledsDash.setPixelColor(0, COLOR_OFF);
|
||||
}
|
||||
@@ -264,32 +324,126 @@ void animatePolice() {
|
||||
}
|
||||
|
||||
constexpr float RAINBOW_SPEED = 2.0;
|
||||
constexpr float RAINBOW_DENSITY = 5.0;
|
||||
constexpr float RAINBOW_DENSITY = 10.0;
|
||||
|
||||
void animateRainbow() {
|
||||
for (int ledIndex = 0; ledIndex < LEDS_HEAD_LENGTH; ledIndex++) {
|
||||
const uint32_t pixelColor = colorHSV((animationFrame * RAINBOW_SPEED) + (ledIndex * RAINBOW_DENSITY));
|
||||
// EDGES, flowing color
|
||||
for (int ledIndex = LEDS_TOP_LENGTH - 1; ledIndex >= 0; ledIndex--) {
|
||||
const uint32_t pixelColor = colorHSV((stableAnimationFrame * RAINBOW_SPEED) + (ledIndex * RAINBOW_DENSITY));
|
||||
|
||||
ledsHeadLeft.setPixelColor(ledIndex, pixelColor);
|
||||
ledsHeadLeft.setPixelColor(LEDS_EDGE_LENGTH - ledIndex, pixelColor);
|
||||
|
||||
ledsHeadRight.setPixelColor(ledIndex, pixelColor);
|
||||
ledsHeadRight.setPixelColor(LEDS_EDGE_LENGTH - ledIndex, pixelColor);
|
||||
}
|
||||
|
||||
/*
|
||||
// RINGS, single color
|
||||
ledsHeadLeft.fill(colorHSV(stableAnimationFrame * RAINBOW_SPEED), LEDS_EDGE_LENGTH, LEDS_RING_LENGTH);
|
||||
ledsHeadRight.fill(colorHSV(stableAnimationFrame * RAINBOW_SPEED), LEDS_EDGE_LENGTH, LEDS_RING_LENGTH);
|
||||
|
||||
for (int ledIndex = 0; ledIndex < LEDS_TAIL_LENGTH; ledIndex++) {
|
||||
const uint32_t pixelColor = ledsHeadLeft.ColorHSV((animationFrame * 1000 * RAINBOW_SPEED) + (ledIndex * 1000 * RAINBOW_DENSITY * 5), 255, 255);
|
||||
const int distFromCenter = abs(ledIndex * 2 - (LEDS_TAIL_LENGTH - 1));
|
||||
const uint32_t pixelColor = colorHSV((stableAnimationFrame * RAINBOW_SPEED) - (distFromCenter * RAINBOW_DENSITY * 2));
|
||||
|
||||
if (ledIndex <= 5) {
|
||||
ledsTail.setPixelColor(ledIndex, pixelColor);
|
||||
}
|
||||
|
||||
ledsDash.setPixelColor(1, colorHSV(stableAnimationFrame * RAINBOW_SPEED));
|
||||
ledsDash.setPixelColor(0, colorHSV(stableAnimationFrame * RAINBOW_SPEED + 90));
|
||||
}
|
||||
|
||||
constexpr float KITT_SPEED = 0.5;
|
||||
constexpr int KITT_TRAIL = 4;
|
||||
constexpr float KITT_TAIL_SPEED = 3.0f;
|
||||
|
||||
int kittScanPos(int vPos) {
|
||||
if (vPos < LEDS_RING_LENGTH) {
|
||||
// first ring — reversed so it flows naturally into the edge
|
||||
return LEDS_EDGE_LENGTH + (LEDS_RING_LENGTH - 1 - vPos);
|
||||
} else if (vPos < LEDS_RING_LENGTH + LEDS_EDGE_LENGTH) {
|
||||
// edge forward
|
||||
return vPos - LEDS_RING_LENGTH;
|
||||
} else if (vPos < 2 * LEDS_RING_LENGTH + LEDS_EDGE_LENGTH) {
|
||||
// second ring
|
||||
return LEDS_EDGE_LENGTH + (vPos - LEDS_RING_LENGTH - LEDS_EDGE_LENGTH);
|
||||
} else {
|
||||
ledsTail.setPixelColor(LEDS_TAIL_LENGTH - ledIndex, pixelColor);
|
||||
// edge backward
|
||||
return LEDS_EDGE_LENGTH - 1 - (vPos - 2 * LEDS_RING_LENGTH - LEDS_EDGE_LENGTH);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
ledsTail.fill(ledsTail.ColorHSV(animationFrame * 1000 * RAINBOW_SPEED));
|
||||
void animateKitt() {
|
||||
const long kittFrame = lround(stableAnimationFrame * KITT_SPEED);
|
||||
const int cycleLength = 2 * LEDS_EDGE_LENGTH + 2 * LEDS_RING_LENGTH;
|
||||
const int vPos = kittFrame % cycleLength;
|
||||
|
||||
ledsDash.setPixelColor(1, colorHSV(animationFrame * RAINBOW_SPEED));
|
||||
ledsDash.setPixelColor(0, colorHSV(animationFrame * RAINBOW_SPEED + 90));
|
||||
ledsHeadLeft.fill(COLOR_OFF);
|
||||
ledsHeadRight.fill(COLOR_OFF);
|
||||
|
||||
// trail behind
|
||||
for (int t = 0; t < KITT_TRAIL; t++) {
|
||||
const int pastVPos = ((vPos - t) % cycleLength + cycleLength) % cycleLength;
|
||||
const int scanPos = kittScanPos(pastVPos);
|
||||
|
||||
const uint32_t color = colorHSV(0, 1, 1.0f - (float)t / KITT_TRAIL);
|
||||
ledsHeadLeft.setPixelColor(scanPos, color);
|
||||
ledsHeadRight.setPixelColor(scanPos, color);
|
||||
}
|
||||
|
||||
// lead ahead — shorter and dimmer than trail
|
||||
constexpr int KITT_LEAD = 2;
|
||||
for (int t = 1; t <= KITT_LEAD; t++) {
|
||||
const int futureVPos = ((vPos + t) % cycleLength + cycleLength) % cycleLength;
|
||||
const int scanPos = kittScanPos(futureVPos);
|
||||
|
||||
const uint32_t color = colorHSV(0, 1, 0.3f * (1.0f - (float)t / KITT_LEAD));
|
||||
ledsHeadLeft.setPixelColor(scanPos, color);
|
||||
ledsHeadRight.setPixelColor(scanPos, color);
|
||||
}
|
||||
|
||||
const int scanPos = kittScanPos(vPos);
|
||||
|
||||
|
||||
// normalized cycle (0–1), sped up
|
||||
float tailCycle = (float)vPos * KITT_TAIL_SPEED / cycleLength;
|
||||
tailCycle -= (int)tailCycle; // wrap to 0–1
|
||||
|
||||
// triangle wave: 0 → 1 → 0 (KITT bounce)
|
||||
float tailT = tailCycle * 2.0f;
|
||||
if (tailT > 1.0f) tailT = 2.0f - tailT;
|
||||
|
||||
// optional smoothing (gives that analog feel)
|
||||
tailT = tailT * tailT * (3.0f - 2.0f * tailT); // smoothstep
|
||||
|
||||
// map to LED index
|
||||
const int tailPos = lround(tailT * (LEDS_TAIL_LENGTH - 1));
|
||||
|
||||
// draw tail with falloff
|
||||
for (int i = 0; i < LEDS_TAIL_LENGTH; i++) {
|
||||
const int dist = abs(i - tailPos);
|
||||
float brightness = 0.0f;
|
||||
|
||||
if (dist < KITT_TRAIL) {
|
||||
brightness = 1.0f - (float)dist / KITT_TRAIL;
|
||||
}
|
||||
|
||||
ledsTail.setPixelColor(i, colorHSV(0, 1, brightness));
|
||||
}
|
||||
|
||||
ledsDash.fill(COLOR_OFF);
|
||||
|
||||
float speed = 3.0;
|
||||
|
||||
float dashCycle = (float)vPos * speed / cycleLength;
|
||||
dashCycle -= (int)dashCycle; // keep it in 0–1
|
||||
|
||||
// triangle wave: 0→1→0 over the cycle
|
||||
float t = dashCycle * 2.0;
|
||||
if (t > 1.0) t = 2.0 - t;
|
||||
|
||||
ledsDash.setPixelColor(0, colorHSV(0, 1, t));
|
||||
ledsDash.setPixelColor(1, colorHSV(0, 1, 1.0 - t));
|
||||
}
|
||||
|
||||
void animate(long now) {
|
||||
@@ -311,6 +465,14 @@ void animate(long now) {
|
||||
animateIndicator(RIGHT);
|
||||
}
|
||||
|
||||
if (activeMode == MODE_KITT) {
|
||||
animateKitt();
|
||||
}
|
||||
|
||||
if (now - lastPedalRelease < 1500) {
|
||||
animateBrake();
|
||||
}
|
||||
|
||||
if (activeMode == MODE_RAINBOW) {
|
||||
animateRainbow();
|
||||
}
|
||||
@@ -353,6 +515,7 @@ void shutdown() {
|
||||
void restart() {
|
||||
lastFrame = millis();
|
||||
animationFrame = 0;
|
||||
stableAnimationFrame = 0;
|
||||
isOff = false;
|
||||
|
||||
digitalWrite(PIN_BUCK, BUCK_ON);
|
||||
@@ -372,6 +535,8 @@ void loop() {
|
||||
activeMode = (Mode)((activeMode + 1) % MODE_COUNT);
|
||||
animationFrame = 0; // ensures animations start cleanly instead of halfway
|
||||
|
||||
lastButtonPress = now;
|
||||
|
||||
Serial.print("BUTTON MODE ");
|
||||
Serial.println(activeMode);
|
||||
}
|
||||
@@ -393,6 +558,8 @@ void loop() {
|
||||
indicatorRightActive = false;
|
||||
indicatorRightStart = 0;
|
||||
|
||||
lastButtonPress = now;
|
||||
|
||||
Serial.println("BUTTON LEFT");
|
||||
}
|
||||
|
||||
@@ -405,6 +572,8 @@ void loop() {
|
||||
indicatorLeftActive = false;
|
||||
indicatorLeftStart = 0;
|
||||
|
||||
lastButtonPress = now;
|
||||
|
||||
Serial.println("BUTTON RIGHT");
|
||||
}
|
||||
|
||||
@@ -429,17 +598,22 @@ void loop() {
|
||||
}
|
||||
|
||||
Serial.println("PEDAL FORWARD");
|
||||
}`3
|
||||
|
||||
if (pedal.released()) {
|
||||
lastPedalRelease = now;
|
||||
}
|
||||
|
||||
if (!isOff && now - lastFrame >= ANIMATION_INTERVAL) {
|
||||
lastFrame += ANIMATION_INTERVAL;
|
||||
animationFrame += 1;
|
||||
stableAnimationFrame += 1;
|
||||
|
||||
animate(now);
|
||||
render();
|
||||
}
|
||||
|
||||
if (KEEPALIVE_ENABLE && now - lastPedalPress > KEEPALIVE_TIMEOUT) {
|
||||
if (KEEPALIVE_ENABLE && now - lastPedalPress > KEEPALIVE_TIMEOUT && now - lastButtonPress > KEEPALIVE_TIMEOUT) {
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user