Hi there.. i have a chronoDot v2.1 RTC that works fine.. however i need to connect it to a 4x3 keypad and use the keypad to set the time.. and i have no idea how to go about it.. anyone can help please? thanks lots!
this is the code i currently have..!
#include <Keypad.h> // Keypad library can be downloaded from the Arduino website
// & unzipped into the “libraries” folder of the Arduino software in your PC
#include <LiquidCrystal.h>
#include <Wire.h>
const byte ROWS = 4; //4 rows
const byte COLS = 3; //3 columns
char keys[ROWS][COLS] = {
{'1', '2', '3'}, // arrangement of keys
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {6, 1, 2, 4}; //which Arduino UNO pins connected to R0 – 3 of Keypad
byte colPins[COLS] = {5, 7, 3}; //which Arduino UNO pins connected to C0 - 2 of Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// initialise Keypad library
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
int seconds; //00-59;
int minutes; //00-59;
int hours;//1-12 - 00-23;
int day;//1-7
int date;//01-31
int month;//01-12
int year;//0-99;
void setup() {
Wire.begin();
lcd.begin(16, 4);
seconds = 00;
minutes = 48;
hours = 16;
day = 7;
date = 30;
month = 10;
year = 14;
//initChrono();//just set the time once on your RTC
lcd.print("Welcome!");
delay(3000);
lcd.clear();
}
unsigned char i;
char key;
void loop() {
get_time();
get_date();
display_time();
display_date();
delay(1000);
}
void display_time()
{
char buf[12];
lcd.setCursor(0, 0);
if (hours == 0)
{
lcd.clear();
}
lcd.print("Time ");
lcd.print(itoa(hours, buf, 10));
lcd.print(":");
if (minutes < 10)
{
lcd.print("0");
}
lcd.print(itoa(minutes, buf, 10));
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(itoa(seconds, buf, 10));
}
void display_date()
{
char buf[12];
lcd.setCursor(0, 1);
lcd.print("Date ");
if (month < 10) {
lcd.print("0");
}
lcd.print(itoa(month, buf, 10));
lcd.print("/");
lcd.print(itoa(date, buf, 10));
lcd.print("/");
if (year < 10) {
lcd.print("0");
}
lcd.print(itoa(year, buf, 10));
}
void initChrono()
{
set_time();
set_date();
}
void set_date()
{
Wire.beginTransmission(104);
Wire.write(3);
Wire.write(decToBcd(day));
Wire.write(decToBcd(date));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void get_date()
{
Wire.beginTransmission(104);
Wire.write(3);//set register to 3 (day)
Wire.endTransmission();
Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
day = bcdToDec(Wire.read());
date = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
void set_time()
{
Wire.beginTransmission(104);
Wire.write(0);
Wire.write(decToBcd(seconds));
Wire.write(decToBcd(minutes));
Wire.write(decToBcd(hours));
Wire.endTransmission();
}
void get_time()
{
Wire.beginTransmission(104);
Wire.write(0);//set register to 0
Wire.endTransmission();
Wire.requestFrom(104, 3);//get 3 bytes (seconds,minutes,hours);
seconds = bcdToDec(Wire.read() & 0x7f);
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0x3f);
}
void setHour()
{
hours++;
if (hours > 23)
{
hours = 0;
seconds = 0;
minutes = 0;
}
set_time();
}
void setMinutes()
{
minutes++;
if (minutes > 59)
{
minutes = 0;
}
seconds = 0;
set_time();
}
byte decToBcd(byte val)
{
return ( (val / 10 * 16) + (val % 10) );
}
byte bcdToDec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}