From 98f7689d8fbf9accabcb1c8d5ef4cd7d00c9ad91 Mon Sep 17 00:00:00 2001 From: Adrian Baumgart Date: Mon, 13 Nov 2023 20:38:22 +0100 Subject: [PATCH] added fade animations --- software/src/main.cpp | 72 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/software/src/main.cpp b/software/src/main.cpp index 6e604b5..a1f0f83 100644 --- a/software/src/main.cpp +++ b/software/src/main.cpp @@ -7,7 +7,8 @@ #include #include -void set_all_leds(CRGB leds[], int size, CRGB color); +void set_all_leds(CRGB color, int brightness, int fade); +void set_strip(CRGB leds[], int size, CRGB color); // Color when interior lights are on (e.g. door is open) // Color Temperature RGB values from: https://andi-siess.de/rgb-to-color-temperature/ @@ -22,8 +23,8 @@ CRGB driving_rgb = CRGB(240, 17, 17); #define RELAY_1 3 // Input config -#define IGNITION_POWER 7 -#define LIGHTING_POWER 8 +#define IGNITION_POWER A0 +#define LIGHTING_POWER A1 bool ignition_on = false; bool lighting_on = false; @@ -41,6 +42,13 @@ CRGB fl_leds[LED_FL_NUM]; #define LED_FR_NUM 20 CRGB fr_leds[LED_FR_NUM]; +// Animation settings +#define FADE_ITERATIONS 50 +#define INTERIOR_FADE 1 +#define DRIVING_FADE 2 + +int current_brightness = 0; +CRGB current_color = CRGB(0,0,0); void setup() { // Add front-left LEDs @@ -52,14 +60,16 @@ void setup() { pinMode(RELAY_1, OUTPUT); pinMode(IGNITION_POWER, INPUT_PULLUP); pinMode(LIGHTING_POWER, INPUT_PULLUP); - + digitalWrite(RELAY_1, LOW); + Serial.begin(9600); } void loop() { - ignition_on = digitalRead(IGNITION_POWER); - lighting_on = digitalRead(LIGHTING_POWER); + ignition_on = analogRead(IGNITION_POWER) > 1000; + lighting_on = analogRead(LIGHTING_POWER) > 1000; if (!ignition_on && !lighting_on) { // Turn off LED Strip power + set_all_leds(CRGB(0,0,0), 0, 0); digitalWrite(RELAY_1, LOW); } else { @@ -68,18 +78,54 @@ void loop() { // Interior light has priority if (lighting_on) { - FastLED.setBrightness(INTERIOR_BRIGHTNESS); - set_all_leds(fl_leds, LED_FL_NUM, interior_rgb); - set_all_leds(fr_leds, LED_FR_NUM, interior_rgb); + + set_all_leds(interior_rgb, INTERIOR_BRIGHTNESS, INTERIOR_FADE); } else if (ignition_on) { - FastLED.setBrightness(DRIVING_BRIGHTNESS); - set_all_leds(fl_leds, LED_FL_NUM, driving_rgb); - set_all_leds(fr_leds, LED_FR_NUM, driving_rgb); + set_all_leds(driving_rgb, DRIVING_BRIGHTNESS, DRIVING_FADE); } } -void set_all_leds(CRGB leds[], int size, CRGB color) { +void set_all_leds(CRGB color, int brightness, int fade) { + // Calculate the difference between the new value and the current value. + // The order is important to have the right prefix. + // e.g. 200 -> 50 => 50-200 => -150 + // 50 -> 200 => 200-50 = 150 + int delta_brightness = brightness - current_brightness; + int delta_r = color.red - current_color.red; + int delta_g = color.green - current_color.green; + int delta_b = color.blue - current_color.blue; + + for(int i = 1; i<=FADE_ITERATIONS;i++) { + // Calculate the new rgb and brightness value for each iteration. + // The delta is divided by the amount of fade iterations to get the required delta for each iteration + // Then we multiply this by the current iteration count, so it goes towards the total delta value from above + int brightness_step_value = current_brightness + ((delta_brightness / FADE_ITERATIONS)*i); + int red_step_value = current_color.red + ((delta_r / FADE_ITERATIONS)*i); + int green_step_value = current_color.green + ((delta_g / FADE_ITERATIONS)*i); + int blue_step_value = current_color.blue + ((delta_b / FADE_ITERATIONS)*i); + + // to account for any rounding issues in the step value calculations, the right brightness and color is manually set on the last iteration + if (i == FADE_ITERATIONS) { + brightness_step_value = brightness; + red_step_value = color.red; + green_step_value = color.green; + blue_step_value = color.blue; + } + + CRGB step_color = CRGB(red_step_value, green_step_value, blue_step_value); + FastLED.setBrightness(brightness_step_value); + + set_strip(fl_leds, LED_FL_NUM, step_color); + set_strip(fr_leds, LED_FR_NUM, step_color); + + delay(fade/FADE_ITERATIONS); + } + current_brightness = brightness; + current_color = color; +} + +void set_strip(CRGB leds[], int size, CRGB color) { for(int i = 0;i