From 87fc8d12b2df856eb6c91b664bcfea94cdd80503 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 18 Nov 2018 22:37:27 +0100 Subject: [PATCH] fluorescent effect first tries --- esp-deckenlicht.ino | 60 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/esp-deckenlicht.ino b/esp-deckenlicht.ino index 2d01aa4..85d5f52 100644 --- a/esp-deckenlicht.ino +++ b/esp-deckenlicht.ino @@ -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 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) {