#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
const int pin1 = 2;
const int pin2 = 3;
const int pin3 = 4;
const int pin4 = 5;
const int pin5 = 6;
const int pin6 = 7;
const int potPinA0 = A0;
const int potPinA1 = A1;
const int potPinA2 = A2;
const int potPinA3 = A3;
bool phase3Active = false;
// Define start and end times for Phase P1 and P2
int p1StartHour = 19;
int p1StartMinute = 38;
int p1EndHour = 20;
int p1EndMinute = 0;
int p2StartHour = 20;
int p2StartMinute = 0;
int p2EndHour = 22;
int p2EndMinute = 10;
// Adjustable delay times for potentiometers
int p1IrrigationEventDelaySeconds;
int p1IntervalDelayMinutes;
int p2IntervalDelayMinutes;
int p2IrrigationEventDelaySeconds;
void setup() {
Serial.begin(9600);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
Wire.begin();
rtc.begin();
if (!rtc.begin()) {
Serial.println("Konnte keine RTC finden. Überprüfe die Verbindung!");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC hat die Echtzeit verloren! Setze die Zeit erneut...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize adjustable delay times
p1IrrigationEventDelaySeconds = 60; // Default value, adjust as needed
p1IntervalDelayMinutes = 60; // Default value, adjust as needed
p2IntervalDelayMinutes = 720; // Default value, adjust as needed
p2IrrigationEventDelaySeconds = 240; // Default value, adjust as needed
}
void loop() {
DateTime now = rtc.now();
int currentHour = now.hour();
int currentMinute = now.minute();
Serial.print("Aktuelle Zeit: ");
Serial.print(currentHour);
Serial.print(":");
Serial.println(currentMinute);
// Phase P1
if (isInPhase(currentHour, currentMinute, p1StartHour, p1StartMinute, p1EndHour, p1EndMinute)) {
Serial.println("Phase P1 läuft...");
digitalWrite(pin5, HIGH);
runPhaseP1();
phase3Active = false;
}
// Phase P2
else if (isInPhase(currentHour, currentMinute, p2StartHour, p2StartMinute, p2EndHour, p2EndMinute)) {
Serial.println("Phase P2 läuft...");
digitalWrite(pin6, HIGH);
runPhaseP2();
phase3Active = false;
}
// Phase P3
else {
if (!phase3Active) {
Serial.println("Phase P3 aktiv...");
digitalWrite(pin4, HIGH);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
runPhaseP3();
phase3Active = true;
}
}
}
void runPhaseP1() {
Serial.println("Phase P1 aktiv...");
int potValueA0 = analogRead(potPinA0);
int potValueA1 = analogRead(potPinA1);
Serial.print("Potentiometer A0 value: ");
Serial.println(potValueA0);
Serial.print("Potentiometer A1 value: ");
Serial.println(potValueA1);
int delayHighA0 = map(potValueA0, 0, 1023, 0, p1IrrigationEventDelaySeconds); // Mapping to seconds
int delayLowA1 = map(potValueA1, 0, 1023, 0, p1IntervalDelayMinutes); // Mapping to minutes
digitalWrite(pin4, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin1, HIGH);
Serial.println("Pin 1 eingeschaltet um " + getCurrentTime());
Serial.print("P1 Irrigation Event (seconds): ");
Serial.println(delayHighA0);
Serial.print("P1 Interval (minutes): ");
Serial.println(delayLowA1);
delay(delayHighA0 * 1000);
digitalWrite(pin1, LOW);
Serial.println("Pin 1 ausgeschaltet um " + getCurrentTime());
if (potValueA1 > 0) {
delay(delayLowA1 * 60000);
}
}
void runPhaseP2() {
Serial.println("Phase P2 aktiv...");
int potValueA2 = analogRead(potPinA2);
int potValueA3 = analogRead(potPinA3);
Serial.print("Potentiometer A2 value: ");
Serial.println(potValueA2);
Serial.print("Potentiometer A3 value: ");
Serial.println(potValueA3);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
if (potValueA3 > 0) {
int lower50PercentThreshold = 512; // 50% of 1023
int lower50PercentMappedValue = map(potValueA3, 0, lower50PercentThreshold, 0, p2IntervalDelayMinutes / 5);
int upper50PercentMappedValue = map(potValueA3, lower50PercentThreshold, 1023, p2IntervalDelayMinutes / 5, p2IntervalDelayMinutes);
int finalDelayA3 = (potValueA3 <= lower50PercentThreshold) ? lower50PercentMappedValue : upper50PercentMappedValue;
Serial.print("P2 Interval (minutes): ");
Serial.println(finalDelayA3);
delay(finalDelayA3 * 60000);
}
digitalWrite(pin1, HIGH);
Serial.println("Pin 1 eingeschaltet um " + getCurrentTime());
if (potValueA2 > 0) {
int delayHighA2 = map(potValueA2, 0, 1023, 0, p2IrrigationEventDelaySeconds); // Mapping to seconds
Serial.print("P2 Irrigation Event (seconds): ");
Serial.println(delayHighA2);
delay(delayHighA2 * 60000);
}
digitalWrite(pin1, LOW);
Serial.println("Pin 1 ausgeschaltet um " + getCurrentTime());
}
void runPhaseP3() {
digitalWrite(pin4, HIGH);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
}
String getCurrentTime() {
DateTime now = rtc.now();
return String(now.hour()) + ":" + String(now.minute());
}
bool isInPhase(int currentHour, int currentMinute, int startHour, int startMinute, int endHour, int endMinute) {
return (currentHour > startHour || (currentHour == startHour && currentMinute >= startMinute)) &&
(currentHour < endHour || (currentHour == endHour && currentMinute <= endMinute));
}