/*
11 Oct 2022

Purpose:Test Alarm Function

Berhasil

*/

int ledPin = 13;
int buttonPin = 2;
int led_State = 0;
int button_State_Old = 0;
int button_State_New;
int alarm_State = 0;
int jam_State = 0;

int Time_Hour = 5;
int time_hour = 5;

unsigned long prev_millis_Old = 0;

const long interval = 100;


void setup(){

Serial.begin(9600);
pinMode(ledPin,OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);

}


void loop(){

unsigned long current_millis = millis();

if(current_millis - prev_millis_Old >= interval){

  prev_millis_Old = current_millis;

  if (Time_Hour == time_hour){

    buttonSetting_1();

  }
  else{

    buttonSetting_2();

  }

}
 Serial.print("Button State: ");
 Serial.print(button_State_New);
 Serial.print(", led State: ");
 Serial.print(led_State);
 Serial.print(", alarm state: ");
 Serial.println(alarm_State);

}

void buttonSetting_1(){
 
  button_State_New = digitalRead(buttonPin);
  
  //if Light button new state is not the same with Light button old state
  if ( button_State_New != button_State_Old ){

    //if Light button new state is 1
    if( button_State_New == 1){

      //if Light state is 0,open window 1,and now Light State is 1
      if(led_State == 0 ){
        
        digitalWrite(ledPin,HIGH);
        led_State = 1;
        alarm_State = 1;
        
      }
      else{

        digitalWrite(ledPin,LOW);
        led_State = 0;
        alarm_State = 0;
        
      }
    }
    //Now the Light Button old is same as Light Button New
    button_State_Old = button_State_New;    
  }
  
}

void buttonSetting_2(){
  
  button_State_New = digitalRead(buttonPin);
   
  //if Light button new state is not the same with Light button old state
  if ( button_State_New != button_State_Old ){

    //if Light button new state is 1
    if( button_State_New == 1){

      //if Light state is 0,open window 1,and now Light State is 1
      if(led_State == 0 ){
        
        digitalWrite(ledPin,LOW);
        led_State = 1;
        alarm_State = 0;
        
      }
      else{

        digitalWrite(ledPin,HIGH);
        led_State = 0;
        alarm_State = 0;
        
      }
    }
    //Now the Light Button old is same as Light Button New
    button_State_Old = button_State_New;    
  }
  
}