#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <TM1637Display.h>
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define BUTTON_PIN 13
#define TM1637_CLK 21
#define TM1637_DIO 22
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
TM1637Display display(TM1637_CLK, TM1637_DIO);
bool screenState = false;
unsigned long previousMillis = 0;
const long blinkInterval = 500;
bool currentScreen = 0; // 0 = Startbildschirm, 1 = Konfigurationsbildschirm
// Timer Variablen
bool timerRunning = false;
unsigned long timerStartTime = 0;
unsigned long timerDuration = 30000; // 30 Sekunden in Millisekunden
unsigned long lastSegmentUpdate = 0;
const long segmentUpdateInterval = 10; // 10ms für Hundertstel
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Display Reset
pinMode(TFT_RST, OUTPUT);
digitalWrite(TFT_RST, HIGH);
delay(100);
digitalWrite(TFT_RST, LOW);
delay(100);
digitalWrite(TFT_RST, HIGH);
delay(100);
tft.begin();
tft.setRotation(3);
// TM1637 initialisieren
display.setBrightness(0x0f); // Maximale Helligkeit
display.showNumberDecEx(0, 0b01000000, true); // 00.00 anzeigen
drawStartScreen();
}
void loop() {
unsigned long currentMillis = millis();
// Blinkender Kasten nur auf Startbildschirm
if (currentScreen == 0) {
if (currentMillis - previousMillis >= blinkInterval) {
previousMillis = currentMillis;
screenState = !screenState;
drawStartBox(screenState);
}
}
// Timer auf Segmentanzeige aktualisieren
if (currentScreen == 1 && timerRunning) {
if (currentMillis - lastSegmentUpdate >= segmentUpdateInterval) {
lastSegmentUpdate = currentMillis;
updateSegmentDisplay();
}
}
// Button abfragen
if (digitalRead(BUTTON_PIN) == LOW) {
delay(50); // Entprellung
if (digitalRead(BUTTON_PIN) == LOW) {
if (currentScreen == 0) {
// Wechsel zu Konfigurationsbildschirm
currentScreen = 1;
drawConfigScreen();
} else {
// Im Konfigurationsbildschirm: Timer starten/stoppen
if (!timerRunning) {
startTimer();
} else {
stopTimer();
}
}
}
}
}
void drawStartScreen() {
tft.fillScreen(ILI9341_BLACK);
// Titel
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.setCursor(50, 50);
tft.println("HNGR13 BOMB");
// Unterer Text
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(80, 100);
tft.println("Bereit zum Starten");
}
void drawStartBox(bool visible) {
int x = 100, y = 150, w = 120, h = 40;
if (visible) {
tft.fillRect(x, y, w, h, ILI9341_GREEN);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(x + 40, y + 12);
tft.println("START");
} else {
tft.fillRect(x, y, w, h, ILI9341_BLACK);
tft.drawRect(x, y, w, h, ILI9341_GREEN);
}
}
void drawConfigScreen() {
tft.fillScreen(ILI9341_BLACK);
// Titel
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(3);
tft.setCursor(50, 50);
tft.println("KONFIGURATION");
// Timer Status
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(80, 100);
tft.println("Timer: BEREIT");
// Segmentanzeige Info
tft.setCursor(80, 130);
tft.println("TM1637: 00.00");
// Start/Stop Button
tft.fillRect(100, 180, 120, 40, ILI9341_GREEN);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(120, 192);
tft.println("START");
}
void updateConfigScreenTimer() {
tft.fillRect(80, 100, 200, 25, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(80, 100);
if (timerRunning) {
tft.println("Timer: LÄUFT");
// Button auf STOP ändern
tft.fillRect(100, 180, 120, 40, ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(125, 192);
tft.println("STOP");
} else {
tft.println("Timer: BEREIT");
// Button auf START ändern
tft.fillRect(100, 180, 120, 40, ILI9341_GREEN);
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(120, 192);
tft.println("START");
}
}
void startTimer() {
timerRunning = true;
timerStartTime = millis();
updateConfigScreenTimer();
}
void stopTimer() {
timerRunning = false;
updateConfigScreenTimer();
}
void updateSegmentDisplay() {
if (!timerRunning) return;
unsigned long elapsed = millis() - timerStartTime;
unsigned long remaining = (timerDuration > elapsed) ? (timerDuration - elapsed) : 0;
// In Sekunden und Hundertstel umrechnen
unsigned long seconds = remaining / 1000;
unsigned long hundredths = (remaining % 1000) / 10;
// Format: SS.HH (z.B. 30.00)
int displayValue = (seconds * 100) + hundredths;
// Mit Punkt in der Mitte anzeigen
display.showNumberDecEx(displayValue, 0b01000000, true);
// Timer abgelaufen?
if (remaining == 0) {
timerRunning = false;
updateConfigScreenTimer();
// Alarm anzeigen
for (int i = 0; i < 5; i++) {
display.showNumberDec(8888, true); // Alle Segmente an
delay(200);
display.clear();
delay(200);
}
display.showNumberDecEx(0, 0b01000000, true); // Zurück zu 00.00
}
}
5V Quelle
Stromversorgung