Sprache auswählen

© Borgmann Aquaponik & Hydroponik
Alle Rechte Vorbehalten
https://borgmann-aquaponik-hydroponik.ch

Viel Erfolg wünschen wir Ihnen!

Aus dem Artikel zu: Lichtsteuerung im smarten Gewächshaus: Energieeffizienz und Wachstum


// Vereinfachtes Arduino/ESP32 Code-Beispiel für Sonnenaufgang/Sonnenuntergang
const int warmWhitePin = 13; // PWM-Pin für Warmweiß-LED
const int redLedPin = 12;    // PWM-Pin für Rot-LED
const int blueLedPin = 14;   // PWM-Pin für Blau-LED

const int sunriseDuration = 30 * 60 * 1000; // 30 Minuten (ms)
const int sunsetDuration = 30 * 60 * 1000;  // 30 Minuten (ms)
const int dayDuration = 12 * 60 * 60 * 1000; // 12 Stunden Taglicht (ms)

unsigned long currentTime;
unsigned long dayStartTime = 0; // Speichert den Start des aktuellen Tageszyklus

void setup() {
  // Für ESP32 müssen PWM-Frequenz und Auflösung konfiguriert werden
  ledcSetup(0, 5000, 8); // Kanal 0, Frequenz 5kHz, 8 Bit Auflösung (0-255)
  ledcAttachPin(warmWhitePin, 0);
  ledcSetup(1, 5000, 8); // Kanal 1
  ledcAttachPin(redLedPin, 1);
  ledcSetup(2, 5000, 8); // Kanal 2
  ledcAttachPin(blueLedPin, 2);

  Serial.begin(115200);
  Serial.println("Starte Lichtzyklus.");
}

void loop() {
  currentTime = millis();

  // Reset des Tageszyklus alle 24 Stunden (oder basierend auf Echtzeituhr)
  if (currentTime - dayStartTime >= (24 * 60 * 60 * 1000)) { // 24 Stunden
     dayStartTime = currentTime;
     Serial.println("Neuer Tageszyklus beginnt.");
  }

  long timeInDay = (currentTime - dayStartTime) % (24 * 60 * 60 * 1000); // Zeit seit Tagesstart

  int warmWhiteIntensity = 0;
  int redIntensity = 0;
  int blueIntensity = 0;

  // Sonnenaufgangs-Phase
  if (timeInDay < sunriseDuration) {
    float progress = (float)timeInDay / sunriseDuration;
    warmWhiteIntensity = (int)(progress * 255); // Max 255
    redIntensity = (int)(progress * 200);      // Etwas weniger Rot
  }
  // Taglicht-Phase (inkl. Blaulicht)
  else if (timeInDay >= sunriseDuration && timeInDay < (sunriseDuration + dayDuration)) {
    warmWhiteIntensity = 255;
    redIntensity = 200;
    blueIntensity = 150; // Blau hinzu
  }
  // Sonnenuntergangs-Phase
  else if (timeInDay >= (sunriseDuration + dayDuration) && timeInDay < (sunriseDuration + dayDuration + sunsetDuration)) {
    float progress = (float)(timeInDay - (sunriseDuration + dayDuration)) / sunsetDuration;
    warmWhiteIntensity = (int)((1.0 - progress) * 255);
    redIntensity = (int)((1.0 - progress) * 200);
    blueIntensity = 0; // Blau zuerst weg
  }
  // Nacht-Phase
  else {
    warmWhiteIntensity = 0;
    redIntensity = 0;
    blueIntensity = 0;
  }

  ledcWrite(0, warmWhiteIntensity);
  ledcWrite(1, redIntensity);
  ledcWrite(2, blueIntensity);

  delay(1000); // Aktualisierungsrate
}