#include <DHT.h>
#define DHTPIN 15
#define DHTTYPE DHT22
#define TIMEDHT 1000
const int ledCount = 10;
int ledPins[] = {
2, 4, 5, 18, 19, 21, 22, 23, 13, 12
};
DHT dht(DHTPIN, DHTTYPE);
unsigned long timerDHT = 0;
float humidity = 0.0;
float celsius = 0.0;
float fahrenheit = 0.0;
void setup() {
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
dht.begin();
}
void getTemperature() {
if ((millis() - timerDHT) > TIMEDHT) {
timerDHT = millis();
humidity = dht.readHumidity();
celsius = dht.readTemperature();
fahrenheit = dht.readTemperature(true);
if (isnan(humidity) || isnan(celsius) || isnan(fahrenheit)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
void loop() {
getTemperature();
int ledLevel = map(celsius, -40, 80, 0, ledCount);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
} else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}