From 0e18b3371baa0adaa41029310f8434c9e4b82c17 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Sun, 3 Mar 2019 08:32:20 +0000 Subject: [PATCH] Add ability for PWM controlled RGB. --- Lighting.cpp | 28 +++++++++++++++++++++------- Lighting.h | 29 ++++++++++++++++++++++++++--- monitor.ino | 32 ++++++++++++++++++++++++-------- 3 files changed, 71 insertions(+), 18 deletions(-) diff --git a/Lighting.cpp b/Lighting.cpp index 9f857c0..3a04be6 100644 --- a/Lighting.cpp +++ b/Lighting.cpp @@ -3,8 +3,7 @@ #define MAX_HEAT_INDEX 240.0 -Lighting::Lighting(int ledCount, NaturalLight* naturalLight, Weather* weather, float cloudCoverLimit) { - _ledCount = ledCount; +Lighting::Lighting(NaturalLight* naturalLight, Weather* weather, float cloudCoverLimit) { _naturalLight = naturalLight; _weather = weather; _cloudCoverLimit = cloudCoverLimit; @@ -41,6 +40,22 @@ float Lighting::getPaletteBrightness(){ return getBrightness() * 255.0; } +int Lighting::byteToAnalogue(byte input){ + float fraction = input / 255.0; + return fraction * 1024; +} + +void Lighting::outputColour(CRGB colour){ + if (_useStrip) + FastLED.showColor(colour); + + if (_usePWM){ + analogWrite(_rPin, byteToAnalogue(colour.r)); + analogWrite(_gPin, byteToAnalogue(colour.g)); + analogWrite(_bPin, byteToAnalogue(colour.b)); + } +} + void Lighting::updateRGB(int now) { byte heatIndex = getPaletteHeatIndex(now); byte brightness = getPaletteBrightness(); @@ -50,16 +65,15 @@ void Lighting::updateRGB(int now) { if (_weather->getCondition() == WeatherCondition::Thunder && millis() >= _nextLightningFlash) { int flashes = random8(2, 8); for (int i = 0; i < flashes; i++) { - FastLED.showColor(CRGB::White); + outputColour(CRGB::White); delay(random8(10, 20)); - FastLED.showColor(colour); + outputColour(colour); delay(random8(40, 80)); } _nextLightningFlash = millis() + (random8(1, 60) * 1000); } - fill_solid(_leds, _ledCount, colour); - FastLED.show(); + outputColour(colour); } _lastHeatIndex = heatIndex; _lastBrightness = brightness; @@ -69,7 +83,7 @@ void Lighting::updateWhite(){ float brightness = (_heatIndex - 0.5) / 0.5; if (brightness<0) brightness = 0; - analogWrite(_whitePin, 1024 * brightness); + analogWrite(_whitePin, PWMRANGE * brightness); } void Lighting::update(int now) { diff --git a/Lighting.h b/Lighting.h index 4851a6a..df46f0d 100644 --- a/Lighting.h +++ b/Lighting.h @@ -12,7 +12,7 @@ DEFINE_GRADIENT_PALETTE(_sunrise_p) { }; class Lighting { - int _ledCount, _whitePin; + int _ledCount, _whitePin, _rPin, _gPin, _bPin; NaturalLight* _naturalLight; Weather* _weather; float _cloudCoverLimit; @@ -22,6 +22,7 @@ class Lighting { float _heatIndex; float _brightness; byte _lastHeatIndex, _lastBrightness; + bool _useStrip, _usePWM; float _currentHeatIndex(); float getIndexMultiplier(int now, int startTime, int endTime, bool swap); @@ -29,17 +30,39 @@ class Lighting { float getPaletteHeatIndex(int time); float getBrightness(); float getPaletteBrightness(); + int byteToAnalogue(byte input); + void outputColour(CRGB colour); void updateRGB(int now); void updateWhite(); public: - Lighting(int ledCount, NaturalLight* _naturalLight, Weather* _weather, float cloudCoverLimit); + Lighting(NaturalLight* _naturalLight, Weather* _weather, float cloudCoverLimit); - template void setup() { + template void setupStrip() { + _ledCount = LED_COUNT; FastLED.addLeds(_leds, _ledCount).setCorrection(TypicalSMD5050).setTemperature(Tungsten40W); _whitePin = WHITE_PIN; pinMode(_whitePin, OUTPUT); + analogWrite(_whitePin, 0); + _useStrip = true; } + + template void setupPWM() { + _rPin = R_PIN; + _gPin = G_PIN; + _bPin = B_PIN; + _whitePin = WHITE_PIN; + pinMode(_rPin, OUTPUT); + pinMode(_gPin, OUTPUT); + pinMode(_bPin, OUTPUT); + pinMode(_whitePin, OUTPUT); + analogWrite(_rPin, 0); + analogWrite(_gPin, 0); + analogWrite(_bPin, 0); + analogWrite(_whitePin, 0); + _usePWM = true; + } + void update(int time); float getCurrentHeatIndex() { return _heatIndex; } float getCurrentBrightness() { return _brightness; } diff --git a/monitor.ino b/monitor.ino index 54fc565..eafb655 100644 --- a/monitor.ino +++ b/monitor.ino @@ -6,23 +6,32 @@ #include "NaturalLight.h" #include "Weather.h" -#define HOSTNAME "Fishtank" +#define HOSTNAME "72L_Aquarium" #define MQTT_SERVER "192.168.1.3" #define SSID "GCHQ Surveillance Van" #define PASSWORD "cocklol." -#define PH_TOPIC "/home/sensors/fishtank/ph" -#define TEMPERATURE_TOPIC "/home/sensors/fishtank/temperature" +#define BASE_TOPIC "/home/sensors/" HOSTNAME + +#define PH_TOPIC BASE_TOPIC "/ph" +#define TEMPERATURE_TOPIC BASE_TOPIC "/temperature" #define TEMPERATURE_PIN D5 +#define LED_USE_STRIP #define RGB_PIN D3 -#define WHITE_PIN D2 #define LED_COUNT 41 + +#define LED_USE_PWM +#define R_PIN D8 +#define G_PIN D7 +#define B_PIN D6 + +#define WHITE_PIN D2 #define LATITUDE "20.548103" #define LONGITUDE "96.916835" #define TIMEZONE_OFFSET 30600 // 8.5 hours in seconds -#define LIGHT_INDEX_TOPIC "/home/sensors/fishtank/lightindex" -#define BRIGHTNESS_TOPIC "/home/sensors/fishtank/brightness" +#define LIGHT_INDEX_TOPIC BASE_TOPIC "/lightindex" +#define BRIGHTNESS_TOPIC BASE_TOPIC "/brightness" #define CLOUD_COVER_LIMIT 50.0 // percent; #define NTP_POOL "uk.pool.ntp.org" @@ -40,12 +49,20 @@ Networking _networking = Networking(HOSTNAME, SSID, PASSWORD ,MQTT_SERVER); Sensors _sensors = Sensors(TEMPERATURE_PIN, TEMPERATURE_TOPIC, PH_TOPIC, &_networking, &sensors); NaturalLight _naturalLight = NaturalLight(LATITUDE, LONGITUDE, TIMEZONE_OFFSET); Weather _weather = Weather(LATITUDE, LONGITUDE); -Lighting _lighting = Lighting(LED_COUNT, &_naturalLight, &_weather, CLOUD_COVER_LIMIT); +Lighting _lighting = Lighting(&_naturalLight, &_weather, CLOUD_COVER_LIMIT); void setup() { Serial.begin(115200); +#ifdef LED_USE_STRIP + _lighting.setupStrip(); +#endif + +#ifdef LED_USE_PWM + _lighting.setupPWM(); +#endif + _networking.setup(); _sensors.setup(); @@ -54,7 +71,6 @@ void setup() { _naturalLight.update(); _weather.update(); - _lighting.setup(); } void publishFloat(char* topic, float value){