Projekt P03 - Uhrzeit + Temperatur + Humidity
Intuitive Farbskala: Rot (zu niedrig) → Gelb → Grün (optimal) → Gelb → Rot (zu hoch)
🔴🟠🟡🟢🟢🟢🟢🟡🟠🔴
15° 17° 19° 21° 23° 25° 27° 29° (je 1.5°C/LED)
🔴🟠🟡🟢🟢🟢🟢🟡🟠🔴
30% 34% 38% 42% 50% 54% 58% 62% (je 4%/LED)
#include <TM1637Display.h>
#include <RTClib.h>
#include <DHT.h>
#include <FastLED.h>
// TM1637 Display
const int PIN_CLK = 2;
const int PIN_DIO = 3;
TM1637Display display(PIN_CLK, PIN_DIO);
// NeoPixel Streifen
const int NUM_LEDS = 10;
CRGB ledsTemp[NUM_LEDS]; // Pin 5
CRGB ledsHumid[NUM_LEDS]; // Pin 6
// Sensoren
DHT dht(4, DHT22);
RTC_DS1307 rtc;// Farbe basierend auf Position (0-9) in der Skala
CRGB getScaleColor(int position) {
// Mitte (4,5) = Grün, Ränder (0,9) = Rot
int distFromCenter = abs(position - 4);
switch (distFromCenter) {
case 0: return CRGB::Green; // Optimal
case 1: return CRGB::GreenYellow;
case 2: return CRGB::Yellow;
case 3: return CRGB::Orange;
default: return CRGB::Red; // Zu extrem
}
}
// Wert auf Position (0-9) mappen
int mapToScale(float value, float min, float max) {
int pos = map(value, min, max, 0, 9);
return constrain(pos, 0, 9);
}void updateTempLeds(float temp) {
int activePos = mapToScale(temp, 15.0, 30.0);
for (int i = 0; i < NUM_LEDS; i++) {
if (i <= activePos) {
ledsTemp[i] = getScaleColor(i);
} else {
ledsTemp[i] = CRGB::Black;
}
}
FastLED.show();
}
void updateHumidLeds(float humid) {
int activePos = mapToScale(humid, 30.0, 70.0);
for (int i = 0; i < NUM_LEDS; i++) {
if (i <= activePos) {
ledsHumid[i] = getScaleColor(i);
} else {
ledsHumid[i] = CRGB::Black;
}
}
FastLED.show();
}void loop() {
// Uhrzeit auf TM1637 anzeigen
DateTime now = rtc.now();
int displayTime = now.hour() * 100 + now.minute();
display.showNumberDecEx(displayTime, 0b01000000,
true); // Doppelpunkt an
// Alle 2 Sekunden Sensor lesen
static unsigned long lastRead = 0;
if (millis() - lastRead > 2000) {
lastRead = millis();
float temp = dht.readTemperature();
float humid = dht.readHumidity();
if (!isnan(temp) && !isnan(humid)) {
updateTempLeds(temp);
updateHumidLeds(humid);
}
}
delay(500); // Doppelpunkt-Blinken
}showNumberDec(num, leading_zeros)
showNumberDecEx(num, dots, leading_zeros)
DHT22 im Simulator anklicken um Werte zu ändern!
Du hast eine visuelle Klimastation gebaut!
Datum-Anzeige, Min/Max-Speicherung, Alarm bei Extremwerten