From d46229bf4bcdaa57782d7377b75d9f2e4594de46 Mon Sep 17 00:00:00 2001 From: Nis Boerge Wechselberg Date: Fri, 20 Nov 2015 21:38:45 +0100 Subject: [PATCH] Added breathing mode and adjusted fading speed --- .../ESP8266-RGB-WiFiServer.ino | 57 ++++++++++++------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/ESP8266-RGB-WiFiServer/ESP8266-RGB-WiFiServer.ino b/ESP8266-RGB-WiFiServer/ESP8266-RGB-WiFiServer.ino index 86a8711..ba3625d 100644 --- a/ESP8266-RGB-WiFiServer/ESP8266-RGB-WiFiServer.ino +++ b/ESP8266-RGB-WiFiServer/ESP8266-RGB-WiFiServer.ino @@ -49,10 +49,13 @@ unsigned char blueValue; unsigned char decColor; // Scale factors -float globalScale = 3.0; -float redScale = 1.0; -float greenScale = 1.0; -float blueScale = 0.7; +const float globalScale = 3.0; +const float redScale = 1.0; +const float greenScale = 1.0; +const float blueScale = 0.7; +float breathScale = 0.999; +float breathAdjustment = -0.001; + // HTTP configuration const unsigned char port = 80; @@ -149,23 +152,30 @@ void loop() { if (red < 0) { red = 0; } if (green < 0) { green = 0; } if (blue < 0) { blue = 0; } - if (mode > 1) { mode = 1; } + if (mode > 3) { mode = 3; } if (red > 255) { red = 255; } if (green > 255) { green = 255; } if (blue > 255) { blue = 255; } // Update values modeValue = mode; - if (mode == 1) { - decColor = 0; - redValue = 255; - greenValue = 0; - blueValue = 0; - } else { + + + breathScale = 0.999; + breathAdjustment = -0.001; + decColor = 0; + + if (modeValue % 2 == 0) { redValue = red; greenValue = green; blueValue = blue; } + if (modeValue % 2 != 0) { + redValue = 255; + greenValue = 0; + blueValue = 0; + } + persistToEEPROM(); } } else if (req.indexOf("/get/") != -1) { @@ -202,9 +212,7 @@ void loop() { } void updateLight() { - if (modeValue == 0) { - // Normal static color mode - } else if (modeValue == 1) { + if (modeValue % 2 == 1) { // Color cylce mode unsigned short rgbColor[3]; rgbColor[0] = redValue; @@ -224,20 +232,31 @@ void updateLight() { decColor = incColor; } } + + if (modeValue > 1) { + // Breath mode + breathScale += breathAdjustment; + + if (breathScale <= 0.001 || breathScale >= 0.999) { + breathAdjustment = 0 - breathAdjustment; + } + + } + setLight(redValue, greenValue, blueValue); - delay(15); + delay(30); } /* Helper function to set the colors of the led strip */ void setLight(unsigned char red, unsigned char green, unsigned char blue) { - analogWrite(redPin, red * globalScale * redScale); - analogWrite(greenPin, green * globalScale * greenScale); - analogWrite(bluePin, blue * globalScale * blueScale); + analogWrite(redPin, red * globalScale * breathScale * redScale); + analogWrite(greenPin, green * globalScale * breathScale * greenScale); + analogWrite(bluePin, blue * globalScale * breathScale * blueScale); } void readFromEEPROM() { unsigned char newMode = EEPROM.read(modeAddress); - if (newMode > 1) { + if (newMode > 3) { // Uninitialized cells return 255, so reset that modeValue = 0; } else {