DDU_Exammen/ModuleTimer/ModuleTimer.ino

75 lines
1.6 KiB
Arduino
Raw Normal View History

2024-05-15 13:41:20 +02:00
#include <M5Core2.h>
#define penaltyAmount 30 * 1000
#define drawInterval 1000
unsigned long startTime;
const unsigned long countdownDuration = 5 * 60 * 1000; // 5 minutes in milliseconds
unsigned long penalties = 0;
void reset()
{
startTime = millis();
penalties = 0;
}
void setup()
{
M5.begin();
M5.Lcd.begin();
M5.Lcd.fillScreen(TFT_BLACK); // Set initial background color
startTime = millis(); // Record the start time
Serial.begin(9600);
}
bool pressed = false;
void loop()
{
unsigned long elapsedTime = millis() - startTime + penalties;
unsigned long remainingTime = countdownDuration - elapsedTime;
// Check if the time has run out
if (remainingTime <= 0)
{
displayTime(0, 0); // Display 00:00
turnScreenRed();
return;
}
// Calculate remaining minutes and seconds
unsigned long minutes = remainingTime / (60 * 1000);
unsigned long seconds = (remainingTime / 1000) % 60;
// Display remaining time
if (millis() % drawInterval < 10)
{
M5.Lcd.fillScreen(BLACK);
displayTime(minutes, seconds);
}
if (!pressed && M5.Touch.ispressed())
{
Serial.println("Pressed");
penalties += penaltyAmount;
pressed = true;
}
else if (pressed && !M5.Touch.ispressed())
{
pressed = false;
}
}
void displayTime(unsigned long minutes, unsigned long seconds)
{
M5.Lcd.setCursor(75, 50);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.setTextSize(100);
M5.Lcd.printf("%02d:%02d", minutes, seconds);
}
void turnScreenRed()
{
M5.Lcd.fillScreen(TFT_RED);
}