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 LED Fading mit NodeMCU v 1.0
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
*/ */
class RgbLed { class RgbLed {
@ -24,7 +16,7 @@ class RgbLed {
byte ledBlueIntensityTo; byte ledBlueIntensityTo;
long durationLeft; long durationLeft;
long currentMillis; long previousMillis;
public: public:
RgbLed(int redPin, int greenPin, int bluePin) RgbLed(int redPin, int greenPin, int bluePin)
@ -46,7 +38,7 @@ class RgbLed {
ledBlueIntensityTo = ledBlueIntensity; ledBlueIntensityTo = ledBlueIntensity;
durationLeft = 0; durationLeft = 0;
currentMillis = 0; previousMillis = 0;
} }
public: public:
@ -59,25 +51,29 @@ class RgbLed {
ledGreenIntensityTo = ledGreenIntensity; ledGreenIntensityTo = ledGreenIntensity;
ledBlueIntensityTo = ledBlueIntensity; ledBlueIntensityTo = ledBlueIntensity;
} }
} }
public: public:
void Update(long currentMillis) void Update(long currentMillis)
{ {
// if(currentMillis - this->currentMillis >= interval) { if (ledRedIntensityTo != ledRedIntensity && ledGreenIntensityTo != ledGreenIntensity && ledBlueIntensityTo != ledBlueIntensity) {
// this->currentMillis = currentMillis; ledRedIntensity = lerp(ledRedIntensity, ledRedIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis);
// digitalWrite(led, intensity); ledGreenIntensity = lerp(ledGreenIntensity, ledGreenIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis);
// } ledBlueIntensity = lerp(ledBlueIntensity, ledBlueIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis);
// if (ledRedIntensityTo != ledRedIntensity && ledGreenIntensityTo != ledGreenIntensity && ledBlueIntensityTo != ledBlueIntensity) { UpdateLEDs();
// ledRedIntensity = ; }
// ledGreenIntensity = ; durationLeft = durationLeft - (currentMillis - previousMillis);
// ledBlueIntensity = ; previousMillis = currentMillis;
// }
} }
byte lerp(byte from, byte to, long t) { byte lerp(byte intensityStart, byte intensityEnd, long timeStart, long timeEnd, long timeCurrent) {
return from + t * (to - from); 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.ChangeColor(30, 200, 255, 1000);
ledStripe.Update(millis()); 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);
// }
//}