//PROGRAMKNOX-BOX FIX JADI 
//ALHAMDULILLAH
#define mode1 60000 // 60 detik
#define mode2 78000 // 80 detik
//#define PWM_FREQ 5000 // Frekuensi PWM dalam Hz
//#define PWM_MAX 255 // Nilai maksimum PWM (duty cycle 100%)

const int fan = 13;//fan
const int speaker = 12;//speaker
const int startButtonPin = 0;//tombol start
const int selectButtonPin = 1;//tombol pause
const int waktu_60 = A4;// 60 detik
const int waktu_80 = A3;// 80 detik

int count, tambah, value;
unsigned long startTime;
unsigned long pauseTime;
unsigned long elapsedTime;
long jeda = 3000;
bool jalan = true;

enum Mode {
  START,
  PAUSE
};

Mode currentMode = PAUSE;

void setup() {
  //Serial.begin(9600);
  pinMode(fan, OUTPUT);
  pinMode(speaker, OUTPUT);
  pinMode(waktu_60, OUTPUT);
  pinMode(waktu_80, OUTPUT);
  pinMode(startButtonPin, INPUT);
  pinMode(selectButtonPin, INPUT);

  digitalWrite(fan, 0);
  digitalWrite(speaker, 0);
  digitalWrite(waktu_60, LOW);
  digitalWrite(waktu_80, LOW);

}

void loop() {
  value = digitalRead(selectButtonPin);
  if (value == 0) {
    tambah++;
    delay(300);
  }
  switch(tambah){
    default:
    digitalWrite(waktu_60, HIGH);
    digitalWrite(waktu_80, LOW);
    break;
    case 1:
    digitalWrite(waktu_60, LOW);
    digitalWrite(waktu_80, HIGH);
    break;
    case 2:
    digitalWrite(waktu_60, HIGH);
    digitalWrite(waktu_80, LOW);
    tambah=0;
    break;
  }

  if (count == 0) {
    currentMode = PAUSE;
  }
  if (digitalRead(startButtonPin) == 0) {
    count++;
    delay(300);
  }
//==========================mode start====================================
  if (currentMode == START) {
    if (count == 2) {
      pauseTime = millis();
      jeda=elapsedTime+3000;
      currentMode = PAUSE;
      count = 0;
    }

    if (jalan) {
      unsigned long currentTime = millis();
      elapsedTime = currentTime - startTime;
      
      if (elapsedTime >= 0) {
        analogWrite(fan, 255);
        analogWrite(speaker, 0);
      }

      if (elapsedTime >= jeda) {
        analogWrite(fan, 255);
        analogWrite(speaker, 130); //harus >128
      }

      if ((elapsedTime >= mode1) && (tambah == 0) || (tambah == 2)) {
        analogWrite(fan, 0);
        analogWrite(speaker, 0);
        jalan = false;
      }

      if ((elapsedTime >= mode2) && (tambah == 1)) {
        analogWrite(fan, 0);
        analogWrite(speaker, 0);
        jalan = false;
      }
//=====================kedip 60=================================
      if ((elapsedTime >= 24000) && (elapsedTime <= 42000) && (tambah == 0) || (tambah == 2)) {
        kedip(1000,waktu_60);
      }
      if ((elapsedTime >= 42000) && (elapsedTime <= 54000) && (tambah == 0) || (tambah == 2)) {
        kedip(500,waktu_60);
      }
      if ((elapsedTime >= 54000) && (tambah == 0) || (tambah == 2)) {
        kedip(200,waktu_60);
      }
//=====================kedip 80=================================
      if ((elapsedTime >= 31200) && (elapsedTime <= 54600) && (tambah == 1)) {
        kedip(1000,waktu_80);
      }
      if ((elapsedTime >= 54600) && (elapsedTime <= 70200) && (tambah == 1)) {
        kedip(500,waktu_80);
      }
      if ((elapsedTime >= 70200) && (tambah == 1)) {
        kedip(200,waktu_80);
      }

    }
//=========Jika mode start dimatikan, reset startTime===========
    else { 
      startTime = millis(); // Set ulang startTime
      jeda=3000;

    }
  }
//==========================mode pause====================================
  else if (currentMode == PAUSE) {
    if (count == 1) {
      startTime = millis() - (pauseTime - startTime); // Melanjutkan waktu dari waktu yang disimpan sebelumnya
      currentMode = START;
      jalan = true;
    }

    digitalWrite(fan, 0);
    digitalWrite(speaker, 0);
  }
}

void kedip(int time,int pinLed){
  digitalWrite(pinLed, HIGH);
  delay(time);
  digitalWrite(pinLed, LOW);
  delay(time);
}