From 3f7fe0b835a9493f12ed564eb11f878b7898e24b Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Wed, 3 Apr 2024 11:00:41 +0200 Subject: [PATCH] Changed ON/OFF handling to fade. --- RiTCh_Lightshow.ino | 113 ++++++++++++++++++++++++++++++++------------ 1 file changed, 84 insertions(+), 29 deletions(-) diff --git a/RiTCh_Lightshow.ino b/RiTCh_Lightshow.ino index 1e89bf0..9a58c57 100644 --- a/RiTCh_Lightshow.ino +++ b/RiTCh_Lightshow.ino @@ -22,7 +22,7 @@ #define DEBUG 1 #define SENSOR_SCHED 10 -#define WORKER_SCHED 100 +#define DMX_COLOR_CHANGER_SCHED 100 #define LED_SCHED 50 #define LEVEL_CHECK_SCHED 100 #define DEBUG_SCHED 500 @@ -35,8 +35,8 @@ #define DMX_MAX_CHANNEL 512 #define MAX_VOL_LEVEL 30 #define MAX_DMX_LEVEL 255 -#define MAX_DMX_SPOTS 2 #define DMX_FOG_MACHINE_ADDRESS 100 +#define MAX_RAND_WHITE_LEVEL 128 #define DMX_BRIGHTNESS 0 #define DMX_FADE_TIME 1 @@ -44,9 +44,10 @@ #define DMX_FOG_LEVEL 3 #define MIN_DMX_FADE_TIME 1 -#define MAX_DMX_FADE_TIME 10 -#define MIN_DMX_HOLD_TIME 5 -#define MAX_DMX_HOLD_TIME 15 +#define MAX_DMX_FADE_TIME 15 +#define MIN_DMX_HOLD_TIME 3 +#define MAX_DMX_HOLD_TIME 30 +#define ON_OFF_FADE_TIME 1 // Arduino pins #define POTI1_PIN A1 @@ -84,6 +85,8 @@ typedef struct { float values[4] = { 0.0, 0.0, 0.0, 0.0 }; } dmx_spot; +#define MAX_DMX_SPOTS 2 + dmx_spot spot[MAX_DMX_SPOTS]; // schedular @@ -147,11 +150,11 @@ void show_led(void) { analogWrite(LED_PIN, LED_PLAY_BRIGHTNESS); } -void worker(void) { +void dmx_auto_colorchanger(void) { // DMX auto Fade - if (light_state != NORMAL) - return; + //if (light_state != NORMAL) + // return; for (uint8_t s = 0; s < MAX_DMX_SPOTS; s++) { uint8_t diff_counter = 0; @@ -165,40 +168,65 @@ void worker(void) { } if (diff_counter != 0) { // start hold timer - int8_t start = MIN_DMX_FADE_TIME + MIN_DMX_FADE_TIME * poti_level[1] / 255.0 * 4; - spot[s].steps = random(start, start + ((MAX_DMX_HOLD_TIME - start) * poti_level[DMX_HOLD_TIME] / 255.0)) * 1000 / WORKER_SCHED; + if (light_state != NORMAL) + continue; + int8_t start = MIN_DMX_HOLD_TIME + MIN_DMX_HOLD_TIME * poti_level[1] / 255.0 * 4; + spot[s].steps = random(start, start + ((MAX_DMX_HOLD_TIME - start) * poti_level[DMX_HOLD_TIME] / 255.0)) * 1000 / DMX_COLOR_CHANGER_SCHED; #ifdef DEBUG Serial.print(F("Spot ")); Serial.print(s, DEC); Serial.print(F(" holding for ")); - Serial.print(spot[s].steps * WORKER_SCHED / 1000, DEC); + Serial.print(spot[s].steps * DMX_COLOR_CHANGER_SCHED / 1000, DEC); Serial.println(F(" seconds")); #endif } else { // new random values + if (light_state != NORMAL) + continue; RGBConverter color_converter; uint8_t rgb[3]; - int8_t start = MIN_DMX_FADE_TIME + MIN_DMX_FADE_TIME * poti_level[1] / 255.0 * 4; + uint8_t start = MIN_DMX_FADE_TIME + MIN_DMX_FADE_TIME * poti_level[2] / 255.0 * 4; - spot[s].steps = random(start, start + ((MAX_DMX_FADE_TIME - start) * poti_level[DMX_FADE_TIME] / 255.0)) * 1000 / WORKER_SCHED; color_converter.hsvToRgb(random(0, 1024) / 1024.0, 1.0, 1.0, rgb); - for (uint8_t i = 0; i < 3; i++) - spot[s].diff[i] = (rgb[i] - spot[s].values[i]) / spot[s].steps; - spot[s].diff[3] = (random(0, 128) - spot[3].values[3]) / 255.0; + + if (spot[s].dmx_type > 3) + set_rgbw(&spot[s], random(start, start + ((MAX_DMX_FADE_TIME - start) * poti_level[DMX_FADE_TIME] / 255.0)), rgb, random(0, MAX_RAND_WHITE_LEVEL)); + else + set_rgb(&spot[s], random(start, start + ((MAX_DMX_FADE_TIME - start) * poti_level[DMX_FADE_TIME] / 255.0)), rgb); + #ifdef DEBUG Serial.print(F("Spot ")); Serial.print(s, DEC); - Serial.print(F(" changing color for ")); - Serial.print(spot[s].steps * WORKER_SCHED / 1000, DEC); + Serial.print(F(" changing color to R[")); + Serial.print(spot[s].values[0], 2); + Serial.print(F("] G[")); + Serial.print(spot[s].values[1], 2); + Serial.print(F("] B[")); + Serial.print(spot[s].values[2], 2); + if (spot[s].dmx_type > 3) { + Serial.print(F("] W[")); + Serial.print(spot[s].values[3], 2); + } + Serial.print(F("] for ")); + Serial.print(spot[s].steps * DMX_COLOR_CHANGER_SCHED / 1000, DEC); Serial.println(F(" seconds")); #endif } } else { for (uint8_t i = 0; i < spot[s].dmx_type; i++) { - spot[s].values[i] += spot[s].diff[i]; - DmxSimple.write(spot[s].address + i, uint8_t(spot[s].values[i] * poti_level[DMX_BRIGHTNESS] / 255.0 + 0.5)); + if (spot[s].diff[i] != 0.0) { + spot[s].values[i] += spot[s].diff[i]; + if (spot[s].values[i] > 255.0) + spot[s].values[i] = 255.0; + else if (spot[s].values[i] < 0) + spot[s].values[i] = 0.0; + + DmxSimple.write(spot[s].address + i, uint8_t(spot[s].values[i] * poti_level[DMX_BRIGHTNESS] / 255.0 + 0.5)); + } + + #ifdef DEBUG -/* Serial.print(F("Spot ")); + Serial.print(F("Spot ")); Serial.print(s, DEC); Serial.print(F(" color ")); Serial.print(i, DEC); @@ -207,8 +235,9 @@ void worker(void) { Serial.print(F(" diff ")); Serial.print(spot[s].diff[i], 4); Serial.print(F(" value ")); - Serial.println(spot[s].values[i], 4);*/ + Serial.println(spot[s].values[i], 4); #endif + } } spot[s].steps--; @@ -312,9 +341,14 @@ void do_button_short(uint8_t b) { #endif light_state = WHITE; for (uint8_t s = 0; s < MAX_DMX_SPOTS; s++) { + uint8_t rgb[3] = { 255, 255, 255 }; for (uint8_t i = 0; i < spot[s].dmx_type; i++) - DmxSimple.write(spot[s].address + i, 255); + if (spot[s].dmx_type > 3) + set_rgbw(&spot[s], ON_OFF_FADE_TIME, rgb, 255); + else + set_rgb(&spot[s], ON_OFF_FADE_TIME, rgb); } + } else { #ifdef DEBUG Serial.println(F("Light: OFF")); @@ -337,8 +371,12 @@ void do_button_short(uint8_t b) { if (light_state == OFF) { for (uint8_t s = 0; s < MAX_DMX_SPOTS; s++) { + uint8_t rgb[3] = { 0, 0, 0 }; for (uint8_t i = 0; i < spot[s].dmx_type; i++) - DmxSimple.write(spot[s].address + i, 0); + if (spot[s].dmx_type > 3) + set_rgbw(&spot[s], ON_OFF_FADE_TIME, rgb, 0); + else + set_rgb(&spot[s], ON_OFF_FADE_TIME, rgb); } } } @@ -404,6 +442,18 @@ uint8_t poti_pin_by_number(byte n) { return (0); } +void set_rgbw(dmx_spot* spot, uint8_t fade, uint8_t* rgb, uint8_t w) { + spot->steps = fade * 1000 / DMX_COLOR_CHANGER_SCHED; + spot->diff[3] = (w - spot->values[3]) / spot->steps; + set_rgb(spot, fade, rgb); +} + +void set_rgb(dmx_spot* spot, uint8_t fade, uint8_t* rgb) { + spot->steps = fade * 1000 / DMX_COLOR_CHANGER_SCHED; + for (uint8_t i = 0; i < 3; i++) + spot->diff[i] = (rgb[i] - spot->values[i]) / spot->steps; +} + //-------------------------------------------------------------------------------- // SYSTEM //-------------------------------------------------------------------------------- @@ -412,13 +462,14 @@ void setup() { Serial.begin(9600); #endif - /* #ifdef __AVR_ATmega32U4__ - while (!Serial) { - ; // wait for serial port to connect. Needed for Leonardo only + for (uint8_t i = 0; i <= 10; i++) { + if (!Serial) + delay(100); + else + break; } #endif -*/ #ifdef DEBUG Serial.println(F("")); @@ -453,6 +504,10 @@ void setup() { // DMX setup spot[0].address = 1; spot[1].address = 5; +/* spot[2].address = 10; + spot[3].address = 14; + spot[4].address = 17; + spot[5].address = 20; */ // setup audio card #ifdef DEBUG @@ -485,7 +540,7 @@ void setup() { // init schedular sched.addJob(button_check, SENSOR_SCHED); sched.addJob(level_check, LEVEL_CHECK_SCHED); - sched.addJob(worker, WORKER_SCHED); + sched.addJob(dmx_auto_colorchanger, DMX_COLOR_CHANGER_SCHED); sched.addJob(show_led, LED_SCHED); //sched.addJob(test_worker, 1000); #ifdef DEBUG