/**
Arduino Calculator
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include <MsTimer2.h>
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'h', '0', 'm', 's'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
bool timerPaused = true;
bool timerChange = false;
unsigned long long setTime = 0;
unsigned long long startTime;
unsigned long long Time = 0;
unsigned long long pausedTime=0;
unsigned long long psTime;
unsigned long long pfTime;
int h=0;
int m=0;
int s=0;
int x=0;
int y=0;
int count=0;
void showSpalshScreen() {
lcd.print("LiuJingwen");
lcd.setCursor(0, 1);
String message = "20232241308";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(1000);
lcd.clear();
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
showSpalshScreen();
MsTimer2::set(1000, timerInterrupt); // 1 second interrupt
// MsTimer2::start();
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
handleKeyPress(key);
}
if(!timerPaused&&Time>0){
updateDisplay();
}
else if(0==Time){
lcd.setCursor(0, 0);
lcd.print("Timer Time is up!");
lcd.setCursor(0, 1);
lcd.print("Reset ");
}
}
void timerInterrupt() {
unsigned long long curTime = millis();
Time = setTime-(curTime-startTime-pausedTime);
if(Time==0){
timerPaused=true;
}
}
void handleKeyPress(char key) {
if (key == 'A') {
if(setTime>0&&!timerChange&&timerPaused){
pfTime=millis();
timerPaused=false;
pausedTime+=pfTime-psTime-1;
pfTime=0;
psTime=0;
MsTimer2::start();
}
if(timerChange){
setTime=1000ULL*(3600*h+60*m+s);
startTime = millis();
MsTimer2::start();
h=0;
m=0;
s=0;
lcd.setCursor(0, 1);
lcd.print("Reset ");
lcd.setCursor(6, 1);
timerPaused=false;
timerChange=false;
}
}
else if (key == 'B') {
if(!timerPaused){
timerPaused=true;
psTime=millis();
MsTimer2::stop();
}
}
else if (key == 'C') {
setTime=0;
Time=0;
x=0;
y=0;
pausedTime=0;
timerPaused=true;
lcd.setCursor(0, 1);
lcd.print("Reset ");
lcd.setCursor(6, 1);
}
else if (key == 'h') {
if(count){
h=10*x+y;
x=0;
y=0;
timerChange=true;
count=0;
}
}
else if (key == 'm') {
if(count){
m=10*x+y;
x=0;
y=0;
timerChange=true;
count=0;
}
}
else if (key == 's') {
if(count){
s=10*x+y;
x=0;
y=0;
timerChange=true;
count=0;
}
}
else {
if(count<2){
lcd.setCursor(6+count++, 1);
lcd.print(key);
x=y;
y=key-'0';
}
}
}
void updateDisplay() {
// if(Time>=1ULL){
int hours = (Time /3600000) ;
int minutes = (Time % 3600000)/60000;
int seconds = (Time -hours*3600000-minutes*60000) / 1000;
lcd.setCursor(0, 0);
lcd.print("Timer ");
if (hours < 10)
lcd.print("0");
lcd.print(hours);
lcd.print(":");
if (minutes < 10)
lcd.print("0");
lcd.print(minutes);
lcd.print(":");
if (seconds < 10)
lcd.print("0");
lcd.print(seconds);
lcd.print(" ");
}