From 1c6a176628ece61e69aa057b2dcedeb6afd8585f Mon Sep 17 00:00:00 2001 From: Wojciech Burda Date: Tue, 13 Dec 2022 23:00:47 +0100 Subject: [PATCH] Initial commit --- rgb-led-temp-indicator.ino | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 rgb-led-temp-indicator.ino diff --git a/rgb-led-temp-indicator.ino b/rgb-led-temp-indicator.ino new file mode 100644 index 0000000..fe0a289 --- /dev/null +++ b/rgb-led-temp-indicator.ino @@ -0,0 +1,64 @@ +#include +#include + +#define ONE_WIRE_BUS 2 + +OneWire oneWire(ONE_WIRE_BUS); +DallasTemperature sensors(&oneWire); + +int red_light_pin = 5; +int green_light_pin = 4; +int blue_light_pin = 0; + +unsigned long previousMillis = 0; +const long interval = 60000; + +void setup(void) { + Serial.begin(9600); + sensors.begin(); + pinMode(red_light_pin, OUTPUT); + pinMode(green_light_pin, OUTPUT); + pinMode(blue_light_pin, OUTPUT); + showTempWithLED(meassureTemp()); +} + +void loop(void) { + unsigned long currentMillis = millis(); + + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + showTempWithLED(meassureTemp()); + } +} + +float meassureTemp() { + sensors.requestTemperatures(); + float tempC = sensors.getTempCByIndex(0); + + if (tempC != DEVICE_DISCONNECTED_C) { + Serial.print("Temperature: "); + Serial.println(tempC); + } else { + Serial.println("Error: Could not read temperature data"); + } + return tempC; +} + +// Had to invert colors because of LED type +void showTempWithLED(float tempC) { + if (tempC <= 21) { + RGB_color(255, 255, 0); + } + else if(tempC >= 24) { + RGB_color(0, 255, 255); + } + else { + RGB_color(255, 0, 255); + } +} + +void RGB_color(int red_light_value, int green_light_value, int blue_light_value) { + analogWrite(red_light_pin, red_light_value); + analogWrite(green_light_pin, green_light_value); + analogWrite(blue_light_pin, blue_light_value); +}