From f6a6723f065820b028a012fde11701ded8f2e453 Mon Sep 17 00:00:00 2001 From: ThePendulum Date: Tue, 28 Apr 2026 06:07:16 +0200 Subject: [PATCH] Added dash indicator ouputs. --- QuadLightsV3.ino | 178 ++++++++++++++++++++++++++++++----------------- 1 file changed, 113 insertions(+), 65 deletions(-) diff --git a/QuadLightsV3.ino b/QuadLightsV3.ino index 215f44d..7c5edc9 100644 --- a/QuadLightsV3.ino +++ b/QuadLightsV3.ino @@ -1,42 +1,50 @@ #include #include -const bool KEEPALIVE_ENABLE = false; -const int KEEPALIVE_TIMEOUT = 5000; // ms +constexpr bool KEEPALIVE_ENABLE = false; +constexpr int KEEPALIVE_TIMEOUT = 5000; // ms -const int LEDS_TAIL_PIN = PA6; -const int LEDS_TAIL_LENGTH = 10; -const int LEDS_TAIL_BRIGHTNESS = 255; +constexpr int LEDS_TAIL_PIN = PA6; +constexpr int LEDS_TAIL_LENGTH = 10; +constexpr int LEDS_TAIL_BRIGHTNESS = 255; -const int LEDS_TOP_LENGTH = 11; -const int LEDS_SIDE_LENGTH = 2; -const int LEDS_BOTTOM_LENGTH = 10; -const int LEDS_RING_LENGTH = 16; -const int LEDS_HEAD_LENGTH = LEDS_TOP_LENGTH + LEDS_SIDE_LENGTH + LEDS_BOTTOM_LENGTH + LEDS_RING_LENGTH; -const int LEDS_HEAD_BRIGHTNESS = 100; +constexpr int LEDS_LEFT_PIN = PA5; +constexpr int LEDS_RIGHT_PIN = PA7; -const int LEDS_LEFT_PIN = PA5; -const int LEDS_RIGHT_PIN = PA7; +constexpr int LEDS_TOP_LENGTH = 11; +constexpr int LEDS_SIDE_LENGTH = 2; +constexpr int LEDS_BOTTOM_LENGTH = 10; +constexpr int LEDS_RING_LENGTH = 16; +constexpr int LEDS_EDGE_LENGTH = LEDS_TOP_LENGTH + LEDS_SIDE_LENGTH + LEDS_BOTTOM_LENGTH; +constexpr int LEDS_HEAD_LENGTH = LEDS_TOP_LENGTH + LEDS_SIDE_LENGTH + LEDS_BOTTOM_LENGTH + LEDS_RING_LENGTH; +constexpr int LEDS_HEAD_BRIGHTNESS = 100; -const int PIN_ALIVE = PA2; -const int PIN_BUCK = PA1; +constexpr int LEDS_DASH_PIN = PB0; +constexpr int LEDS_DASH_LENGTH = 10; +constexpr int LEDS_DASH_BRIGHTNESS = 255; -const int PIN_PEDAL = PA0; -const int PIN_PEDAL_FORWARD = PA3; -const int PIN_PEDAL_REVERSE = PA4; +constexpr int PIN_ALIVE = PA2; +constexpr int PIN_BUCK = PA1; -const int PIN_BUTTON_MODE = PB8; -const int PIN_BUTTON_RIGHT = PB7; -const int PIN_BUTTON_LEFT = PB9; +constexpr int PIN_PEDAL = PA0; +constexpr int PIN_PEDAL_FORWARD = PA3; +constexpr int PIN_PEDAL_REVERSE = PA4; -const int LED_ON = LOW; -const int LED_OFF = HIGH; +constexpr int PIN_BUTTON_MODE = PB8; +constexpr int PIN_BUTTON_LEFT = PB7; +constexpr int PIN_BUTTON_RIGHT = PB9; -const int PEDAL_ON = HIGH; -const int PEDAL_OFF = LOW; +constexpr int LED_ON = LOW; +constexpr int LED_OFF = HIGH; -const int BUCK_ON = HIGH; -const int BUCK_OFF = LOW; +constexpr int PEDAL_ON = HIGH; +constexpr int PEDAL_OFF = LOW; + +constexpr int BUCK_ON = HIGH; +constexpr int BUCK_OFF = LOW; + +constexpr int ANIMATION_FPS = 30; +constexpr int ANIMATION_INTERVAL = lround(1000 / ANIMATION_FPS); Button pedal(PIN_PEDAL); Button pedalForward(PIN_PEDAL_FORWARD); @@ -49,6 +57,13 @@ Button buttonRight(PIN_BUTTON_RIGHT); Adafruit_NeoPixel ledsTail(LEDS_TAIL_LENGTH, LEDS_TAIL_PIN, NEO_GRBW + NEO_KHZ800); Adafruit_NeoPixel ledsHeadLeft(LEDS_HEAD_LENGTH, LEDS_LEFT_PIN, NEO_GRBW + NEO_KHZ800); 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_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); void setup() { // initialize digital pin LED_BUILTIN as an output. @@ -72,10 +87,12 @@ void setup() { ledsTail.setBrightness(LEDS_TAIL_BRIGHTNESS); ledsHeadLeft.setBrightness(LEDS_HEAD_BRIGHTNESS); ledsHeadRight.setBrightness(LEDS_HEAD_BRIGHTNESS); + ledsDash.setBrightness(LEDS_DASH_BRIGHTNESS); ledsTail.begin(); ledsHeadLeft.begin(); ledsHeadRight.begin(); + ledsDash.begin(); pedal.begin(); pedalForward.begin(); @@ -86,20 +103,49 @@ void setup() { buttonRight.begin(); Serial.begin(9600); + + ledsTail.clear(); + ledsHeadLeft.clear(); + ledsHeadRight.clear(); + ledsDash.clear(); } -bool isOff = false; -bool isLedOn = false; -bool isBuckOn = false; +enum Mode { + MODE_HEAD, + MODE_POLICE, + MODE_HAZARD, + MODE_RAINBOW, + MODE_COUNT, +}; -long lastPedalPress = millis(); +Mode activeMode = MODE_HEAD; -// the loop function runs over and over again forever -void loop() { - const long now = millis(); +unsigned long animationFrame = 0; +unsigned long lastFrame = 0; - // ledsTail.clear(); +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"); + + for (int ledIndex = LEDS_EDGE_LENGTH; ledIndex < LEDS_EDGE_LENGTH + LEDS_RING_LENGTH; ledIndex += 1) { + ledsTarget.setPixelColor(ledIndex, COLOR_AMBER); + } +} + +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)); lastPedalPress = now; @@ -114,56 +160,58 @@ void loop() { ledsTail.fill(ledsTail.Color(100, 0, 0, 0)); } - ledsHeadLeft.fill(ledsHeadLeft.Color(0, 0, 0, 255)); - ledsHeadRight.fill(ledsHeadRight.Color(0, 0, 0, 255)); + // always animate headlights, let other modes override + animateHeadlights(); + + // we need the boolean to prevent the indicators from turning on right after boot + if (isIndicatingLeft && animationFrame - lastIndicatingLeft < 100) { + animateIndicator(ledsHeadLeft); + } + + if (isIndicatingRight && animationFrame - lastIndicatingRight < 100) { + animateIndicator(ledsHeadRight); + } +} + +// the loop function runs over and over again forever +void loop() { + const unsigned long now = millis(); if (buttonMode.pressed()) { ledsTail.fill(ledsTail.Color(0, 0, 255, 0)); - Serial.println("BUTTON MODE"); + activeMode = (Mode)((activeMode + 1) % MODE_COUNT); + + Serial.print("BUTTON MODE "); + Serial.println(activeMode); } if (buttonLeft.pressed()) { - ledsTail.fill(ledsTail.Color(0, 255, 255, 0)); + isIndicatingLeft = true; + lastIndicatingLeft = animationFrame; Serial.println("BUTTON LEFT"); } if (buttonRight.pressed()) { - ledsTail.fill(ledsTail.Color(255, 0, 255, 0)); + isIndicatingRight = true; + lastIndicatingRight = animationFrame; Serial.println("BUTTON RIGHT"); } - ledsTail.show(); - ledsHeadLeft.show(); - ledsHeadRight.show(); + if (now - lastFrame >= ANIMATION_INTERVAL) { + lastFrame += ANIMATION_INTERVAL; + animationFrame += 1; - delay(100); + animate(now); - /* - if (pedal.read() == Button::PRESSED) { - digitalWrite(LED_BUILTIN, LED_ON); - lastPedalPress = now; - } else { - digitalWrite(LED_BUILTIN, LED_OFF); + ledsTail.show(); + ledsHeadLeft.show(); + ledsHeadRight.show(); + ledsDash.show(); } - if ((now - lastPedalPress > 2000) && (now - lastPedalPress <= 4000) && !isBuckOn) { - digitalWrite(PIN_BUCK, BUCK_ON); - Serial.println("BUCK ON"); - - isBuckOn = true; - } - - if ((now - lastPedalPress > 4000) && isBuckOn) { - digitalWrite(PIN_BUCK, BUCK_OFF); - Serial.println("BUCK OFF"); - - isBuckOn = false; - } - */ - if (KEEPALIVE_ENABLE && now - lastPedalPress > KEEPALIVE_TIMEOUT) { // digitalWrite(PIN_BUCK, BUCK_OFF); Serial.println("SHUTTING DOWN");