Multiclock

Projekt P02 - Uhr, Stoppuhr, Alarm und Timer

Plus
DS1307 RTC
LCD Keypad
State Machine
1

Projektübersicht

4 Modi in einem Gerät

  • Uhr - Echtzeit mit DS1307, einstellbar
  • Stoppuhr - Start/Stop/Reset
  • Alarm - Weckzeit einstellen, rote LED aktiv
  • Timer - Countdown, gelbe LED aktiv

Navigation

LEFT/RIGHT: Modus wechseln | UP/DOWN: Werte ändern | SELECT: Aktion

2

Komponenten

Hardware

  • Arduino Uno - Hauptprozessor
  • LCD Keypad Shield - Display + 5 Tasten
  • DS1307 RTC - Echtzeituhr mit Batterie
  • Rote LED - Alarm aktiv Anzeige
  • Gelbe LED - Timer aktiv Anzeige
  • Piezo Buzzer - Akustischer Alarm
3

State Machine

Zustandsbasierte Programmierung

  • MODE_CLOCK - Zeitanzeige, Einstellmodus
  • MODE_STOPWATCH - Stoppuhr-Logik
  • MODE_ALARM - Alarmzeit, An/Aus
  • MODE_TIMER - Countdown-Funktion

Vorteil

Klare Trennung der Funktionen, einfache Erweiterung!

Code - Struktur & Modi

#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;

Code - Stoppuhr

// 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);
}

Code - Alarm & Timer

// 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();
    }
}

Code - Tastensteuerung

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
    }
}
4

LED-Anzeigen

Visuelle Status-Anzeige

  • Rote LED - Leuchtet wenn Alarm aktiviert
  • Gelbe LED - Leuchtet während Timer läuft
  • Buzzer - Signalton bei Alarm/Timer-Ende

Im Code

digitalWrite(PIN_LED_ALARM, alarmEnabled);
digitalWrite(PIN_LED_TIMER, timerRunning);

5

Bedienung

Tasten-Funktionen

  • LEFT/RIGHT - Zwischen Modi wechseln
  • UP/DOWN - Werte erhöhen/verringern
  • SELECT - Aktion ausführen (Start/Stop/Ein/Aus)

Modi-Reihenfolge

CLOCK ↔ STOPWATCH ↔ ALARM ↔ TIMER ↔ CLOCK

Geschafft!

Du hast eine multifunktionale Uhr gebaut!

Nächstes Projekt

P03: Klimastation mit TM1637 und NeoPixels

← →
1 / 11