diff --git a/LEDWolke.ino b/LEDWolke.ino index 25a45f1..b003f5f 100644 --- a/LEDWolke.ino +++ b/LEDWolke.ino @@ -1,13 +1,5 @@ /* - ESP8266 BlinkWithoutDelay by Simon Peter - Blink the blue LED on the ESP-01 module - Based on the Arduino Blink without Delay example - This example code is in the public domain - - The blue LED on the ESP-01 module is connected to GPIO1 - (which is also the TXD pin; so we cannot use Serial.print() at the same time) - - Note that this sketch uses LED_BUILTIN to find the pin with the internal LED + LED Fading mit NodeMCU v 1.0 */ class RgbLed { @@ -24,7 +16,7 @@ class RgbLed { byte ledBlueIntensityTo; long durationLeft; - long currentMillis; + long previousMillis; public: RgbLed(int redPin, int greenPin, int bluePin) @@ -46,7 +38,7 @@ class RgbLed { ledBlueIntensityTo = ledBlueIntensity; durationLeft = 0; - currentMillis = 0; + previousMillis = 0; } public: @@ -59,25 +51,29 @@ class RgbLed { ledGreenIntensityTo = ledGreenIntensity; ledBlueIntensityTo = ledBlueIntensity; } - } public: void Update(long currentMillis) { - // if(currentMillis - this->currentMillis >= interval) { - // this->currentMillis = currentMillis; - // digitalWrite(led, intensity); - // } - // if (ledRedIntensityTo != ledRedIntensity && ledGreenIntensityTo != ledGreenIntensity && ledBlueIntensityTo != ledBlueIntensity) { - // ledRedIntensity = ; - // ledGreenIntensity = ; - // ledBlueIntensity = ; - // } + if (ledRedIntensityTo != ledRedIntensity && ledGreenIntensityTo != ledGreenIntensity && ledBlueIntensityTo != ledBlueIntensity) { + ledRedIntensity = lerp(ledRedIntensity, ledRedIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis); + ledGreenIntensity = lerp(ledGreenIntensity, ledGreenIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis); + ledBlueIntensity = lerp(ledBlueIntensity, ledBlueIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis); + UpdateLEDs(); + } + durationLeft = durationLeft - (currentMillis - previousMillis); + previousMillis = currentMillis; } - byte lerp(byte from, byte to, long t) { - return from + t * (to - from); + byte lerp(byte intensityStart, byte intensityEnd, long timeStart, long timeEnd, long timeCurrent) { + return ( ( timeCurrent - timeStart )( intensityEnd - intensityStart) / ( timeEnd - timeStart) ) + intensityStart; + } + + void UpdateLEDs() { + digitalWrite(ledRedPin, ledRedIntensity); + digitalWrite(ledGreenPin, ledGreenIntensity); + digitalWrite(ledBluePin, ledBlueIntensity); } }; @@ -92,12 +88,3 @@ void loop() ledStripe.ChangeColor(30, 200, 255, 1000); ledStripe.Update(millis()); } - - -//void led_blink(int led, byte intensity, long interval) { -// unsigned long currentMillis = millis(); -// if(currentMillis - previousMillis >= interval) { -// previousMillis = currentMillis; -// digitalWrite(led, intensity); -// } -//}