#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0X27, 16, 2);
#include <RTClib.h>
RTC_DS1307 rtc;
int st = 0;
int h = 0; // hour
int m = 0; // minute
int s = 0; // second
int h1 = 0; // hour
int m1 = 0; // minute
int s1 = 0; // second
int h2 = 0; // hour
int m2 = 0; // minute
int s2 = 0; // second
int StartTime = 6;
//Header[3] = {"Time","Start","Stop"};
void setup() {
// init
lcd.init();
lcd.backlight();
rtc.begin();
pinMode(2, INPUT);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(13, OUTPUT);
} //End void setup()
void loop() {
int sw1 = 1;
lcd.clear();
///////////////////////
/* DateTime now = rtc.now();
h1 = now.hour();
m1 = now.minute();
s1 = now.second();
lcd.setCursor(0, 0); lcd.print("Time"); // print Header Time
Start();
*/
Time(); delay(1000);// DateTime now = rtc.now();
sw1 = digitalRead(4); delay(100);
if (sw1 == 0) { // ปุ่มถูกกด
st++;
//check()
if (st >= 7) {
st = 0;
}
////////////////
if (st == 0) {
Time();
} else {
lcd.clear();
Start();
Stop();
} ///end else
// push switch
Serial.println(sw1);
// Start();
// Stop();
delay(1000);
}
}// End void loop()
////////////////////////////////////////////////////////////
void Start() {
if (st == 1) {
int sw2 = digitalRead(3);
delay(100);
//
Serial.println(st);
//
if (sw2 == 0) {
h1++;
if (h1 >= 24) {
h1 = 0;
}// check h>24
}// check sw2
int sw3 = digitalRead(5);
delay(100);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
h1--;
if (h1 < 0) {
h1 = 23;
}// check h<0
}// check sw3
lcd.setCursor(StartTime, 0);
lcd.print(" ");
delay(100);
} // end st=1
// ตั้งนาที
if (st == 2) {
int sw2 = digitalRead(3);
delay(100);
/*
Serial.println(st);
*/
if (sw2 == 0) {
m1++;
if (m1 >= 59) {
m1 = 0;
} // check m>59
}
int sw3 = digitalRead(5);
delay(100);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
m1--;
if (m1 < 0) {
m1 = 59;
} // check m>59
}
lcd.setCursor(StartTime + 3, 0);
lcd.print(" ");
delay(100);
}// check st=2
// ตั้งวินาที
if (st == 3) {
int sw2 = digitalRead(3);
delay(100);
//
Serial.println(st);
//
if (sw2 == 0) {
s1++;
if (s1 >= 59) {
s1 = 0;
} // check m>59
}
int sw3 = digitalRead(5);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
s1--;
if (s1 < 0) {
s1 = 59;
}// check second<0
}// check sw3
lcd.setCursor(StartTime + 6, 0);
lcd.print(" ");
delay(100);
}// check st=3
// lcd.setCursor(0, 0);
// lcd.print(String(h) + ":" + String(m) + ":" + String(s) + " ");
// delay(100);
// lcd.clear();
// แสดงผล
lcd.setCursor(0, 0); lcd.print("Start"); // print Header Start
lcd.setCursor(StartTime, 0);
if (h1 <= 9) {
lcd.print("0"); lcd.print(h1);
}
lcd.print(h1);
lcd.setCursor(StartTime + 2, 0); lcd.print(":");
// นาที
lcd.setCursor(StartTime + 3, 0);
if (m1 <= 9) {
lcd.print("0"); lcd.print(m1);
}
lcd.print(m1);
lcd.setCursor(StartTime + 5, 0); lcd.print(":");
// วินาที
lcd.setCursor(StartTime + 6, 0);
if (s1 <= 9) {
lcd.print("0"); lcd.print(s1);
}
// lcd.print(s);
//lcd.setCursor(2,4); lcd.print(":");
//delay(1000);
//
//
}
//////////////////////////////////////
void Stop() { /// stop = 2
// ตั้งชั่วโมง
if (st == 4) {
int sw2 = digitalRead(3);
delay(10);
//
Serial.println(st);
//
if (sw2 == 0) {
h2++;
if (h2 >= 23) {
h2 = 0;
}// check h>24
}// check sw2
int sw3 = digitalRead(5);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
h2--;
if (h2 < 0) {
h2 = 23;
}// check second<0
}
lcd.setCursor(StartTime, 1);
lcd.print(" ");
delay(100);
} // end st=1
// ตั้งนาที
if (st == 5) {
int sw2 = digitalRead(3);
delay(10);
//
Serial.println(st);
//
if (sw2 == 0) {
m2++;
if (m2 >= 59) {
m2 = 0;
} // check m>59
}
int sw3 = digitalRead(5);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
m2--;
if (m2 < 0) {
m2 = 59;
}// check second<0
}
lcd.setCursor(StartTime + 3, 1);
lcd.print(" ");
delay(100);
}// check st=2
// ตั้งวินาที
if (st == 6) {
int sw2 = digitalRead(3);
delay(10);
//
Serial.println(st);
//
if (sw2 == 0) {
s2++;
if (s2 >= 59) {
s2 = 0;
} // check m>59
}
int sw3 = digitalRead(5);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
s2--;
if (s2 < 0) {
s2 = 59;
}// check second<0
}
lcd.setCursor(StartTime + 6, 1);
lcd.print(" ");
delay(100);
}// check st=3
// lcd.setCursor(0, 0);
// lcd.print(String(h) + ":" + String(m) + ":" + String(s) + " ");
// delay(100);
// lcd.clear();
// แสดงผล
lcd.setCursor(0, 1); lcd.print("Stop "); // print Header Stop
lcd.setCursor(StartTime, 1);
if (h2 <= 9) {
lcd.print("0"); lcd.print(h2);
}
lcd.print(h2);
lcd.setCursor(StartTime + 2, 1); lcd.print(":");
// นาที
lcd.setCursor(StartTime + 3, 1);
if (m2 <= 9) {
lcd.print("0"); lcd.print(m2);
}
lcd.print(m2);
lcd.setCursor(StartTime + 5, 1); lcd.print(":");
// วินาที
lcd.setCursor(StartTime + 6, 1);
if (s2 <= 9) {
lcd.print("0"); lcd.print(s2);
}
// lcd.print(s);
//lcd.setCursor(2,4); lcd.print(":");
//delay(1000);
//
//
}
void Time() {
DateTime now = rtc.now();
h = now.hour();
m = now.minute();
s = now.second();
if (st == 1) {
int sw2 = digitalRead(3);
delay(100);
//
Serial.println(st);
//
if (sw2 == 0) {
h++;
if (h >= 24) {
h = 0;
}// check h>24
}// check sw2
int sw3 = digitalRead(5);
delay(100);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
h--;
if (h < 0) {
h = 23;
}// check h<0
}// check sw3
lcd.setCursor(StartTime, 0);
lcd.print(" ");
delay(100);
} // end st=1
// ตั้งนาที
if (st == 2) {
int sw2 = digitalRead(3);
delay(100);
/*
Serial.println(st);
*/
if (sw2 == 0) {
m++;
if (m >= 59) {
m = 0;
} // check m>59
}
int sw3 = digitalRead(5);
delay(100);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
m--;
if (m < 0) {
m = 59;
} // check m>59
}
lcd.setCursor(StartTime + 3, 0);
lcd.print(" ");
delay(100);
}// check st=2
// ตั้งวินาที
if (st == 3) {
int sw2 = digitalRead(3);
delay(100);
//
Serial.println(st);
//
if (sw2 == 0) {
s++;
if (s > 59) {
s = 0;
} // check m>59
}
int sw3 = digitalRead(5);
if (sw3 == 0) { // สวิทซ์ลดค่าถูกกด
s--;
if (s < 0) {
s = 59;
}// check second<0
}// check sw3
lcd.setCursor(StartTime + 6, 0);
lcd.print(" ");
delay(100);
}// check st=3
// lcd.setCursor(0, 0);
// lcd.print(String(h) + ":" + String(m) + ":" + String(s) + " ");
// delay(100);
// lcd.clear();
// แสดงผล
lcd.setCursor(0, 0); lcd.print("TIME"); // print Header Start
lcd.setCursor(StartTime, 0);
if (h <= 9) {
lcd.print("0"); lcd.print(h1);
}
lcd.print(h);
lcd.setCursor(StartTime + 2, 0); lcd.print(":");
// นาที
lcd.setCursor(StartTime + 3, 0);
if (m <= 9) {
lcd.print("0"); lcd.print(m);
}
lcd.print(m);
lcd.setCursor(StartTime + 5, 0); lcd.print(":");
// วินาที
lcd.setCursor(StartTime + 6, 0);
if (s <= 9) {
lcd.print("0"); lcd.print(s);
}
// lcd.print(s);
//lcd.setCursor(2,4); lcd.print(":");
delay(1000);
//
//
}
//////////////////////////////////////