Added mode switching and indicator animation.
This commit is contained in:
133
QuadLightsV3.ino
133
QuadLightsV3.ino
@@ -59,12 +59,35 @@ 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_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_RAINBOW,
|
||||
MODE_COUNT,
|
||||
};
|
||||
|
||||
Mode activeMode = MODE_HEAD;
|
||||
|
||||
unsigned long animationFrame = 0;
|
||||
unsigned long lastFrame = 0;
|
||||
|
||||
unsigned long lastPedalPress = millis();
|
||||
|
||||
unsigned long indicatorLeftStart = 0;
|
||||
unsigned long indicatorRightStart = 0;
|
||||
bool indicatorLeftActive = false;
|
||||
bool indicatorRightActive = false;
|
||||
|
||||
void setup() {
|
||||
// initialize digital pin LED_BUILTIN as an output.
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
@@ -110,41 +133,60 @@ void setup() {
|
||||
ledsDash.clear();
|
||||
}
|
||||
|
||||
enum Mode {
|
||||
MODE_HEAD,
|
||||
MODE_POLICE,
|
||||
MODE_HAZARD,
|
||||
MODE_RAINBOW,
|
||||
MODE_COUNT,
|
||||
};
|
||||
|
||||
Mode activeMode = MODE_HEAD;
|
||||
|
||||
unsigned long animationFrame = 0;
|
||||
unsigned long lastFrame = 0;
|
||||
|
||||
void animateHeadlights() {
|
||||
ledsHeadLeft.fill(COLOR_WHITE);
|
||||
ledsHeadRight.fill(COLOR_WHITE);
|
||||
|
||||
ledsDash.fill(ledsDash.Color(0, 255, 0, 0));
|
||||
}
|
||||
|
||||
void animateIndicator(Adafruit_NeoPixel& ledsTarget) {
|
||||
Serial.println("ANIMATING INDICATOR");
|
||||
constexpr int INDICATOR_DURATION = 3000; // total ms
|
||||
constexpr int INDICATOR_HOLD_FRAMES = 15;
|
||||
constexpr int INDICATOR_OFF_FRAMES = 20;
|
||||
constexpr float INDICATOR_SPEED = 2;
|
||||
|
||||
for (int ledIndex = LEDS_EDGE_LENGTH; ledIndex < LEDS_EDGE_LENGTH + LEDS_RING_LENGTH; ledIndex += 1) {
|
||||
ledsTarget.setPixelColor(ledIndex, COLOR_AMBER);
|
||||
void animateIndicator(Direction direction) {
|
||||
Adafruit_NeoPixel& ledsTarget = direction == LEFT // reference, not pointer
|
||||
? ledsHeadLeft
|
||||
: ledsHeadRight;
|
||||
|
||||
const int ringStart = LEDS_EDGE_LENGTH;
|
||||
const int ringEnd = LEDS_EDGE_LENGTH + LEDS_RING_LENGTH;
|
||||
|
||||
const long indicatorFrame =
|
||||
lround(animationFrame * INDICATOR_SPEED)
|
||||
% (LEDS_RING_LENGTH + INDICATOR_HOLD_FRAMES + INDICATOR_OFF_FRAMES);
|
||||
|
||||
const long sweepEnd = LEDS_RING_LENGTH;
|
||||
const long holdEnd = LEDS_RING_LENGTH + INDICATOR_HOLD_FRAMES;
|
||||
|
||||
const long sweepIndex = direction == LEFT
|
||||
? ringStart + (indicatorFrame % LEDS_RING_LENGTH)
|
||||
: ringEnd - (indicatorFrame % LEDS_RING_LENGTH);
|
||||
|
||||
for (int ledIndex = ringStart; ledIndex < ringEnd; ledIndex++) {
|
||||
if (indicatorFrame < sweepEnd) {
|
||||
// SWEEP
|
||||
const bool isLit = direction == LEFT
|
||||
? ledIndex < sweepIndex
|
||||
: ledIndex >= sweepIndex;
|
||||
ledsTarget.setPixelColor(ledIndex, isLit ? COLOR_AMBER : COLOR_OFF);
|
||||
|
||||
ledsDash.fill(COLOR_AMBER);
|
||||
} else if (indicatorFrame < holdEnd) {
|
||||
// HOLD
|
||||
ledsTarget.setPixelColor(ledIndex, COLOR_AMBER);
|
||||
} else {
|
||||
// OFF
|
||||
ledsTarget.setPixelColor(ledIndex, COLOR_OFF);
|
||||
}
|
||||
}
|
||||
|
||||
if (indicatorFrame < holdEnd) {
|
||||
ledsDash.fill(COLOR_AMBER);
|
||||
} else {
|
||||
ledsDash.fill(COLOR_OFF);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long lastPedalPress = millis();
|
||||
|
||||
unsigned long lastIndicatingLeft = 0;
|
||||
unsigned long lastIndicatingRight = 0;
|
||||
bool isIndicatingLeft = false;
|
||||
bool isIndicatingRight = false;
|
||||
|
||||
void animate(long now) {
|
||||
if (pedalForward.read() == Button::PRESSED) {
|
||||
ledsTail.fill(ledsTail.Color(255, 0, 0, 0));
|
||||
@@ -164,15 +206,22 @@ void animate(long now) {
|
||||
animateHeadlights();
|
||||
|
||||
// we need the boolean to prevent the indicators from turning on right after boot
|
||||
if (isIndicatingLeft && animationFrame - lastIndicatingLeft < 100) {
|
||||
animateIndicator(ledsHeadLeft);
|
||||
if (indicatorLeftActive && now - indicatorLeftStart < INDICATOR_DURATION) {
|
||||
animateIndicator(LEFT);
|
||||
}
|
||||
|
||||
if (isIndicatingRight && animationFrame - lastIndicatingRight < 100) {
|
||||
animateIndicator(ledsHeadRight);
|
||||
if (indicatorRightActive && now - indicatorRightStart < INDICATOR_DURATION) {
|
||||
animateIndicator(RIGHT);
|
||||
}
|
||||
}
|
||||
|
||||
void render() {
|
||||
ledsTail.show();
|
||||
ledsHeadLeft.show();
|
||||
ledsHeadRight.show();
|
||||
ledsDash.show();
|
||||
}
|
||||
|
||||
// the loop function runs over and over again forever
|
||||
void loop() {
|
||||
const unsigned long now = millis();
|
||||
@@ -187,15 +236,25 @@ void loop() {
|
||||
}
|
||||
|
||||
if (buttonLeft.pressed()) {
|
||||
isIndicatingLeft = true;
|
||||
lastIndicatingLeft = animationFrame;
|
||||
animationFrame = 0; // ensures animations start cleanly instead of halfway
|
||||
|
||||
indicatorLeftActive = true;
|
||||
indicatorLeftStart = now;
|
||||
|
||||
indicatorRightActive = false;
|
||||
indicatorRightStart = 0;
|
||||
|
||||
Serial.println("BUTTON LEFT");
|
||||
}
|
||||
|
||||
if (buttonRight.pressed()) {
|
||||
isIndicatingRight = true;
|
||||
lastIndicatingRight = animationFrame;
|
||||
animationFrame = 0; // ensures animations start cleanly instead of halfway
|
||||
|
||||
indicatorRightActive = true;
|
||||
indicatorRightStart = now;
|
||||
|
||||
indicatorLeftActive = false;
|
||||
indicatorLeftStart = 0;
|
||||
|
||||
Serial.println("BUTTON RIGHT");
|
||||
}
|
||||
@@ -205,11 +264,7 @@ void loop() {
|
||||
animationFrame += 1;
|
||||
|
||||
animate(now);
|
||||
|
||||
ledsTail.show();
|
||||
ledsHeadLeft.show();
|
||||
ledsHeadRight.show();
|
||||
ledsDash.show();
|
||||
render();
|
||||
}
|
||||
|
||||
if (KEEPALIVE_ENABLE && now - lastPedalPress > KEEPALIVE_TIMEOUT) {
|
||||
|
||||
Reference in New Issue
Block a user