Arduino LCD Clock (Displaying 5's After each figure)

Hey Guys
I need a little guidance with my current project that uses an RTC to hold the time and display it onto a LCD.
I have compiled the code fairly well it seems but my only error comes across as placing 5's after each unit of time. (see attachment)

Here is the code I used to upload the time to the RTC

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

void setDateDs1307(
  byte second,     //0-59
  byte minute,     //0-59
  byte hour,       //1-23
  byte dayOfWeek,  //1-7 1=Mon, 7=Sun
  byte dayOfMonth, //1-28/29/30/31
  byte month,      //1-12
  byte year       //0-99
)    
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}

void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  pinMode(13, OUTPUT);

  second = 30;
  minute = 10;
  hour = 16;
  dayOfWeek = 3;
  dayOfMonth = 4;
  month = 1;
  year = 17;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop()
{
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

And here is the code I used to display the time

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define DS1307_I2C_ADDRESS 0x68
#define I2C_ADDR    0x3F
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

void getDateDs1307(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year)
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  *second      = bcdToDec(Wire.read() & 0x7f);
  *minute      = bcdToDec(Wire.read());
  *hour        = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek   = bcdToDec(Wire.read());
  *dayOfMonth  = bcdToDec(Wire.read());
  *month       = bcdToDec(Wire.read());
  *year        = bcdToDec(Wire.read()); 
}

void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  lcd.begin(16, 2);
}

void loop()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  String s, m, d, mth, h;

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  if (second < 10) { s = "0" + String(second); } else { s = String(second); }
  if (minute < 10) { m = "0" + String(minute); } else { m = String(minute); }
  h = String(hour);
  if (dayOfMonth < 10) { d = "0" + String(dayOfMonth); } else { d = String(dayOfMonth); }
  if (month < 10) { mth = "0" + String(month); } else { mth = String(month); }

  char* days[] = { "NA", "Mon", "Tue", "Wed", "Thu", "Wed", "Thu", "Fri", "Sat", "Sun"};

  lcd.clear();
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.setCursor(4,0);
  lcd.print(h + ":" + m + ":" + s);
  lcd.setCursor(1,1);
  lcd.print(String(days[dayOfWeek]) + " " + d + "/" + mth + "/20" + year);
  delay(1000);
}

Any help is much appreciated and Happy New Year!

15879193_1609436329083628_1142171933_n.jpg

You really should quit pissing away resources using the String class. sprintf() is still way more resource-intense a function than you need, but it uses less code space and less SRAM than the String class.

I don't see anything that would explain the '5' on the end of each String, but, you would need to print the String instances BETWEEN delimiters, to see where the 5s are coming from.

By the way,

void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

Those variables seem pretty useless.

Don't use the String class... you'll have an happier 2017 :slight_smile:

assuming all is set up appropriately, this will do the same without messing around with your limited arduino memory

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define DS1307_I2C_ADDRESS 0x68
#define I2C_ADDR    0x3F
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

const char* days[] = { "NA", "Mon", "Tue", "Wed", "Thu", "Wed", "Thu", "Fri", "Sat", "Sun"};

byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}

void getDateDs1307(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  *second      = bcdToDec(Wire.read() & 0x7f);
  *minute      = bcdToDec(Wire.read());
  *hour        = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek   = bcdToDec(Wire.read());
  *dayOfMonth  = bcdToDec(Wire.read());
  *month       = bcdToDec(Wire.read());
  *year        = bcdToDec(Wire.read());
}

void setup()
{
  Wire.begin();
  lcd.begin(16, 2);
}

void loop()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

  lcd.clear();
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.setCursor(4, 0);

  //  lcd.print(h + ":" + m + ":" + s);
  lcd.print(hour);
  lcd.print(":");
  if (minute < 10) lcd.print("0");
  lcd.print(minute);
  lcd.print(":");
  if (second < 10) lcd.print("0");
  lcd.print(second);

  lcd.setCursor(1, 1);
  // lcd.print(String(days[dayOfWeek]) + " " + d + "/" + mth + "/20" + year);

  lcd.print(days[dayOfWeek]);
  lcd.print(" ");
  if (dayOfMonth < 10) lcd.print("0");
  lcd.print(dayOfMonth);
  lcd.print("/");
  if (month < 10) lcd.print("0");
  lcd.print(month);
  lcd.print("/20");
  lcd.print(year);

  delay(1000);
}

Thanks for your help but it still displays 5's after every value, maybe the problem lies in actually uploading the time to the RTC?
Its annoying as I can't find any part of the code that would cause this.

have you checked a basic code using a standard library for the DS1307? setting and printing time to the console, not to the LCD (just to ensure your clock is working).

Does your LCD have an IDC backpack? how is it wired?

Its annoying as I can't find any part of the code that would cause this.

The first thing to do, then, is determine where the extra 5 comes from. Serial.print() the values between delimiters. If there is no extra 5 on the end, then the problem is with the LCD. If the extra 5 is there, then the problem is with the Arduino code. Divide and conquer.

So I think the problem lies in

byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}

I have a feeling this is why it doesnt display the proper time

And what leads you to this conclusion?
It should be easy to make up a simple test sketch

a quick google on

byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

returns 5000+ results... that would be a lot of examples with broken code...

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  *second      = bcdToDec(Wire.read() & 0x7f);
  *minute      = bcdToDec(Wire.read());
  *hour        = bcdToDec(Wire.read() & 0x3f);

bcdToDec(0x7f) = 85
bcdToDec(0x3f) = 45
bcdToDec(0xff) = 165

You should check what Wire.read() returns, it looks like it returns 255 or maybe even -1.