#include "CLK.h"
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <TimerOne.h>
CLK myCLK;
LiquidCrystal_I2C lcd(0x27, 20, 4);
const uint8_t ROWS = 4;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
{ '1', '2', '3'},
{ '4', '5', '6'},
{ '7', '8', '9'},
{ '*', '0', '#'}
};
uint8_t colPins[COLS] = { 5, 4, 3 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad1(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
bool _auto = true;
int count1 = 0;
int digit = 0;
String h = "", m = "", s = "";
int ch = 23, cm = 59, cs = 59, ss = 99;
void displaydate()
{
lcd.setCursor(0, 0);
lcd.print("myClock: S1152014");
lcd.setCursor(0, 1);
lcd.print(myCLK._year);
lcd.print("/");
if (myCLK._month < 10)
lcd.print("0");
lcd.print(myCLK._month);
lcd.print("/");
if (myCLK._day < 10)
lcd.print("0");
lcd.print(myCLK._day);
}
void displaytime()
{
lcd.setCursor(0, 2);
if (myCLK._hour < 10)
lcd.print("0");
lcd.print(myCLK._hour);
lcd.print(":");
if (myCLK._minute < 10)
lcd.print("0");
lcd.print(myCLK._minute);
lcd.print(":");
if (myCLK._second < 10)
lcd.print("0");
lcd.print(myCLK._second);
}
void countdown()
{
lcd.setCursor(0, 3);
if (ch < 10)
lcd.print("0");
lcd.print(ch);
lcd.print(":");
if (cm < 10)
lcd.print("0");
lcd.print(cm);
lcd.print(":");
if (cs < 10)
lcd.print("0");
lcd.print(cs);
lcd.print('.');
if (ss < 10)
lcd.print("0");
lcd.print(ss);
lcd.print(" ");
}
void tick()
{
if (ss <= 0)
{
if (cs > 0)
{
cs--;
ss = 99;
}
}
else
ss--;
if (cs <= 0)
{
if (cm > 0)
{ cm--;
cs = 59;
}
}
if (cm <= 0)
{
if (ch > 0)
{ ch--;
cm = 59;
}
}
if (count1 >= 100)
count1 = 0;
else
count1++;
}
void setup() {
Serial.begin(9600);
myCLK.init();
lcd.init();
for (int i = 0; i <= 2; i++)
{
lcd.backlight();
delay(500);
lcd.noBacklight();
delay(500);
}
lcd.backlight();
delay(500);
displaydate();
Timer1.initialize(10000); // us單位
Timer1.attachInterrupt(tick);
}
void loop() {
myCLK.uptime();
displaytime();
char key = keypad1.getKey();
if (key != NO_KEY) {
if (key == '*')
{
_auto = true;
ch = h.toInt();
cm = m.toInt();
cs = s.toInt();
h = "";
m = "";
s = "";
}
else if (key == '#')
{
_auto = false;
digit = 0;
}
else
{
if ((digit + 1) % 3 == 0)
digit++;
lcd.setCursor(digit, 3);
lcd.print(key);
if (digit < 2)
{
if (digit == 0)
{
h = "";
m = "";
s = "";
}
h = h + key;
}
else if (digit > 2 && digit < 5)
m = m + key;
else if (digit > 5 && digit < 8)
s = s + key;
if (digit >= 7)
{
digit = 0;
}
else
digit++;
}
}
if (_auto)
countdown();
if (!_auto)
{
if (count1 >= 0 && count1 <= 50)
{
lcd.setCursor(2, 3);
lcd.print(' ');
lcd.setCursor(5, 3);
lcd.print(' ');
}
else if (count1 > 50)
{
lcd.setCursor(2, 3);
lcd.print(':');
lcd.setCursor(5, 3);
lcd.print(':');
}
}
}