[SOLVED] Displaying date on LCD with DS1307 using button inputs

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();
  }

If anyone could assist that'll be great. Thanks!

here's my code:

Start by editing your post to put your code between code brackets instead of

quote

brackets.

Don

Sorry for the newbie tendency haha. I formatted the code better now

I finally found the issue. Apparently, the LCD displayed the date for pins 6,7,8 but not for pins 9,10,11(the one shown in the code). Any insight or explanation as why this happened would be great.

Any insight or explanation as why this happened would be great.

Because there is a conflict in the use of pin 11.

Don

Okay. So what would be the cause of this conflict? Could it be the 13K pull-up resistors I used for my push-buttons? Or does it have something to do with the Arduino?

Also, I played around with the pins config.
Good means pins worked perfectly:
Bad means the pins cause some type of screen malfunction onto the LCD (in this case "conflicts") :

Good = Pins 0,1,6,7,8,9,10
Bad = Pins 2,3,4,5,11,12,13

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

I'm not sure about pin13, but it may have to do with the attached led indicator and its resistance pulling the voltage low.

Okay. So what would be the cause of this conflict?

The conflict is that you have chosen to use the same pin for both the push-button and for the LCD.

Bad = Pins 2,3,4,5,11,12,13

Pins 2,3,4,5,11,and 12 are 'bad' because you are already using them for the LCD.
Pin 13 may be bad because the on-board LED is connected to that pin.

Don

Ah makes sense now. Thanks