// ----------- ใช้งาน DS1307   -----------
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// ------- ประกาศตัวแปรแทนขา I/O --------
#define SW1 13
#define SW2 27
#define SW3 32
#define BUZ 25
// -------- ประกาศใช้ตัสแปรตั้งเวลา  ----------
#include "SPI.h"
#include <Adafruit_ILI9341.h>
#include "settime.h"
#define TFT_DC 4
#define TFT_CS  2
bool st_SetH=0;
bool st_SetM=0;
bool st_SetS=0;

byte SetH;
byte SetM;
byte SetS;
byte Setfh;
byte Setfm;
byte Setfs;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int hours;
int Minute;
int Second;
/* ********************************************************************** */
void setup() {
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(BUZ, OUTPUT);

  Serial.begin(115200);
  Serial.println("Hello, ESP32!");

  tft.begin();
 tft.setRotation(1);
 tft.fillScreen(ILI9341_BLACK);
 tft.drawRGBBitmap(0 , 0,(uint16_t *)dragonBitmap,DRAGON_WIDTH, DRAGON_HEIGHT);

    if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
  
};
/* ********************************************************************** */
void loop() {
  DateTime now = rtc.now();
  
  if(digitalRead(SW3) == 0){  // กด SW3 ตั้งเวลา
     delay(3000);
     if(digitalRead(SW3) == 0){                    // กด SW3 ค้างไว้ 2 วิ
      tone(23, 262, 1000);
      tft.fillScreen(ILI9341_BLACK);
 tft.drawRGBBitmap(0 , 0,(uint16_t *)dragonBitmap,DRAGON_WIDTH, DRAGON_HEIGHT);
        st_SetH = 1;
        Serial.println("-----------------------------");
        Serial.println("       Set Timer Mode       ");
        Serial.println("-----------------------------");
        Serial.println("->Set Hours");
        // ------- ตั้งเวลา Hours (ชม.) --------
        while(st_SetH == 1){
          if(digitalRead(SW1) == 0){     // เพิ่มค่า ชม. ครั้งละ 1
             while(digitalRead(SW1)) delay(10);
             SetH = SetH + 1;
             if(SetH >= 24) SetH = 24;
             tft.setCursor(100, 120);
              tft.setTextColor(ILI9341_BLACK,ILI9341_WHITE);
              tft.setTextSize(3);
              tft.println(String(SetH));
              
          }
          if(digitalRead(SW2) == 0){     // ลดค่า ชม. ครั้งละ 1
             while(digitalRead(SW2)) delay(10);
             SetH = SetH - 1;
             if(SetH == 255) SetH = 0;
             Serial.println("Hours: " + String(SetH));
             delay(50);
          }
          if(digitalRead(SW3) == 0){        // ยืนยันตั้งค่า ชม.
             delay(1000);
            if(digitalRead(SW3) == 0){
               st_SetH = 0;  // ออกจาก while ชม.
               st_SetM = 1;  // เข้า while นาที
               Serial.println("Set Hours OK");
            }

          }
        } 
        // -----------   Set Minute (นาที)  ---------
        Serial.println();
        Serial.println("->Set Minute");
        while(st_SetM == 1){
           if(digitalRead(SW1) == 0){     // เพิ่มค่า ชม. ครั้งละ 1
             while(digitalRead(SW1)) delay(10);
             SetM = SetM + 1;
             if(SetM >= 60) SetM = 60;
             Serial.println("Minute: " + String(SetM));
             delay(50);
          }
          if(digitalRead(SW2) == 0){     // ลดค่า ชม. ครั้งละ 1
             while(digitalRead(SW2)) delay(10);
             SetM = SetM - 1;
             if(SetM == 255) SetM = 0;
             Serial.println("Minute: " + String(SetM));
             delay(50);
          }
          if(digitalRead(SW3) == 0){        // ยืนยันตั้งค่า ชม.
              delay(1000);
             if(digitalRead(SW3) == 0){
                st_SetM = 0;  // ออกจาก while นาที.
                st_SetS = 1;  // เข้า while นาที
                Serial.println("Set Minute OK");
             }
          }
        }
        // ------------Set Second-------------
        Serial.println();
        Serial.println("->Set Second");
        while(st_SetS == 1){
          if(digitalRead(SW1) == 0){     // เพิ่มค่า ชม. ครั้งละ 1
             while(digitalRead(SW1)) delay(10);
             SetS = SetS + 1;
             if(SetS >= 60) SetS = 60;
             Serial.println("Second: " + String(SetS));
             delay(50);
          }
          if(digitalRead(SW2) == 0){     // ลดค่า ชม. ครั้งละ 1
             while(digitalRead(SW2)) delay(10);
             SetS = SetS - 1;
             if(SetS == 255) SetS = 0;
             Serial.println("Second: " + String(SetS));
             delay(50);
          }
          if(digitalRead(SW3) == 0){        // ยืนยันตั้งค่า ชม.
              delay(1000);
             if(digitalRead(SW3) == 0){
                st_SetS = 0;  // ออกจาก while วินาที.
                Serial.println("Set Second OK");
             }
          }
        }
        // -----------------------------------
     }    // if เช็คการกด SW3 ค้าง
  }      // if กด SW3 2 วิ
  Serial.print(" Cerrent Time =  ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print("      Time ON =  ");
  Serial.print(SetH);
  Serial.print(':');
  Serial.print(SetM);
  Serial.print(':');
   Serial.print(SetS);
   Serial.print("      Time OFF =  ");
  Serial.print(Setfh);
  Serial.print(':');
   Serial.print(Setfm);
  Serial.print(':');
   Serial.print(Setfh);
   Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
   Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");


  Serial.println();

  tft.setTextColor(ILI9341_RED,ILI9341_WHITE);
  tft.setTextSize(2);
   tft.setCursor(190, 220);
   tft.println(hours, DEC);
   tft.setCursor(215, 220);
   tft.println(":");
   tft.setCursor(230, 220);
   tft.println(Minute, DEC);
   tft.setCursor(255, 220);
   tft.println(":");
   tft.setCursor(270, 220);
   tft.println(Second, DEC);




    tft.setCursor(135, 100);
   tft.println(SetH);
   tft.setCursor(165, 100);
   tft.println(":");
   tft.setCursor(180, 100);
   tft.println(SetM);
   tft.setCursor(205, 100);
   tft.println(":");
   tft.setCursor(225, 100);
   tft.println(SetS);
   delay(1000);

   tft.setCursor(135, 175);
   tft.println(Setfh);
   tft.setCursor(165, 175);
   tft.println(":");
   tft.setCursor(180, 175);
   tft.println(Setfm);
   tft.setCursor(205, 175);
   tft.println(":");
   tft.setCursor(225, 175);
   tft.println(Setfs);
   delay(1000);

   tft.setCursor(255, 20);
   tft.println(now.year(), DEC);
  
   tft.setCursor(100, 20);
   tft.println(now.month(), DEC);
  
   tft.setCursor(190, 20);
   tft.println(now.day(), DEC);
   delay(1000);

  delay(1000); // this speeds up the simulation
  if(now.hour() == SetH && now.minute() == SetM && now.second() == SetS){
     tone(23, 262, 1000); // Plays 262Hz tone for 0.250 seconds

  }
  tft.drawRGBBitmap(0 , 0,(uint16_t *)dragonBitmap,DRAGON_WIDTH, DRAGON_HEIGHT);
    
};
NOCOMNCVCCGNDINLED1PWRRelay Module
GND5VSDASCLSQWRTCDS1307+