#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// I2C LCD Adresse (häufig 0x27 oder 0x3F, überprüfe die Adresse deines Moduls)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Servo Motor Pin
const int servoPin = 3;
Servo myservo;
// Button Pins
const int blueButton = 7;
const int yellowButton = 8;
const int greenButton = 2;
const int redButton = 4;
// Zeitvariablen
int hours = 0;
int minutes = 0;
int seconds = 0;
// Statusvariablen
bool isCountingDown = false;
bool isServoMoving = false;
// Setup-Funktion
void setup() {
lcd.init();
lcd.backlight();
updateDisplay();
pinMode(blueButton, INPUT_PULLUP);
pinMode(yellowButton, INPUT_PULLUP);
pinMode(greenButton, INPUT_PULLUP);
pinMode(redButton, INPUT_PULLUP);
myservo.attach(servoPin);
myservo.write(0); // Anfangsposition des Servos
}
// Hauptschleife
void loop() {
if (!isCountingDown && !isServoMoving) {
handleButtonPress();
} else if (isCountingDown && !isServoMoving) {
if (digitalRead(redButton) == LOW) {
isCountingDown = false;
isServoMoving = true;
myservo.write(90);
delay(500); // Warten, bis der Servo sich bewegt hat
showEndMessageAndResetTime();
}
}
}
// Button-Eingaben verarbeiten
void handleButtonPress() {
if (digitalRead(blueButton) == LOW) {
adjustTime(-30);
updateDisplay();
delay(200);
}
if (digitalRead(yellowButton) == LOW) {
adjustTime(30);
updateDisplay();
delay(200);
}
if (digitalRead(greenButton) == LOW) {
isCountingDown = true;
countdown();
}
}
// Zeit anpassen (in Minuten)
void adjustTime(int minutesToAdd) {
minutes += minutesToAdd;
while (minutes >= 60) {
minutes -= 60;
hours++;
}
while (minutes < 0) {
minutes += 60;
hours = max(0, hours - 1);
}
}
// Display aktualisieren
void updateDisplay() {
lcd.clear();
lcd.print("Time: ");
lcd.print(formatTime(hours) + ":" + formatTime(minutes) + ":" + formatTime(seconds));
}
// Zeitformatierung (hh:mm:ss)
String formatTime(int timeUnit) {
return (timeUnit < 10) ? "0" + String(timeUnit) : String(timeUnit);
}
// Countdown-Funktion
void countdown() {
while ((hours > 0 || minutes > 0 || seconds > 0) && isCountingDown) {
delay(1000);
decreaseSeconds();
updateDisplay();
if (digitalRead(redButton) == LOW) {
isCountingDown = false;
isServoMoving = true;
myservo.write(90);
delay(500); // Warten, bis der Servo sich bewegt hat
showEndMessageAndResetTime();
return;
}
}
if (hours == 0 && minutes == 0 && seconds == 0) {
isServoMoving = true;
myservo.write(90); // Servo bewegen, wenn die Zeit abgelaufen ist
delay(500); // Warten, bis der Servo sich bewegt hat
showEndMessageAndResetTime();
}
}
// Sekunden verringern
void decreaseSeconds() {
if (seconds > 0) {
seconds--;
} else {
if (minutes > 0) {
minutes--;
seconds = 59;
} else if (hours > 0) {
hours--;
minutes = 59;
seconds = 59;
}
}
}
// Endmeldung anzeigen und Zeit zurücksetzen
void showEndMessageAndResetTime() {
lcd.clear();
lcd.print("Time is up!");
lcd.setCursor(0, 1);
lcd.print("Press red btn");
while (digitalRead(redButton) != LOW) {
delay(100);
}
// Zeit zurücksetzen
hours = 0;
minutes = 0;
seconds = 0;
myservo.write(0); // Servo in die Ausgangsposition bewegen
delay(500); // Warten, bis der Servo sich bewegt hat
isServoMoving = false;
isCountingDown = false;
updateDisplay();
}