#include <TM1637.h>
#include <RTClib.h>
#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
byte CLK = 12;
byte DIO = 13;
TM1637 time(CLK, DIO); //เรีกใช้งาน lib
byte led = 10;
RTC_DS1307 rtc; //ย่อคำสั่งเรียกใช้งาน
String d0, d1, d2, d3, getkey, function; //เก็บค่าประจำตำแหน่
void setup() { //ตั้งค่าการทำงานของโปรแกร
Serial.begin(9600);
pinMode(led, OUTPUT);
time.init();//เริ่มต้นการทำงานของโปรแกรม
time.set(2); //set ค่าเป็น2
rtc.begin();//เริ่มต้นการทำงานของModule clock
}
void loop() { //การทำงานหลักของโปรแกร
DateTime dt = rtc.now();
String hour = String(dt.hour());
if (hour.length() == 2) { //เช็คจน. ตัวเล
d0 = String(dt.hour()).substring(0,1);
d1 = String(dt.hour()).substring(1,2);
} else{
d0 = "0";
d1 = String(dt.hour()).substring(0,1);
}
String minute = String(dt.minute());
if(minute.length() == 2) {
d2 = String(dt.minute()).substring(0,1);
d3 = String(dt.minute()).substring(1,2);
}else{
d2 = "0";
d3 = String(dt.minute()).substring(0,1);
}
time.point(1);
char key = keypad.getKey();
if (key != NO_KEY) {
getkey = key;
Serial.println(getkey);
}
if (getkey == "D") {
function ="settime";
time.display(0, 0);
time.display(1, 0);
time.display(2, 0);
time.display(3, 0);
} else if (getkey == "#"){
time.display(0, d0.toInt());
time.display(1, d1.toInt());
time.display(2, d2.toInt());
time.display(3, d3.toInt());
}
}