From 5c5e9c2dc95db3b6693caf09541bcd6e82d43ebe Mon Sep 17 00:00:00 2001 From: ThePendulum Date: Tue, 28 Apr 2026 01:46:49 +0200 Subject: [PATCH] Initial commit. Default output for head and rear lights. Reading pedal and buttons. --- QuadLightsV3.ino | 179 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 + 2 files changed, 181 insertions(+) create mode 100644 QuadLightsV3.ino create mode 100644 README.md diff --git a/QuadLightsV3.ino b/QuadLightsV3.ino new file mode 100644 index 0000000..215f44d --- /dev/null +++ b/QuadLightsV3.ino @@ -0,0 +1,179 @@ +#include +#include + +const bool KEEPALIVE_ENABLE = false; +const int KEEPALIVE_TIMEOUT = 5000; // ms + +const int LEDS_TAIL_PIN = PA6; +const int LEDS_TAIL_LENGTH = 10; +const 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; + +const int LEDS_LEFT_PIN = PA5; +const int LEDS_RIGHT_PIN = PA7; + +const int PIN_ALIVE = PA2; +const int PIN_BUCK = PA1; + +const int PIN_PEDAL = PA0; +const int PIN_PEDAL_FORWARD = PA3; +const int PIN_PEDAL_REVERSE = PA4; + +const int PIN_BUTTON_MODE = PB8; +const int PIN_BUTTON_RIGHT = PB7; +const int PIN_BUTTON_LEFT = PB9; + +const int LED_ON = LOW; +const int LED_OFF = HIGH; + +const int PEDAL_ON = HIGH; +const int PEDAL_OFF = LOW; + +const int BUCK_ON = HIGH; +const int BUCK_OFF = LOW; + +Button pedal(PIN_PEDAL); +Button pedalForward(PIN_PEDAL_FORWARD); +Button pedalReverse(PIN_PEDAL_REVERSE); + +Button buttonMode(PIN_BUTTON_MODE); +Button buttonLeft(PIN_BUTTON_LEFT); +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); + +void setup() { + // initialize digital pin LED_BUILTIN as an output. + pinMode(LED_BUILTIN, OUTPUT); + pinMode(PIN_ALIVE, OUTPUT); + pinMode(PIN_BUCK, OUTPUT); + pinMode(PIN_PEDAL, INPUT_PULLUP); + + pinMode(PIN_PEDAL_FORWARD, INPUT_PULLUP); + pinMode(PIN_PEDAL_REVERSE, INPUT_PULLUP); + + pinMode(PIN_BUTTON_MODE, INPUT_PULLUP); + pinMode(PIN_BUTTON_LEFT, INPUT_PULLUP); + pinMode(PIN_BUTTON_RIGHT, INPUT_PULLUP); + + // digitalWrite(LED_BUILTIN, LED_ON); + digitalWrite(PIN_ALIVE, HIGH); + digitalWrite(PIN_BUCK, BUCK_ON); + digitalWrite(LED_BUILTIN, LED_OFF); + + ledsTail.setBrightness(LEDS_TAIL_BRIGHTNESS); + ledsHeadLeft.setBrightness(LEDS_HEAD_BRIGHTNESS); + ledsHeadRight.setBrightness(LEDS_HEAD_BRIGHTNESS); + + ledsTail.begin(); + ledsHeadLeft.begin(); + ledsHeadRight.begin(); + + pedal.begin(); + pedalForward.begin(); + pedalReverse.begin(); + + buttonMode.begin(); + buttonLeft.begin(); + buttonRight.begin(); + + Serial.begin(9600); +} + +bool isOff = false; +bool isLedOn = false; +bool isBuckOn = false; + +long lastPedalPress = millis(); + +// the loop function runs over and over again forever +void loop() { + const long now = millis(); + + // ledsTail.clear(); + + if (pedalForward.read() == Button::PRESSED) { + ledsTail.fill(ledsTail.Color(255, 0, 0, 0)); + lastPedalPress = now; + + Serial.println("PEDAL FORWARD"); + } else if (pedalReverse.read() == Button::PRESSED) { + ledsTail.fill(ledsTail.Color(0, 0, 0, 255)); + lastPedalPress = now; + + Serial.println("PEDAL REVERSE"); + } else { + ledsTail.fill(ledsTail.Color(100, 0, 0, 0)); + } + + ledsHeadLeft.fill(ledsHeadLeft.Color(0, 0, 0, 255)); + ledsHeadRight.fill(ledsHeadRight.Color(0, 0, 0, 255)); + + if (buttonMode.pressed()) { + ledsTail.fill(ledsTail.Color(0, 0, 255, 0)); + + Serial.println("BUTTON MODE"); + } + + if (buttonLeft.pressed()) { + ledsTail.fill(ledsTail.Color(0, 255, 255, 0)); + + Serial.println("BUTTON LEFT"); + } + + if (buttonRight.pressed()) { + ledsTail.fill(ledsTail.Color(255, 0, 255, 0)); + + Serial.println("BUTTON RIGHT"); + } + + ledsTail.show(); + ledsHeadLeft.show(); + ledsHeadRight.show(); + + delay(100); + + /* + if (pedal.read() == Button::PRESSED) { + digitalWrite(LED_BUILTIN, LED_ON); + lastPedalPress = now; + } else { + digitalWrite(LED_BUILTIN, LED_OFF); + } + + 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"); + + Serial.println("BUCK OFF"); + digitalWrite(PIN_BUCK, LOW); + + delay(100); + + Serial.println("SELF OFF, GOODBYE"); + digitalWrite(PIN_ALIVE, LOW); + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b40322 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# ATVLED +Arduino light control script for toy cars and ATVs.