#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

int ledPin=13; // choose the pin for the LED
int switchPinUp=2; // choose the input pin (for a pushbutton)
int switchPinMin=3;
int pbReset=4;
int val = 0; // variable for reading the pin status
int counter = 0;
int currentStateUp = 0;
int previousStateUp = 0;
int currentStateMin = 0;
int previousStateMin = 0;
int currentStateReset = 0;
int previousStateReset = 0;
bool lock=false;

void setup() {

  lcd.init();
  lcd.backlight();  
  lcd.setCursor(1,0);
  lcd.print("SISTEM COUNTER");
  lcd.setCursor(2,1);
  lcd.print("MEKATRONIKA");
  delay(1000);
  lcd.clear();

  lcd.setCursor(1,0);
  lcd.print("Tekan Tombol");
  lcd.setCursor(6,1);
  lcd.print("Up");

    pinMode(ledPin, OUTPUT); // declare LED as output
    pinMode(switchPinUp, INPUT); // declare pushbutton as input
    pinMode(switchPinMin, INPUT); // declare pushbutton as input
    Serial.begin(9600);
}

void loop() 
{  

  setingan();
  Reset();   

  if (lock==false)
    Up();
    //Down();      
  
  delay(100);
}

void Up()
{
  val = digitalRead(switchPinUp); // read input value
    if (val == LOW) { // check if the input is HIGH (button released)        
        currentStateUp = 1;  //Cek kondisi tombol Up ditekan
    }
    else {  
        currentStateUp = 0;  //Cek kondisi tombol Up dilepas
    }
    if(currentStateUp != previousStateUp){ //Tombol Up ditekan hitung nilai counter
        if(currentStateUp == 1){
            counter = counter+1;
            Serial.println(counter);
            lcd.clear();
            lcd.setCursor(0,0);lcd.print("Set Point = 5");
            lcd.setCursor(0,1);lcd.print("Count     = ");
            lcd.setCursor(12,1);lcd.print(counter);
        }
    }
    previousStateUp = currentStateUp;
}

void Down()
{
  val = digitalRead(switchPinMin); // read input value
    if (val == LOW) { // check if the input is HIGH (button released)       
        currentStateMin = 1;
    }
    else {       
        currentStateMin = 0;
    }
    if(currentStateMin != previousStateMin){
        if(currentStateMin == 1){
            counter = counter-1;
            Serial.println(counter);      
            lcd.clear();        
            lcd.setCursor(0,0);lcd.print("Set Point = 5");
            lcd.setCursor(0,1);lcd.print("Count     =");
            lcd.setCursor(12,1);lcd.print(counter);
        }
    }
    previousStateMin = currentStateMin;
}

void Reset()
{
   
    val = digitalRead(pbReset); // read input value
    if (val == LOW) { // check if the input is HIGH (button released)        
        currentStateReset = 1;
        lock=false;
    }
    else {  
        currentStateReset = 0;
    }
    if(currentStateReset != previousStateReset){
        if(currentStateReset == 1){
            counter = 0;
            Serial.println(counter);
            lcd.clear();
            lcd.setCursor(0,0);lcd.print("Set Point =");
            lcd.setCursor(0,1);lcd.print("Count     =");
            lcd.setCursor(12,1);lcd.print(counter);
        }
    }
    previousStateReset = currentStateReset;
}

void setingan()
{
  if (counter== 5)  
    {
        lock=true;
        digitalWrite(ledPin, HIGH);  

    } 
  else 
    {
        digitalWrite(ledPin, LOW); 
    }
}