add on off toggle fade

This commit is contained in:
interfisch 2019-10-12 17:32:20 +02:00
parent f3362835aa
commit d94bee3314
1 changed files with 32 additions and 10 deletions

View File

@ -7,6 +7,8 @@
#define FW_NAME "tischlicht" #define FW_NAME "tischlicht"
#define FW_VERSION "1.0.1" #define FW_VERSION "1.0.1"
//#define DEBUG //turns on continuous serial debug printing
/* /*
* To Update configuration (wifi credentials) from data/homie/config.json: * To Update configuration (wifi credentials) from data/homie/config.json:
* Connect to serial. On ESP-12E connect flash jumper * Connect to serial. On ESP-12E connect flash jumper
@ -55,9 +57,11 @@ HomieNode lightNode("light", "light");
#define PWM_MAX 1023 //10 bit dac #define PWM_MAX 1023 //10 bit dac
boolean sleep=true; //true turns lights off boolean sleep=true; //true turns lights off. like set_..
float sleep_fadevalue=0; //0=off, 1=on
float sleep_fadevalue_change_per_loop=0.01; //fixed value. For manual calculatoin: sleep_fadevalue_change_per_loop=_difference/fadetime*UPDATETIME;
float set_brightness=0; //0 to 1 float set_brightness=2; //0 to 1
#define BRIGHTNESS_MIN 0.0 #define BRIGHTNESS_MIN 0.0
#define BRIGHTNESS_MAX 2.0 //if temperature is in between both strips brightness of 2 means both are at full power. otherwise brightness will be clipped #define BRIGHTNESS_MAX 2.0 //if temperature is in between both strips brightness of 2 means both are at full power. otherwise brightness will be clipped
float brightness=set_brightness; float brightness=set_brightness;
@ -294,6 +298,18 @@ void loopHandler() {
flag_updatePWM=true; //force update flag_updatePWM=true; //force update
} }
//Sleep
if ( (sleep && sleep_fadevalue>0) || (!sleep && sleep_fadevalue<1) ) { //not fully turned off or on
if (sleep) { //turn off
sleep_fadevalue-=sleep_fadevalue_change_per_loop;
}else{ //turn on
sleep_fadevalue+=sleep_fadevalue_change_per_loop;
}
if (sleep_fadevalue>1) { sleep_fadevalue=1; } //limit
if (sleep_fadevalue<0) { sleep_fadevalue=0; }
flag_updatePWM=true; //force update
}
//calculate and update pwm //calculate and update pwm
if (brightness != set_brightness || temperature != set_temperature || flag_updatePWM) { //if target not reached if (brightness != set_brightness || temperature != set_temperature || flag_updatePWM) { //if target not reached
flag_updatePWM=false; // reset flag flag_updatePWM=false; // reset flag
@ -301,22 +317,27 @@ void loopHandler() {
uint16_t pwmCW; uint16_t pwmCW;
uint16_t pwmWW; uint16_t pwmWW;
float temp=mapFloat(temperature, TEMPERATURE_MIN, TEMPERATURE_MAX, 0.0,1.0); //0=warmwhite, 1=coldwhite float temp=mapFloat(temperature, TEMPERATURE_MIN, TEMPERATURE_MAX, 0.0,1.0); //0=warmwhite, 1=coldwhite
pwmCW=brightness*PWM_MAX*temp; pwmCW=(brightness*sleep_fadevalue)*PWM_MAX*temp;
pwmWW=brightness*PWM_MAX*(1-temp); pwmWW=(brightness*sleep_fadevalue)*PWM_MAX*(1-temp);
if (pwmCW>PWM_MAX) { pwmCW=PWM_MAX; } //limit if (pwmCW>PWM_MAX) { pwmCW=PWM_MAX; } //limit
if (pwmWW>PWM_MAX) { pwmWW=PWM_MAX; } //limit if (pwmWW>PWM_MAX) { pwmWW=PWM_MAX; } //limit
analogWrite(LED_WW, PWM_MAX-pwmWW); //full pwm is led off
analogWrite(LED_CW, PWM_MAX-pwmCW); //full pwm is led off
/*
if (!sleep) { if (!sleep) {
analogWrite(LED_WW, PWM_MAX-pwmWW); //full pwm is led off analogWrite(LED_WW, PWM_MAX-pwmWW); //full pwm is led off
analogWrite(LED_CW, PWM_MAX-pwmCW); //full pwm is led off analogWrite(LED_CW, PWM_MAX-pwmCW); //full pwm is led off
}else{ }else{
analogWrite(LED_WW, PWM_MAX); //light off analogWrite(LED_WW, PWM_MAX); //light off
analogWrite(LED_CW, PWM_MAX); //light off analogWrite(LED_CW, PWM_MAX); //light off
} }*/
} }
} }
#ifdef DEBUG
if (loopmillis >= last_debugupdatetime+DEBUGUPDATETIME ) { if (loopmillis >= last_debugupdatetime+DEBUGUPDATETIME ) {
last_debugupdatetime = loopmillis; last_debugupdatetime = loopmillis;
@ -330,9 +351,9 @@ void loopHandler() {
Serial.print(" set="); Serial.print(" set=");
Serial.print(set_temperature); Serial.print(set_temperature);
Serial.print(" change="); Serial.print(" change=");
Serial.println(brightness_change_per_loop); Serial.print(brightness_change_per_loop);
Serial.print(" sleep_fadevalue=");
Serial.println(sleep_fadevalue);
/* /*
uint16_t pwmCW; uint16_t pwmCW;
@ -356,6 +377,7 @@ void loopHandler() {
*/ */
} }
#endif
} }