Projekt P02 - Uhr, Stoppuhr, Alarm und Timer
LEFT/RIGHT: Modus wechseln | UP/DOWN: Werte ändern | SELECT: Aktion
Klare Trennung der Funktionen, einfache Erweiterung!
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
// Betriebsmodi
enum Mode {
MODE_CLOCK, // Uhrzeit anzeigen
MODE_STOPWATCH, // Stoppuhr
MODE_ALARM, // Alarm einstellen
MODE_TIMER // Countdown Timer
};
// Pin-Definitionen
const int PIN_LED_ALARM = 2; // Rote LED
const int PIN_LED_TIMER = 3; // Gelbe LED
const int PIN_BUZZER = 10; // Piezo
Mode currentMode = MODE_CLOCK;
RTC_DS1307 rtc;// Stoppuhr-Variablen
unsigned long stopwatchStart = 0;
unsigned long stopwatchElapsed = 0;
bool stopwatchRunning = false;
void updateStopwatch() {
if (stopwatchRunning) {
stopwatchElapsed = millis() - stopwatchStart;
}
// Zeit in MM:SS.hh formatieren
int mins = stopwatchElapsed / 60000;
int secs = (stopwatchElapsed / 1000) % 60;
int hund = (stopwatchElapsed / 10) % 100;
lcd.setCursor(0, 1);
if (mins < 10) lcd.print("0");
lcd.print(mins); lcd.print(":");
if (secs < 10) lcd.print("0");
lcd.print(secs); lcd.print(".");
if (hund < 10) lcd.print("0");
lcd.print(hund);
}// Alarm-Variablen
int alarmHour = 7, alarmMinute = 0;
bool alarmEnabled = false;
// Timer-Variablen
int timerMinutes = 5;
unsigned long timerEnd = 0;
bool timerRunning = false;
void checkAlarm(DateTime now) {
if (alarmEnabled &&
now.hour() == alarmHour &&
now.minute() == alarmMinute &&
now.second() == 0) {
triggerAlarm();
}
}
void checkTimer() {
if (timerRunning && millis() >= timerEnd) {
timerRunning = false;
triggerAlarm();
}
}void handleButtons() {
Button btn = readButton();
if (btn == BTN_NONE) return;
switch (currentMode) {
case MODE_CLOCK:
if (btn == BTN_LEFT) currentMode = MODE_TIMER;
if (btn == BTN_RIGHT) currentMode = MODE_STOPWATCH;
if (btn == BTN_SELECT) enterSetTime();
break;
case MODE_STOPWATCH:
if (btn == BTN_SELECT) toggleStopwatch();
if (btn == BTN_DOWN) resetStopwatch();
if (btn == BTN_RIGHT) currentMode = MODE_ALARM;
break;
case MODE_ALARM:
if (btn == BTN_UP) alarmMinute++;
if (btn == BTN_DOWN) alarmMinute--;
if (btn == BTN_SELECT) alarmEnabled = !alarmEnabled;
break;
// ... Timer ähnlich
}
}digitalWrite(PIN_LED_ALARM, alarmEnabled);
digitalWrite(PIN_LED_TIMER, timerRunning);
CLOCK ↔ STOPWATCH ↔ ALARM ↔ TIMER ↔ CLOCK
Du hast eine multifunktionale Uhr gebaut!
P03: Klimastation mit TM1637 und NeoPixels