Implemented time based transition timing

This commit is contained in:
Kai Lauterbach 2023-04-25 20:06:04 +02:00
parent 9e998f9c92
commit f51c0da0de
3 changed files with 51 additions and 19 deletions

View file

@ -15,6 +15,7 @@
#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_LIGHTENGINE_INTERVAL_MS (100UL)
#define MY_NTP_SERVER "de.pool.ntp.org"

View file

@ -39,7 +39,7 @@ IPAddress dns ( 192, 168, 0, 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 4 // 24V-15V = 9V range; 9V ≙ 1024-640 = 383 counts; 383/100% = 3,83 counts (1%) / % => round up 4 counts / % (~1%)
#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 += "<div class=\"pure-control-group\">";
http_content += "<label for=\"bri\"" + (String)light_num + ">Bri</label>";
http_content += "<input id=\"bri" + (String)light_num + "\" name=\"bri" + (String)light_num + "\" type=\"range\" min=\"0\" max=\"100\" value=\"" + (String)bri[light_num] + "\">";
http_content += "<label id=\"bri" + (String)light_num + "_val\" name=\"bri" + (String)light_num + "\">" + (String)bri[light_num] + "</label>";
http_content += "<input id=\"bri" + (String)light_num + "\" name=\"bri" + (String)light_num + "\" type=\"range\" min=\"0\" max=\"255\" value=\"" + (String)bri[light_num] + "\">";
http_content += "<label id=\"bri" + (String)light_num + "_val\" name=\"bri" + (String)light_num + "\">" + (String)(int)(bri[light_num] * 100.0 / 255.0) + "</label>%";
http_content += "<script>";
http_content += "var slider" + (String)light_num + " = document.getElementById(\"bri" + (String)light_num + "\");";
http_content += "var output" + (String)light_num + " = document.getElementById(\"bri" + (String)light_num + "\_val\");";
http_content += "output" + (String)light_num + ".innerHTML = slider" + (String)light_num + ".value;";
http_content += "output" + (String)light_num + ".innerHTML = (Math.round((slider" + (String)light_num + ".value * 100.0 / 255.0) * 100) / 100).toFixed(2);";
http_content += "slider" + (String)light_num + ".oninput = function() {";
http_content += "output" + (String)light_num + ".innerHTML = this.value;";
http_content += "output" + (String)light_num + ".innerHTML = (Math.round((this.value * 100.0 / 255.0) * 100) / 100).toFixed(2);";
http_content += "}";
http_content += "</script>";
http_content += "</div>";

View file

@ -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]);
}
}
//********************************//