fluorescent effect first tries

This commit is contained in:
interfisch 2018-11-18 22:37:27 +01:00
parent 4eedd68546
commit 87fc8d12b2
1 changed files with 46 additions and 14 deletions

View File

@ -30,6 +30,11 @@ int fluorescentSet=0;
bool fluorescentActive=false;
long fluorescentLastActivated=0;
#define FLUORESCENT_TIMEDIVIDE 10
int fluorescentTemp=0;
#define FLUORESCENTUPDATEINTERVAL 20
long fluorescentLastUpdated=0;
int fluorescentCurrentBrightness=0; //current brightness for effect duration
#define FLUORESCENTTEMPMAX 400
bool lastSensorValue = false;
@ -130,16 +135,20 @@ bool fluorescentHandler(const HomieRange& range, const String& value) {
lightNode.setProperty("fluorescent").send(value);
fluorescentSet=value.toInt();
disco = false;
if (fluorescentSet==0){
if (fluorescentSet==0){ // turned off
fluorescentActive=false; //set effect off
w0 = 0;
w1 = 0;
w2 = 0;
w3 = 0;
}else{
}else{ //turned on
if (w0==0 && w1==0 && w2==0 && w3==0){ //turned on and was off before
//Initialization
fluorescentActive=true; //start effect
fluorescentLastActivated=millis();
fluorescentLastUpdated=millis();
fluorescentTemp=0; //"temperature" for warmup
fluorescentCurrentBrightness=0; //brightness for effect duration
}
}
output();
@ -267,20 +276,43 @@ void loopHandler()
}
}
if (fluorescentActive){
long _time=millis()-fluorescentLastActivated;
int _b=_time/10;
if (_time/10>fluorescentSet){
fluorescentActive=false;
_b=fluorescentSet; //set disired value
}
long _time=millis()-fluorescentLastActivated; //time since activated
//mosquitto_pub -h raum.ctdo.de -t "homie/esp-deckenlicht/strip/fluorescent/set" -m "255"
if (millis() > fluorescentLastUpdated+FLUORESCENTUPDATEINTERVAL){ //Update values
fluorescentTemp+=random(0, 5); //min (inclusive), max (exclusive)
fluorescentLastUpdated=millis();
if (random(0,256)<fluorescentTemp*1.0/FLUORESCENTTEMPMAX*256){ //the warmer, the more often
if (random(0,10)==0){ //ignite
fluorescentCurrentBrightness=fluorescentSet*0.9;
}
}
if (random(0,256)>fluorescentTemp*1.0/FLUORESCENTTEMPMAX*256){ //the colder, the more often
if (fluorescentCurrentBrightness<5){ //not on
if (random(0,20)==0){ //ignite
fluorescentCurrentBrightness=fluorescentSet*0.9;
}
}
fluorescentCurrentBrightness-=50;
}
if (_b<0){
_b=0;
}else if (_b>255){
_b=255;
if (fluorescentTemp>=FLUORESCENTTEMPMAX){ //finished
fluorescentActive=false;
fluorescentCurrentBrightness=fluorescentSet; //set disired value
}
if (fluorescentCurrentBrightness<0){
fluorescentCurrentBrightness=0;
}else if (fluorescentCurrentBrightness>255){
fluorescentCurrentBrightness=255;
}
w0 = w1 = w2 = w3 = fluorescentCurrentBrightness;
output();
}
w0 = w1 = w2 = w3 = _b;
output();
}
bool sensorValue = debouncer.read();
if (Homie.isConfigured() && Homie.isConnected() && sensorValue != lastSensorValue) {