Implemented linear interpolation

This commit is contained in:
Nik 2018-06-27 16:57:18 +02:00
parent f1389eda5d
commit dbd5706481
1 changed files with 19 additions and 32 deletions

View File

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