From 9662f03cfa7b3fbb1b7ce30d52b6493e2d4fcaba Mon Sep 17 00:00:00 2001 From: andysheen Date: Fri, 9 Oct 2020 11:12:10 +0100 Subject: [PATCH] added functionality so that the user led fades over time --- ESP32-SOCKETIO.ino | 9 ++++----- rgbled.ino | 44 ++++++++++++++++++++++++-------------------- socketIO.ino | 2 +- utility.ino | 1 + 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/ESP32-SOCKETIO.ino b/ESP32-SOCKETIO.ino index c5974c2..b96e9e4 100644 --- a/ESP32-SOCKETIO.ino +++ b/ESP32-SOCKETIO.ino @@ -74,17 +74,16 @@ using namespace ace_button; #define FASTLONGFADE 120 unsigned long LONGFADEMINUTESMAX = 360; #define LONGFADECHECKMILLIS 60000 -unsigned long prevLongFadeVal = 0; +unsigned long prevLongFadeVal[NUMPIXELS] = {0,0}; uint8_t hue[NUMPIXELS]; uint8_t saturation[NUMPIXELS]; uint8_t value[NUMPIXELS]; -uint8_t prevRemoteSat; bool ledChanged[NUMPIXELS] = {false, false}; unsigned long prevPixelMillis; -bool isLongFade = false; +bool isLongFade[NUMPIXELS]= {false,false}; unsigned long prevlongPixelMillis; -unsigned long longFadeMinutes; -unsigned long prevLongFadeMillis; +unsigned long longFadeMinutes[NUMPIXELS]; +unsigned long prevLongFadeMillis[NUMPIXELS]; bool isRemoteLedFading = false; CRGB leds[NUMPIXELS]; bool readyToFadeRGB[NUMPIXELS] = {false, false}; diff --git a/rgbled.ino b/rgbled.ino index 8f234a3..a72fb44 100644 --- a/rgbled.ino +++ b/rgbled.ino @@ -26,7 +26,6 @@ void rgbLedHandler() { if (ledChanged[USERLED]) { ledChanged[USERLED] = false; saturation[USERLED] = 255; - value[USERLED] = 255; leds[USERLED] = CHSV(hue[USERLED], saturation[USERLED], value[USERLED]); FastLED.show(); } @@ -37,7 +36,7 @@ void rgbLedHandler() { leds[REMOTELED] = CHSV(hue[REMOTELED], saturation[REMOTELED], value[REMOTELED]); FastLED.show(); } - //long fade on remote led + //long fade longFadeHandler(); } //updating every 5 seconds to make sure the leds dont lose their colours @@ -50,6 +49,7 @@ void rgbLedHandler() { } void cycleHue(int led) { + value[led] = 255; if (hue[led] < 255) { hue[led]++; } else { @@ -101,27 +101,31 @@ void fadeRGBHandler() { } } -void startLongFade() { - isLongFade = true; - longFadeMinutes = LONGFADEMINUTESMAX; - prevLongFadeVal = 0; +void startLongFade(byte LED) { + isLongFade[LED] = true; + longFadeMinutes[LED] = LONGFADEMINUTESMAX; + prevLongFadeVal[LED] = 0; } void longFadeHandler() { - if (isLongFade) { - if (millis() - prevLongFadeMillis > LONGFADECHECKMILLIS) { - prevLongFadeMillis = millis(); - longFadeMinutes--; - unsigned long currLongFadeVal = long((float)longFadeMinutes / ((float)LONGFADEMINUTESMAX / (float)RGBLEDPWMSTART)); - if (currLongFadeVal != prevLongFadeVal) { - prevLongFadeVal = currLongFadeVal; - currLongFadeVal = currLongFadeVal - 1; - value[REMOTELED] = (byte)fscale(0, RGBLEDPWMSTART, 0, RGBLEDPWMSTART, currLongFadeVal, -3); - ledChanged[REMOTELED] = true; - Serial.println(value[REMOTELED]); - } - if (longFadeMinutes <= 0) { - isLongFade = false; + for (byte i = 0; i < NUMPIXELS; i++) { + if (isLongFade[i]) { + if (millis() - prevLongFadeMillis[i] > LONGFADECHECKMILLIS) { + prevLongFadeMillis[i] = millis(); + longFadeMinutes[i]--; + unsigned long currLongFadeVal = long((float)longFadeMinutes[i] / ((float)LONGFADEMINUTESMAX / (float)RGBLEDPWMSTART)); + if (currLongFadeVal != prevLongFadeVal[i]) { + prevLongFadeVal[i] = currLongFadeVal; + currLongFadeVal = currLongFadeVal - 1; + value[i] = (byte)fscale(0, RGBLEDPWMSTART, 0, RGBLEDPWMSTART, currLongFadeVal, -3); + ledChanged[i] = true; + Serial.print("LED:"); + Serial.println(i); + Serial.println(value[i]); + } + if (longFadeMinutes[i] <= 0) { + isLongFade[i] = false; + } } } } diff --git a/socketIO.ino b/socketIO.ino index 43dca74..f67900c 100644 --- a/socketIO.ino +++ b/socketIO.ino @@ -43,7 +43,7 @@ void socketIO_msg(const char * payload, size_t length) { //added to enable reset of fading mid fade isFadingRGB[REMOTELED] = false; fadeRGB(REMOTELED); - startLongFade(); + startLongFade(REMOTELED); } else if (String(data_project) == "test") { blinkDevice(); diff --git a/utility.ino b/utility.ino index 787b60f..925fc21 100644 --- a/utility.ino +++ b/utility.ino @@ -125,6 +125,7 @@ void handleTouchEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) } else { ledChanged[USERLED] = true; fadeRGB(USERLED); + startLongFade(USERLED); socketIO_sendColour(); } isSelectingColour = false;