Hello Everyone,
I have a simple project to change an LED's color based on temperature.
I'd like to see the following:
Red - Above 80
Green - Between 70 and 80
Blue - Below 70
I've included code for Red and Green, it doesn't work though as it's always Red. I am stuck why it doesn't work and how to add the third color in. Sensor is working and temperatures output to serial monitor fine.
#include "DHT.h"
#define DHTPIN 2
#define GREEN 5
#define RED 6
#define BLUE 3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("DHT11 test!");
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
dht.begin();
}
float redValue;
float greenValue;
float blueValue;
void loop() {
// put your main code here, to run repeatedly:
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.println(" *F");
if ((f>=70) && (f<=80))
{
greenValue = 255;
redValue = 0;
blueValue = 0;
analogWrite(GREEN, greenValue);
analogWrite(RED, redValue);
analogWrite(BLUE, blueValue);
}
else (f>=80);
{
greenValue = 0;
redValue = 255;
blueValue = 0;
analogWrite(GREEN, greenValue);
analogWrite(RED, redValue);
analogWrite(BLUE, blueValue);
}
}