#include <TM1637Display.h>
#include "pitches.h"
// Pins for the TM1637 display
#define CLK 9
#define DIO 10
// Pins for buttons, LED, and buzzer
#define BUTTON_UP_PIN 2
#define BUTTON_DOWN_PIN 3
#define BUTTON_START_PIN 4
#define LED_PIN 11
#define MODE_SELECT_PIN 8
#define BUZZER_PIN 12
TM1637Display display(CLK, DIO);
static int countdown_time = 300;
static int initial_countdown_time = countdown_time;
// Noten und Dauer für die Melodien
const int melodies[2][6][2] = {
{ // Akte X
{NOTE_A3, 5},
{NOTE_E4, 5},
{NOTE_D4, 5},
{NOTE_E4, 5},
{NOTE_G4, 5},
{NOTE_E4, 15}
},
{ // Begegnung der 3. Art
{NOTE_D5, 4},
{NOTE_E5, 4},
{NOTE_C5, 4},
{NOTE_C4, 4},
{NOTE_G4, 6},
{NOTE_G4, 6}
}
};
void playConfirmationSound() {
tone(BUZZER_PIN, NOTE_E6);
delay(2); // Pause zwischen den Noten
noTone(BUZZER_PIN);
delay(100);
}
void playStopSound() {
tone(BUZZER_PIN, NOTE_E2);
delay(100); // Pause zwischen den Noten
noTone(BUZZER_PIN);
}
void playStartSound() {
tone(BUZZER_PIN, NOTE_G4);
delay(20); // Pause zwischen den Noten
noTone(BUZZER_PIN);
}
void playMelody() {
//int melody = (digitalRead(MODE_SELECT_PIN) == LOW) ? 0:1;
int melody = 1;
for (int i = 0; i < sizeof(melodies[melody]) / sizeof(melodies[melody][0]); i++) {
int noteDuration = 100 * melodies[melody][i][1];
tone(BUZZER_PIN, melodies[melody][i][0]);
digitalWrite(LED_PIN, HIGH); // LED blinken lassen, während die Melodie abgespielt wird
delay(noteDuration * 0.3); // Pause zwischen den Noten 1
digitalWrite(LED_PIN, LOW);
delay(noteDuration * 0.70); // Pause zwischen den Noten 2
noTone(BUZZER_PIN);
}
}
void setup() {
pinMode(LED_PIN, OUTPUT); // LED Pin setzen
// GPIO-Pins setzen
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
pinMode(BUTTON_START_PIN, INPUT_PULLUP);
pinMode(MODE_SELECT_PIN, INPUT_PULLUP);
display.setBrightness(0x0f); // Helligkeit der Anzeige setzen
display.showNumberDecEx((countdown_time / 60 * 100) + countdown_time % 60, 0b01000000, true);
Serial.begin(9600);
}
void loop() {
bool countdown_running = false;
bool countdown_paused = false;
unsigned long previousMillis = 0;
unsigned long buttonPressStart = 0;
const long interval = 1000;
// Zeit einstellen
while (!countdown_running) {
if (digitalRead(BUTTON_UP_PIN) == LOW) {
countdown_time += 60; // Erhöht die Zeit um eine Minute
initial_countdown_time = countdown_time;
Serial.print("Countdown-Zeit: ");
Serial.print(countdown_time / 60);
Serial.println(" Minuten");
int minutes = countdown_time / 60;
int seconds = countdown_time % 60;
display.showNumberDecEx((minutes * 100) + seconds, 0b01000000, true);
playConfirmationSound();
delay(200); // Entprellung
}
if (digitalRead(BUTTON_DOWN_PIN) == LOW && countdown_time >= 60) {
countdown_time -= 60; // Verringert die Zeit um eine Minute
initial_countdown_time = countdown_time;
Serial.print("Countdown-Zeit: ");
Serial.print(countdown_time / 60);
Serial.println(" Minuten");
int minutes = countdown_time / 60;
int seconds = countdown_time % 60;
display.showNumberDecEx((minutes * 100) + seconds, 0b01000000, true);
playConfirmationSound();
delay(200); // Entprellung
}
if (digitalRead(BUTTON_START_PIN) == LOW) {
buttonPressStart = millis();
while (digitalRead(BUTTON_START_PIN) == LOW) {
if (millis() - buttonPressStart > 1000) { // Wenn der Start-Button länger als 1 Sekunde gedrückt wird
countdown_time = initial_countdown_time; // Zurück zur vorher eingestellten Zeit
Serial.println("Countdown gestoppt und zurückgesetzt!");
int minutes = countdown_time / 60;
int seconds = countdown_time % 60;
display.showNumberDecEx((minutes * 100) + seconds, 0b01000000, true);
countdown_running = false;
countdown_paused = false; // Sicherstellen, dass der Countdown nicht pausiert ist, wenn er neu startet
playStopSound();
delay(200); // Entprellung
return;
}
}
Serial.println("Countdown gestartet!");
countdown_running = true;
playStartSound();
delay(500); // Entprellung
}
}
// Countdown
while (countdown_running) {
unsigned long currentMillis = millis();
if (digitalRead(BUTTON_START_PIN) == LOW) { // Prüfen, ob der Start-Button erneut gedrückt wird
buttonPressStart = millis();
while (digitalRead(BUTTON_START_PIN) == LOW) {
if (millis() - buttonPressStart > 1000) { // Wenn der Start-Button länger als 1 Sekunde gedrückt wird
countdown_time = initial_countdown_time; // Zurück zur vorher eingestellten Zeit
Serial.println("Countdown gestoppt und zurückgesetzt!");
int minutes = countdown_time / 60;
int seconds = countdown_time % 60;
display.showNumberDecEx((minutes * 100) + seconds, 0b01000000, true);
countdown_running = false;
countdown_paused = false; // Sicherstellen, dass der Countdown nicht pausiert ist, wenn er neu startet
playStopSound();
delay(200); // Entprellung
return;
}
}
countdown_paused = !countdown_paused; // Pausieren/Fortsetzen umschalten
Serial.println(countdown_paused ? "Countdown pausiert!" : "Countdown fortgesetzt!");
playStartSound();
delay(200); // Entprellung
}
if (countdown_paused) {
// Anzeige blinken lassen
if ((currentMillis / 400) % 2 == 0) {
display.showNumberDecEx((countdown_time / 60 * 100) + countdown_time % 60, 0b01000000, true); // Anzeige an
} else {
display.clear(); // Anzeige aus
}
continue;
}
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (countdown_time > 0) {
countdown_time -= 1;
int minutes = countdown_time / 60;
int seconds = countdown_time % 60;
display.showNumberDecEx((minutes * 100) + seconds, 0b01000000, true);
} else {
// Buzzer ertönt, wenn die Zeit abgelaufen ist
Serial.println("Zeit abgelaufen!");
countdown_running = false;
countdown_paused = false; // Sicherstellen, dass der Countdown nicht in pausiertem Zustand endet
playMelody();
}
}
}
}