From 11c37b718048a5b0f0b194c7953ed1f169ad140f Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Wed, 28 Nov 2018 17:10:42 +0000 Subject: [PATCH] Add support for PWM controlled white LED strip --- Lighting.cpp | 31 ++++++++++++++++++++++++------- Lighting.h | 10 ++++++++-- monitor.ino | 7 ++++--- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Lighting.cpp b/Lighting.cpp index 6047e9a..4c9471c 100644 --- a/Lighting.cpp +++ b/Lighting.cpp @@ -20,10 +20,13 @@ float Lighting::getIndexMultiplier(int now, int startTime, int endTime, bool swa } float Lighting::getHeatIndex(int now) { - float indexMultiplier = now >= _naturalLight->getSunset() + return now >= _naturalLight->getSunset() ? getIndexMultiplier(now, _naturalLight->getSunset(), _naturalLight->getAstronomicalTwilightEnd(), true) : getIndexMultiplier(now, _naturalLight->getAstronomicalTwilightBegin(), _naturalLight->getSunrise(), false); - return MAX_HEAT_INDEX * indexMultiplier; +} + +float Lighting::getPaletteHeatIndex(int now){ + return MAX_HEAT_INDEX * getHeatIndex(now); } float Lighting::getBrightness() { @@ -31,14 +34,15 @@ float Lighting::getBrightness() { float multiplier = cloudCover * (_cloudCoverLimit / 100.0); if (_weather->getCondition() == WeatherCondition::Other) multiplier *= 2.0; - return 255.0 - (multiplier * 255.0); + return 1 - multiplier; } -void Lighting::update(int now) { - _heatIndex = getHeatIndex(now); - _brightness = getBrightness(); +float Lighting::getPaletteBrightness(){ + return getBrightness() * 255.0; +} - CRGB colour = ColorFromPalette(_sunrise, _heatIndex, _brightness, LINEARBLEND); +void Lighting::updateRGB(int now) { + CRGB colour = ColorFromPalette(_sunrise, getPaletteHeatIndex(now), getPaletteBrightness(), LINEARBLEND); if (_weather->getCondition() == WeatherCondition::Thunder && millis() >= _nextLightningFlash) { int flashes = random8(2, 8); @@ -53,4 +57,17 @@ void Lighting::update(int now) { fill_solid(_leds, _ledCount, colour); FastLED.show(); +} + +void Lighting::updateWhite(){ + float brightness = (_heatIndex - 0.5) / 0.5; + analogWrite(_whitePin, 1024 * brightness); +} + +void Lighting::update(int now) { + _heatIndex = getHeatIndex(now); + _brightness = getBrightness(); + + updateRGB(now); + updateWhite(); } \ No newline at end of file diff --git a/Lighting.h b/Lighting.h index 5d911f8..9818948 100644 --- a/Lighting.h +++ b/Lighting.h @@ -12,7 +12,7 @@ DEFINE_GRADIENT_PALETTE(_sunrise_p) { }; class Lighting { - int _ledCount; + int _ledCount, _whitePin; NaturalLight* _naturalLight; Weather* _weather; float _cloudCoverLimit; @@ -25,13 +25,19 @@ class Lighting { float _currentHeatIndex(); float getIndexMultiplier(int now, int startTime, int endTime, bool swap); float getHeatIndex(int time); + float getPaletteHeatIndex(int time); float getBrightness(); + float getPaletteBrightness(); + void updateRGB(int now); + void updateWhite(); public: Lighting(int ledCount, NaturalLight* _naturalLight, Weather* _weather, float cloudCoverLimit); - template void setup() { + template void setup() { FastLED.addLeds(_leds, _ledCount).setCorrection(TypicalSMD5050).setTemperature(Tungsten40W); + _whitePin = WHITE_PIN; + pinMode(_whitePin, OUTPUT); } void update(int time); float getCurrentHeatIndex() { return _heatIndex; } diff --git a/monitor.ino b/monitor.ino index 6495568..163fa4a 100644 --- a/monitor.ino +++ b/monitor.ino @@ -16,8 +16,9 @@ #define TEMPERATURE_TOPIC "/home/sensors/fishtank/temperature" #define TEMPERATURE_PIN D5 -#define LED_PIN D2 -#define LED_COUNT 36 +#define RGB_PIN D3 +#define WHITE_PIN D2 +#define LED_COUNT 39 #define LATITUDE "20.548103" #define LONGITUDE "96.916835" #define TIMEZONE_OFFSET 27000 // 7.5 hours in seconds @@ -45,7 +46,7 @@ void setup() { _naturalLight.update(); _weather.update(); - _lighting.setup(); + _lighting.setup(); } void publishFloat(char* topic, float value){