//ต่อ LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
//ต่อ rtc (real time clock)
#include "RTClib.h"
RTC_DS1307 rtc;

int st =0;
///int สำหรับ real time
int h = 0;
int m = 0;
int s = 0;
/// int สำหรับ start
int h1 = 0;
int m1 = 0;
int s1 = 0;
///int สำหรับ stop
int h2 = 0;
int m2 = 0;
int s2 = 0;



void setup() {
///กำหนด pinmode ที่เกี่ยวข้อง  
pinMode(4, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(13, OUTPUT);

///สั่ง lcd เริ่มทำงาน
lcd.init();
lcd.backlight();
///สั่ง rtc เริ่มทำงาน
rtc.begin();


} //// End setup


void loop() {


///เริ่มต้นทำงานเมือกด set ที่ pin4 
int sw1 = digitalRead(4);
delay(500);
if(sw1 ==0){
  st++;
}
///ให้กดวน หลังจากกดครั้งที่ 6 แล้ว ถ้ากดอีกให้วนมาตำแหน่งที่ 0 คือ h ของ start ใหม่
if(st>=7){
  st=0;
  lcd.clear();
  delay(500);
  }



///// เรียกคำสั่ง start และ stop จาก function ที่เราทำไว้ด้านล่าง
//start();
//stop();


///ใช้ if กำหนดให้แสดงเวลา real time เมือ st==0 คือ เมื่อกดปุ่ม set ให้แสดงเวลา start และ stop 
if(st==0){
  time();
}else{
  start(); 
  stop();
}
//ถ้า real time ถึงเวลา start ให้ led สว่าง
if(h==h1){
  if(m==m1){
    digitalWrite(13, HIGH);
  }
}
//ถ้า real time ถึงเวลา stop ให้ led ดับ
if(h==h2){
  if(m==m2){
    digitalWrite(13, LOW);
  }
}



}  /// End loop


//////////////////////// Functions
/// function start
void start(){
//ปรับชั่วโมง h1
if(st==1){
  ///กรณีปรับเพิ่ม โดยกด switch up  
  int sw2 = digitalRead(3);
  delay(50);
  if(sw2 ==0){
  h1++;
  if(h1>=24){
  h1=0;
  }
 }
 //กรณีปรับลด โดยกด switch down
  int sw3 = digitalRead(5);
  delay(50);
  if(sw3 ==0){
  h1--;
  if(h1<= 0){
  h1=23;
  }
 }

//display h1 กระพริบ โดยสั่งให้ lcd print "  " ค่าว่าง สลับค่าจริง
 lcd.setCursor(6,0);
 lcd.print("  ");
 delay(50);
}

//ปรับนาที m1
if(st==2){
 //กรณีปรับเพิ่ม โดยกด switch up  
  int sw2 = digitalRead(3);
  delay(50);
  if(sw2 ==0){
  m1++;
  if(m1>=60){
  m1=0;
  }
 }
 //กรณีปรับลด โดยกด switch down
  int sw3 = digitalRead(5);
  delay(50);
  if(sw3 ==0){
  m1--;
  if(m1<=0){
  m1=59;
  }
 }

//display m1 กระพริบ โดยสั่งให้ lcd print "  " ค่าว่าง สลับค่าจริง
 lcd.setCursor(9,0);
 lcd.print("  ");
 delay(50);
}

//ปรับวินาที s1
if(st==3){
 ///กรณีปรับเพิ่ม โดยกด switch up  
  int sw2 = digitalRead(3);
  delay(50);
  if(sw2 ==0){
  s1++;
  if(s1>=60){
  s1=0;
  }
 }
 //กรณีปรับลด โดยกด switch down
  int sw3 = digitalRead(5);
  delay(50);
  if(sw3 ==0){
  s1--;
  if(s1<=0){
  s1=59;
  }
 }

//display s1 กระพริบ โดยสั่งให้ lcd print "  " ค่าว่าง สลับค่าจริง
 lcd.setCursor(12,0);
 lcd.print("  ");
 delay(50);
}

// set แสดงคำว่า start 
lcd.setCursor(0,0);
lcd.print("Start");

// f ตั้งชั่วโมง h1 เป็นค่า 2 หลัก และใช้ if ให้แสดงค่า 0 ที่ตำแหน่งก่อนหน้า ถ้าค่า h <= 9
lcd.setCursor(6,0);
if(h1<=9){
  lcd.print("0");
}
lcd.print(h1);
lcd.setCursor(8,0);
lcd.print(":");

// f ตั้งนาที m1 เป็นค่า 2 หลัก  และใช้ if ให้แสดงค่า 0 ที่ตำแหน่งก่อนหน้า ถ้าค่า m <= 9
lcd.setCursor(9,0);
if(m1<=9){
  lcd.print("0");
}
lcd.print(m1);
lcd.setCursor(11,0);
lcd.print(":");

// f ตั้งวินาที s1 เป็นค่า 2 หลัก  และใช้ if ให้แสดงค่า 0 ที่ตำแหน่งก่อนหน้า ถ้าค่า s <= 9
lcd.setCursor(12,0);
if(s1<=9){
  lcd.print("0");
}
lcd.print(s1);
}
///End function start


/// function stop
void stop(){
//ปรับชั่วโมง h2
if(st==4){
 //กรณีปรับเพิ่ม โดยกด switch up 
  int sw2 = digitalRead(3);
  delay(50);
  if(sw2 ==0){
  h2++;
  if(h2>=24){
  h2=0;
  }
 }
 //กรณีปรับลด โดยกด switch down 
  int sw3 = digitalRead(5);
  delay(50);
  if(sw3 ==0){
  h2--;
  if(h2<=0){
  h2=23;
  }
 }

//display h2 กระพริบ โดยสั่งให้ lcd print "  " ค่าว่าง สลับค่าจริง
 lcd.setCursor(6,1);
 lcd.print("  ");
 delay(50);
}

//ปรับนาที m2
if(st==5){
 //กรณีปรับเพิ่ม โดยกด switch up
  int sw2 = digitalRead(3);
  delay(50);
  if(sw2 ==0){
  m2++;
  if(m2>=60){
  m2=0;
  }
 }
 //กรณีปรับลด โดยกด switch down
  int sw3 = digitalRead(5);
  delay(50);
  if(sw3 ==0){
  m2--;
  if(m2<=0){
  m2=59;
  }
 }

//display m2 กระพริบ โดยสั่งให้ lcd print "  " ค่าว่าง สลับค่าจริง
 lcd.setCursor(9,1);
 lcd.print("  ");
 delay(50);
}

//ปรับวินาที s2
if(st==6){
 ///กรณีปรับเพิ่ม โดยกด switch up
  int sw2 = digitalRead(3);
  delay(50);
  if(sw2 ==0){
  s2++;
  if(s2>=60){
  s2=0;
  }
 }
 //กรณีปรับลด โดยกด switch down
  int sw3 = digitalRead(5);
  delay(50);
  if(sw3 ==0){
  s2--;
  if(s2<=60){
  s2=59;
  }
 }

//display s2 กระพริบ โดยสั่งให้ lcd print "  " ค่าว่าง สลับค่าจริง
 lcd.setCursor(12,1);
 lcd.print("  ");
 delay(50);
}

// set คำว่า stop 
lcd.setCursor(0,1);
lcd.print("Stop");

// f ตั้งชั่วโมง h2 เป็นค่า 2 หลัก และใช้ if ให้แสดงค่า 0 ที่ตำแหน่งก่อนหน้า ถ้าค่า h <= 9
lcd.setCursor(6,1);
if(h2<=9){
  lcd.print("0");
}
lcd.print(h2);
lcd.setCursor(8,1);
lcd.print(":");

// f ตั้งนาที m2 เป็นค่า 2 หลัก  และใช้ if ให้แสดงค่า 0 ที่ตำแหน่งก่อนหน้า ถ้าค่า m <= 9
lcd.setCursor(9,1);
if(m2<=9){
  lcd.print("0");
}
lcd.print(m2);
lcd.setCursor(11,1);
lcd.print(":");

// f ตั้งวินาที s2 เป็นค่า 2 หลัก  และใช้ if ให้แสดงค่า 0 ที่ตำแหน่งก่อนหน้า ถ้าค่า s <= 9
lcd.setCursor(12,1);
if(s2<=9){
  lcd.print("0");
}
lcd.print(s2);
}

/// function real time
void time (){
//set อ่านวันเวลา real time จากเครื่อง
  DateTime now = rtc.now();
///เรียกใช้ real time h m s
h = now.hour();
m = now.minute();
s = now.second();
//แสดงเวลา real time hh:mm:ss
lcd.setCursor(0,0);
lcd.print("Time");

lcd.setCursor(6,0);
if(h<=9){
  lcd.print("0");
}
lcd.print(h);
lcd.setCursor(8,0);
lcd.print(":");

lcd.setCursor(9,0);
if(m<=9){
  lcd.print("0");
}
lcd.print(m);
lcd.setCursor(11,0);
lcd.print(":");

lcd.setCursor(12,0);
if(s<=9){
  lcd.print("0");
}
lcd.print(s);
}

GND5VSDASCLSQWRTCDS1307+