Help With RGB LED Color Based on Temperature

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);
  }

  
  }
  else (f >= 80);

One too many characters in that line

UKHeliBob:

  else (f >= 80);

One too many characters in that line

Close, but no cigar :wink:

(I make it ten)

TheMemberFormerlyKnownAsAWOL: No semicolon after the else Close, but no cigar ;)

Luckily I don't smoke :)

If I remove the semi-colon it does not verify or compile

What if you remove all ten characters . . ?

If I remove this bit (f>=80); wouldn't it turn red when the above if statement is not true? Low or high temp.

If that's the case, how can I add the blue color?

else (f >= 80);Doesn’t make any sense.
However
else if (f >= 80) may start to make sense.

TheMemberFormerlyKnownAsAWOL:
else (f >= 80);Doesn’t make any sense.
However
else if (f >= 80) may start to make sense.

Thank you for the help, that was a dumb oversight on my part!

Any hints how to add in the blue color for temps less than 70 degrees?

EDIT: Just added an additional if statement for (f<70) and it is now working. Thanks again for the help!