Hello everyone,
I am new to this forum. I just started arduino a month and a half ago because my dad recommended it and have not turned back since.
Anyway I working on a LCD display using the DS1307 that sets the time and date using push-buttons. Fortunately, I was able to get the time to display on the LCD, but when it came to the date, it would not appear on the LCD display just like my time section of code did.
I tried to removed the "setting time" section of code so I could isolate solely on the fixing the date section, but still having problems.
If anyone could assist that'll be great. Thanks!
here's my code:
// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h> //I2C Library
//Only setting date in this code:
const int dateButtonPin = 9;
const int monthButtonPin = 10;
const int yearButtonPin = 11;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte ClockData[8];
byte secs;//00-59;
byte mins;//00-59;
byte hrs;//1-12 || 00-23;
byte date; //00-31
byte month;//01-12;
byte year;//0-99;
char buffer[10] = " ";
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
void setup()
{
// setup the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0,0); //moves cursor
lcd.print("Hi This is a"); //print text
lcd.setCursor(0,1);//moves cursor
lcd.print("Test"); //print text
delay(2000);// delay for about 2 secs.
pinMode(dateButtonPin,INPUT);
pinMode(monthButtonPin,INPUT);
pinMode(yearButtonPin, INPUT);
digitalWrite(dateButtonPin,HIGH);
digitalWrite(monthButtonPin,HIGH);
digitalWrite(yearButtonPin,HIGH);
Wire.begin();
}
void loop()
{
Wire.beginTransmission(0x68);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(0x68,7);
ClockData[1] = bcdToDec(Wire.read());//Read secs Reg
ClockData[2] = bcdToDec(Wire.read());//Read mins Reg
ClockData[3] = bcdToDec(Wire.read());//Read hrs Reg
ClockData[4] = bcdToDec(Wire.read());//Read Day Reg
ClockData[5] = bcdToDec(Wire.read());//Read date Reg
ClockData[6] = bcdToDec(Wire.read());//Read month Reg
ClockData[7] = bcdToDec(Wire.read());//Read year Reg
sprintf(buffer, "%02d-%02d-20%02d",ClockData[6],ClockData[5],ClockData[7]);
Serial.begin(9600);
Serial.println("Current Date: ");
Serial.println(buffer);
lcd.setCursor(0,1);//set cursor to line 0 position 0
lcd.clear();
lcd.print("Date: ");
lcd.print(buffer);
delay(1000);
inspect_buttons();
get_date();
}
void set_date()
{
Wire.beginTransmission(0x68);
Wire.write(5);
Wire.write(decToBcd(ClockData[5]));
Wire.write(decToBcd(ClockData[6]));
Wire.write(decToBcd(ClockData[7]));
Wire.endTransmission();
}
void get_date()
{
Wire.beginTransmission(0x68);
Wire.write(5);
Wire.endTransmission();
Wire.requestFrom(0x68, 3);//get 3 bytes
ClockData[5] = bcdToDec(Wire.read());
ClockData[6] = bcdToDec(Wire.read());
ClockData[7] = bcdToDec(Wire.read());
}
void inspect_buttons()
{
int dateButtonState, monthButtonState, yearButtonState;
dateButtonState = digitalRead(dateButtonPin);
monthButtonState = digitalRead(monthButtonPin);
yearButtonState = digitalRead(yearButtonPin);
if(dateButtonState == LOW){
settDate();
Serial.println("Set Date... ");
}
if(monthButtonState == LOW){
setMonth();
Serial.println("Set month... ");
}
if(yearButtonState == LOW){
setYear();
Serial.println("Set year... ");
}
}
void settDate()
{
date++;
if (date > 31)
{
date = 0;
}
ClockData[5] = date;
set_date();
}
void setMonth()
{
month++;
if (month > 12)
{
month = 0;
}
ClockData[6] = month;
set_date();
}
void setYear()
{
year++;
if (year > 99)
{
year = 0;
}
ClockData[7] = year;
set_date();
}