From f51c0da0de205076d46492fe825cd2eff00beda7 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Tue, 25 Apr 2023 20:06:04 +0200 Subject: [PATCH] Implemented time based transition timing --- firmware/config.h | 3 ++- firmware/firmware.ino | 42 +++++++++++++++++++++++++------------ firmware/timing_control.ino | 25 +++++++++++++++++----- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index f190aec..ba39213 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -14,7 +14,8 @@ #define EEPROM_LAST_STATE_ADDRESS 4 // the first "last state" information for the first light #define EEPROM_TIMING_DATA_ADDRESS (EEPROM_LAST_STATE_ADDRESS + LIGHTS_COUNT) // Stored data date per light ELE_USED; HH; MM; CH1; CH2; CH3; CH4; -#define TIME_CHECK_INTERVAL_MS (60000UL) // 60 second interval +#define TIME_CHECK_INTERVAL_MS (60000UL) // 60 second interval +#define TIME_LIGHTENGINE_INTERVAL_MS (100UL) #define MY_NTP_SERVER "de.pool.ntp.org" diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 2d8c58b..92e5624 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -36,10 +36,10 @@ IPAddress dns ( 192, 168, 0, 1); #define SCENE_NIGHTLY 2 // 10 bit PWM -#define PWM_OFF 0 // 0V -#define PWM_MIN 640 // 15V - minimum light amount (~1%) -#define PWM_MAX 1023 // 24V - maximum light amount (100%) -#define BRI_TO_PWM_FACTOR 4 // 24V-15V = 9V range; 9V ≙ 1024-640 = 383 counts; 383/100% = 3,83 counts (1%) / % => round up 4 counts / % (~1%) +#define PWM_OFF 0 // 0V +#define PWM_MIN 640 // 15V - minimum light amount (~1%) +#define PWM_MAX 1023 // 24V - maximum light amount (100%) +#define BRI_TO_PWM_FACTOR 1.506 // 24V-15V = 9V range; 9V ≙ 1024-640 = 384 counts; 384/255 counts =~ 1,506 counts //********************************// @@ -59,6 +59,8 @@ byte mac[6]; ESP8266WebServer server(80); ESP8266HTTPUpdateServer httpUpdateServer; +uint32_t last_lightengine_activity = 0; + //********************************// void apply_scene(uint8_t new_scene, uint8_t light) @@ -95,6 +97,15 @@ void process_lightdata(uint8_t light, float transitiontime) void lightEngine() { + + if (millis() < (last_lightengine_activity + TIME_LIGHTENGINE_INTERVAL_MS)) + { + // abort processing, the transition setting is a delay of seconds + return; + } + + last_lightengine_activity = millis(); + for (int i = 0; i < LIGHTS_COUNT; i++) { if (light_state[i]) @@ -102,27 +113,31 @@ void lightEngine() if (bri[i] != current_bri[i]) { in_transition = true; - current_bri[i] += step_level[i]; + current_bri[i] += step_level[i] / 10.0; if ((step_level[i] > 0.0 && current_bri[i] > bri[i]) || (step_level[i] < 0.0 && current_bri[i] < bri[i])) { current_bri[i] = bri[i]; } - analogWrite(pins[i], calcPWM(current_bri[i])); + uint16_t tmp_pwm = calcPWM(current_bri[i]); + Serial.println("pin" + (String)i + " = PWM(" + (String)tmp_pwm + ")"); + analogWrite(pins[i], tmp_pwm); } } else { if (current_bri[i] != 0) { in_transition = true; - current_bri[i] -= step_level[i]; + current_bri[i] -= step_level[i] / 10.0; if (current_bri[i] < 0) { current_bri[i] = 0; } - analogWrite(pins[i], calcPWM(current_bri[i])); + uint16_t tmp_pwm = calcPWM(current_bri[i]); + Serial.println("pin" + (String)i + " = PWM(" + (String)tmp_pwm + ")"); + analogWrite(pins[i], tmp_pwm); } } } @@ -147,6 +162,7 @@ uint16_t calcPWM(float tbri) { tmp_pwm = PWM_MAX; } + Serial.println((String)tbri + " => " + (String)tmp_pwm); return tmp_pwm; } @@ -485,7 +501,7 @@ void init_webserver() if (server.hasArg("bri" + (String)light)) { - bri[light] = server.arg("bri" + (String)light).toInt(); + bri[light] = (int)server.arg("bri" + (String)light).toInt(); Serial.print("Brightness set to "); Serial.println(bri[light]); } @@ -587,14 +603,14 @@ void init_webserver() http_content += "
"; http_content += ""; - http_content += ""; - http_content += ""; + http_content += ""; + http_content += "%"; http_content += ""; http_content += "
"; diff --git a/firmware/timing_control.ino b/firmware/timing_control.ino index d401337..8def94c 100644 --- a/firmware/timing_control.ino +++ b/firmware/timing_control.ino @@ -126,24 +126,39 @@ void tc_update() bri[2] = tc_data[target_data_block].ch3; bri[3] = tc_data[target_data_block].ch4; + for (uint8_t i = 0; i < LIGHTS_COUNT; i++) + { + Serial.println("bri[" + (String)i + "] = " + (String)bri[i]); + } + // 4. enable/disable the lights light_state[0] = tc_data[target_data_block].ch1 > 0 ? true : false; - light_state[0] = tc_data[target_data_block].ch2 > 0 ? true : false; - light_state[0] = tc_data[target_data_block].ch3 > 0 ? true : false; - light_state[0] = tc_data[target_data_block].ch4 > 0 ? true : false; + light_state[1] = tc_data[target_data_block].ch2 > 0 ? true : false; + light_state[2] = tc_data[target_data_block].ch3 > 0 ? true : false; + light_state[3] = tc_data[target_data_block].ch4 > 0 ? true : false; + + for (uint8_t i = 0; i < LIGHTS_COUNT; i++) + { + Serial.println("light_state[" + (String)i + "] = " + (String)light_state[i]); + } // 5. set the transition time int t_time = 0; if (target_data_block > 0) { - t_time = (tc_data[target_data_block].hh * 60 * 30) - (tc_data[target_data_block-1].hh * 60 * 30); // hours as seconds from now on to the next enabled block - t_time += (tc_data[target_data_block].mm * 60) - (tc_data[target_data_block-1].mm * 60); // add the left over seconds to the next enabled block + t_time = ((uint16_t)tc_data[target_data_block].hh * 60 * 30) - ((uint16_t)tc_data[target_data_block-1].hh * 60 * 30); // hours as seconds from now on to the next enabled block + t_time += ((uint16_t)tc_data[target_data_block].mm * 60) - ((uint16_t)tc_data[target_data_block-1].mm * 60); // add the left over seconds to the next enabled block } transitiontime[0] = t_time; transitiontime[1] = t_time; transitiontime[2] = t_time; transitiontime[3] = t_time; + for (uint8_t i = 0; i < LIGHTS_COUNT; i++) + { + Serial.println("transitiontime[" + (String)i + "] = " + (String)transitiontime[i]); + } + } //********************************//