#include <Simpletimer.h>

const int buttonPin1 = 2; // Pin untuk tombol 1 (10 menit)
const int buttonPin2 = 3; // Pin untuk tombol 2 (20 menit)
const int buttonPin3 = 4; // Pin untuk tombol 3 (Start)
const int buttonPin4 = 5; // Pin untuk tombol 4 (Stop)

Simpletimer timer1{};
int countdown_minutes = 0; // Waktu countdown dalam menit
int countdown_seconds = 0; // Waktu countdown dalam detik

bool countdown_started = false;

void setup() {
  Serial.begin(9600);

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
}

void loop() {
  // Periksa tombol 1 (10 menit)
  if (digitalRead(buttonPin1) == LOW && !countdown_started) {
    countdown_minutes = 10;
    countdown_seconds = countdown_minutes * 60;
    Serial.println("Waktu countdown diatur ke 10 menit.");
    delay(200); // Debouncing
  }

  // Periksa tombol 2 (20 menit)
  if (digitalRead(buttonPin2) == LOW && !countdown_started) {
    countdown_minutes = 20;
    countdown_seconds = countdown_minutes * 60;
    Serial.println("Waktu countdown diatur ke 20 menit.");
    delay(200); // Debouncing
  }

  // Periksa tombol 3 (Start)
  if (digitalRead(buttonPin3) == LOW && (countdown_minutes > 0 || countdown_seconds > 0) && !countdown_started) {
    countdown_started = true;
    Serial.println("Countdown dimulai.");
    delay(200); // Debouncing
  }

  // Periksa tombol 4 (Stop)
  if (digitalRead(buttonPin4) == LOW && countdown_started) {
    countdown_started = false;
    Serial.println("Countdown dihentikan.");
    delay(200); // Debouncing
  }

  // Jalankan countdown jika sudah dimulai
  if (countdown_started && timer1.timer(10)) {
    countdown_seconds--; // Mengurangi waktu countdown setiap detik
    if (countdown_seconds <= 0) {
      // Jika waktu countdown telah habis
      Serial.println("Countdown Selesai!"); 
      countdown_started = false;
    }
    int minutes_remaining = countdown_seconds / 60; // Menghitung menit yang tersisa
    int seconds_remaining = countdown_seconds % 60; // Menghitung detik yang tersisa

    Serial.print("Countdown: ");
    Serial.print(minutes_remaining);
    Serial.print(" minutes ");
    Serial.print(seconds_remaining);
    Serial.println(" seconds");
  }
}