#include <FastLED.h>
#define LED_PIN 2 // Pin connected to the LED strip
#define NUM_LEDS 10 // Number of LEDs in the strip
#define BRAKE_PIN 3 // Digital input pin for brake signal
#define LEFT_PIN 5 // Digital input pin for left turn signal
#define RIGHT_PIN 4 // Digital input pin for right turn signal
#define KILL_PIN 2 // Digital input pin for kill switch
CRGB leds[NUM_LEDS];
volatile bool killSwitchEnabled = false;
volatile bool brakeEnabled = false;
int myLineCounter = 0;
void myDebugPrint(String message)
{
Serial.print(String(myLineCounter));
Serial.println(" :" + message);
myLineCounter++;
}
void setup() {
//For Debug
Serial.begin(115200);
// Attiny85 has ony two external interrupt pins
pinMode(BRAKE_PIN, INPUT_PULLUP); // INT0 <- phisical pin 2
pinMode(KILL_PIN, INPUT_PULLUP); // INT1 <- phisical pin 3
pinMode(LEFT_PIN, INPUT);
pinMode(RIGHT_PIN, INPUT);
// Register external interupts
attachInterrupt(digitalPinToInterrupt(KILL_PIN), killSwitchISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(BRAKE_PIN), brakeISR, CHANGE);
// Setup adresable leds
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
if (!killSwitchEnabled) {
// Only light up if kill switch is not enabled
if (brakeEnabled) {
// Brake signal detected, light up LED strip
//startBrakeAnimation();
myDebugPrint("brakeEnabled");
}
else {
// Check for left turn signal
if (digitalRead(LEFT_PIN) == HIGH && !killSwitchEnabled) {
// Left turn signal detected
// Indicate left turn here
// startLeftTurnAnimation();
myDebugPrint("startLeftTurnAnimation");
}
// Check for right turn signal
if (digitalRead(RIGHT_PIN) == HIGH && !killSwitchEnabled) {
// Right turn signal detected
// Indicate right turn here
//startRightTurnAnimation();
myDebugPrint("startRightTurnAnimation");
}
}
}
else {
myDebugPrint("killSwitchEnabled");
}
}
void killSwitchISR() {
// Kill switch interrupt service routine
killSwitchEnabled = !killSwitchEnabled;
myDebugPrint("killSwitchISR");
}
void brakeISR() {
// Brake interrupt service routine
brakeEnabled = !brakeEnabled;
myDebugPrint("brakeISR");
}
void startLeftTurnAnimation() {
// Left turn animation code
while (true)
{
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red; // Change color to whatever you like
FastLED.show();
// Test this evry iteration in order to be sure
// that its true and we can animate
if (brakeEnabled || killSwitchEnabled || !digitalRead(LEFT_PIN)) return;
delay(100); // Adjust the delay as needed
}
// Reset LEDs
fill_solid(leds, NUM_LEDS, CRGB::Black);
}
}
void startRightTurnAnimation() {
// Right turn animation code
while (true)
{
for (int i = NUM_LEDS - 1; i >= 0; i--) {
leds[i] = CRGB::Red; // Change color to whatever you like
FastLED.show();
// Test this evry iteration in order to be sure
// that its true and we can animate
if (brakeEnabled || killSwitchEnabled || !digitalRead(RIGHT_PIN)) return;
delay(100); // Adjust the delay as needed
}
// Reset LEDs
fill_solid(leds, NUM_LEDS, CRGB::Black);
}
}
void startBrakeAnimation() {
// Brake animation code
while (true)
{
// Loop for blinking effect
for (int i = 0; i < 5; i++) {
// Turn on all LEDs
fill_solid(leds, NUM_LEDS, CRGB::Red);
FastLED.show();
delay(200); // Adjust blinking speed as needed
// Test this evry iteration in order to be sure
// that its true and we can animate
if (killSwitchEnabled) return;
// Turn off all LEDs
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(200); // Adjust blinking speed as needed
}
}
}