#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define INTERNAL_LED 13
#define TRUE 1
#define FALSE 0
#define R1 9
#define R2 8
#define R3 7
#define R4 6
#define C1 5
#define C2 4
#define C3 3
#define C4 2
#define DELAY 1
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int secs;
int minutes;
int hours;
int day;
int month;
int year;
int daysOfMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
bool led13 = HIGH;
bool released = TRUE;
void setup() {
//setup matrix pins
pinMode(R1, INPUT_PULLUP);
pinMode(R2, INPUT_PULLUP);
pinMode(R3, INPUT_PULLUP);
pinMode(R4, INPUT_PULLUP);
lcd.init(); // initialize the lcd
lcd.backlight();
month = inputData ("Month ");
day = inputData ("Day ");
year = inputData ("Year ");
hours = inputData ("Hours (24) ");
minutes = inputData("Minutes ");
secs = inputData ("Seconds ");
// Print a message to the LCD
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Hello, World!");
lcd.setCursor(3,1);
lcd.print("Date and Time");
printTime();
}
int counter = 0;
void loop() {
bool keyPressed;
int key;
counter++;
if (counter > 74){
digitalWrite(INTERNAL_LED, led13);
if (led13 == HIGH)
led13 = LOW;
else
led13 = HIGH;
delay(965);
secs++;
if (secs >= 60) {
secs = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
}
if (hours >=24) {
day++;
hours = 0;
if (day > daysOfMonth[month-1]) {
day = 1;
month++;
if (month > 12) {
year++;
month = 1;
}
}
}
}
printTime();
counter = 0;
}
}
void printTime(){
bool pm = false;
int hoursP;
lcd.setCursor(5,2);
if (month < 10) {
if (day < 10)
lcd.print(" ");
}
lcd.print(month);
lcd.print("/");
lcd.print(day);
lcd.print("/");
lcd.print(year);
lcd.print(" ");
lcd.setCursor(4,3);
if (((hours > 0) && (hours < 10)) || ((hours > 12) && (hours <22)))
lcd.print(" ");
if (hours > 11)
pm = true;
if (hours > 12)
hoursP = hours-12;
else if (hours == 0)
hoursP = 12;
else
hoursP = hours;
lcd.print(hoursP);
lcd.print(":");
if (minutes < 10) lcd.print("0");
lcd.print(minutes);
lcd.print(":");
if (secs < 10) lcd.print("0");
lcd.print(secs);
if (pm)
lcd.print(" PM");
else
lcd.print(" AM");
}
bool readKey(int *keyPressed)
{
int row;
int col;
int hexN;
bool rv = false;
if (getKey(&row, &col)) {
hexN = hexConvert(row, col);
*keyPressed = hexN;
rv = true;
}
return rv;
}
bool getKey(int *row, int *col) {
bool pressed;
pressed = Keypad(row, col);
if (!pressed && !released) {
released = TRUE;
} else if (pressed && released) {
released = FALSE;
} else {
pressed = FALSE;
}
return pressed;
}
bool Keypad(int *row, int *col)
{
int i,j,k;
int temp;
bool pressed;
pressed = FALSE;
delay(DELAY);
for (j=0; j<4; j++) {
pinMode(C1-j, OUTPUT);
digitalWrite(C1-j, LOW);
delay(DELAY);
for (k=0; k<4; k++) {
temp = digitalRead(R1-k);
if (temp == LOW) {
*row = k;
*col = j;
pressed = TRUE;
break;
}
}
digitalWrite(C1-j, HIGH);
pinMode(C1-j, INPUT);
delay(DELAY);
if (pressed){
break;
}
}
return pressed;
}
int hexConvert(int r, int c)
{
int table[4][4] = {
{1,2,3,0xA},
{4,5,6,0xB},
{7,8,9,0xC},
{0xE, 0, 0xF, 0xD}
};
return table[r][c];
}
int inputData(char *strP){
bool enter=false;
bool keyPressed = false;
int rv = 0;
int key;
int digit = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*:Backspace #:Enter");
lcd.setCursor(0,1);
lcd.print(strP);
lcd.setCursor(0,2);
while (!enter) {
keyPressed = readKey(&key);
if (keyPressed == true)
{
if (key < 10) {
lcd.print(key);
rv = rv * 10 + key;
digit++;
} else if (key == 15) {
break;
} else if (key == 14) {
rv = rv / 10;
digit--;
lcd.setCursor(digit,2);
lcd.print(" ");
lcd.setCursor(digit,2);
}
}
}
return rv;
}