RTC output formatting problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
gkozi
 
Posts: 4
Joined: Mon Jan 13, 2014 11:31 am

RTC output formatting problem

Post by gkozi »

I'm trying to simply output date and time to and LCD via i2c backpack from the RTC on the datalogger shield.

Heres the code:
#include <LiquidCrystal.h>
#include "Wire.h"
#include "RTClib.h"

RTC_DS1307 RTC;
LiquidCrystal lcd(0);

void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
lcd.begin(16, 2);
pinMode(10, OUTPUT);
}
void loop ()
{
DateTime now = RTC.now();

lcd.setCursor(0,0);


//curent time
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second()), DEC;


}

Ok, first problem is the seconds count from :1, :2, :3,...etc. for the first minute after uploading, then after :59th sec. count :09,:19,:29,:39, :49,....etc until :10sec then increment as normal.
Second, not really a problem, but when the minutes are in single digits there is no 0 in front, for example: 1:9:14 not 1:09:14. any help is very appreciated, I'm new to this and have very limited coding knowledge. Thanks again!

User avatar
Franklin97355
 
Posts: 23902
Joined: Mon Apr 21, 2008 2:33 pm

Re: RTC output formatting problem

Post by Franklin97355 »

Both problems are the same in that you need to write code to tell the display if the number is less than 10 it needs to add the leading 0.

User avatar
jigsawnz
 
Posts: 180
Joined: Mon Mar 12, 2012 10:17 pm

Re: RTC output formatting problem

Post by jigsawnz »

This will probably solves both problems.

Code: Select all

if ( now.hour() < 10 )
    lcd.print( '0' );  
lcd.print(now.hour(), DEC);
lcd.print(':');
if ( now.minute() < 10 )
    lcd.print('0');
lcd.print(now.minute(), DEC);
lcd.print(':');
if ( now.second() < 10 )
    lcd.print( '0' );
lcd.print(now.second()), DEC;
good luck

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”