/**
Arduino Calculator
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.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', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
uint64_t value = 0;
unsigned long seconds;
void showSpalshScreen() {
lcd.setCursor(5,0);
lcd.print("Li Yaru");
lcd.setCursor(3, 1);
String message = "20222241460";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(500);
}
void updateCursor() {
lcd.cursor();
delay(200);
lcd.noCursor();
delay(200);
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
showSpalshScreen();
lcd.clear();
pinMode(13, OUTPUT); //连接蜂鸣器正极
digitalWrite(13, LOW);
}
int Hour=0,Min=0,Second=0;
int Hour_Init=19,Min_Init=26,Second_Init=35;
int month=0,day=0;
int month_Init=7,day_Init=14;
int sum[13]={0,0,31,59,90,120,151,181,212,243,273,304,334};
String week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
void FormatDisplay(){
lcd.setCursor(4,0);
if(Hour<10) lcd.print('0');
lcd.print(Hour);
lcd.print(':');
if(Min<10) lcd.print('0');
lcd.print(Min);
lcd.print(':');
if(Second<10) lcd.print('0');
lcd.print(Second);
lcd.setCursor(1,1);
if(month<10) lcd.print('0');
lcd.print(month);
lcd.print('.');
if(day<10) lcd.print('0');
lcd.print(day);
lcd.print(' ');
int days=sum[month]+day;
lcd.print(week[(days-1)%7]);
}
void transform(){
month=month_Init;
while(day>=31){
switch(month){
case 2:
month++;
day-=28;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
month++;
day-=31;
break;
case 4:
case 6:
case 9:
case 11:
month++;
day-=30;
break;
}
}
if(day==30){
switch(month){
case 2:
month++;
day-=28;
break;
case 4:
case 6:
case 9:
case 11:
month++;
day-=30;
break;
}
}
else if(day>=28 && month==2){
month++;
day-=28;
}
}
void change(){
Second=(Second_Init+seconds)%60;
int s=(Second_Init+seconds)/60;
Min=(Min_Init+s)%60;
int m=(Min_Init+s)/60;
Hour=(Hour_Init+m)%24;
int h=(Hour_Init+m)/24;
day=day_Init+h;
transform();
}
void display(){
change();
FormatDisplay();
}
int count=0;
void set_clock(){
count++;
if(count==6){
count=0;
return;
}
switch(count){
case 1:
lcd.setCursor(4,0);
updateCursor();
break;
case 2:
lcd.setCursor(7,0);
updateCursor();
break;
case 3:
lcd.setCursor(10,0);
updateCursor();
break;
case 4:
lcd.setCursor(1,1);
updateCursor();
break;
case 5:
lcd.setCursor(4,1);
updateCursor();
break;
}
}
void set_add(){
if(!count) return ;
switch(count){
case 1:
Hour_Init++;
lcd.clear();
break;
case 2:
Min_Init++;
lcd.clear();
break;
case 3:
Second_Init++;
lcd.clear();
break;
case 4:
month_Init++;
lcd.clear();
break;
case 5:
day_Init++;
lcd.clear();
break;
}
display();
}
void set_sub(){
if(!count) return ;
switch(count){
case 1:
Hour_Init--;
lcd.clear();
break;
case 2:
Min_Init--;
lcd.clear();
break;
case 3:
Second_Init--;
lcd.clear();
break;
case 4:
month_Init--;
lcd.clear();
break;
case 5:
day_Init--;
lcd.clear();
break;
}
display();
}
int set_Hour=0,set_Min=0,set_Second=0;
int set_month=0,set_day=0;
int flag=0;
void set_Display(){
lcd.setCursor(4,0);
if(set_Hour<10) lcd.print('0');
lcd.print(set_Hour);
lcd.print(':');
if(set_Min<10) lcd.print('0');
lcd.print(set_Min);
lcd.print(':');
if(set_Second<10) lcd.print('0');
lcd.print(set_Second);
lcd.setCursor(5,1);
if(set_month<10) lcd.print('0');
lcd.print(set_month);
lcd.print('.');
if(set_day<10) lcd.print('0');
lcd.print(set_day);
}
void set_alarm(char key){
switch(flag){
case 1:
lcd.setCursor(4,0);
updateCursor();
set_Hour+=(key-'0')*10;
break;
case 2:
updateCursor();
set_Hour+=(key-'0');
break;
case 3:
lcd.setCursor(7,0);
updateCursor();
set_Min+=(key-'0')*10;
break;
case 4:
updateCursor();
set_Min+=(key-'0');
break;
case 5:
lcd.setCursor(10,0);
updateCursor();
set_Second+=(key-'0')*10;
break;
case 6:
updateCursor();
set_Second+=(key-'0');
break;
case 7:
lcd.setCursor(5,1);
updateCursor();
set_month+=(key-'0')*10;
break;
case 8:
updateCursor();
set_month+=(key-'0');
break;
case 9:
lcd.setCursor(8,1);
updateCursor();
set_day+=(key-'0')*10;
break;
case 10:
updateCursor();
set_day+=(key-'0');
break;
}
if(flag==10){
lcd.clear();
set_Display();
delay(1000);
}
flag++;
}
void processInput(char key) {
switch(key){
case '*':
set_clock();
break;
case '+':
set_add();
break;
case '-':
set_sub();
break;
case '.':
flag=1;
lcd.clear();
lcd.setCursor(2,0);
lcd.print("set up time");
delay(500);
lcd.clear();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if(!flag) break;
set_alarm(key);
break;
}
}
unsigned int trans(int h,int m,int s){
return h*3600+m*60+s;
}
void alarm(){
if(flag!=11) return ;
if(Hour==set_Hour&&Min==set_Min&&Second==set_Second&&set_month==month&&set_day==day){
tone(13,2093,1000); //蜂鸣器响
flag=0;
}
}
void loop() {
seconds=millis()/1000;
display();
char key = keypad.getKey();
if (key) {
processInput(key);
}
alarm();
}