LCD clock code

I’ve been trying to figure this out for a couple of days but I know nothing about reading or writing code. Six months ago I didn’t know about Arduino.

I built this LCD clock and it works. I wanted to change the code to have a leading zero if the seconds were less than 10. (also minutes and hours). It adds the leading zero but it adds it even for over 10 (:024). It seems my if statement isn’t working.

This is the code (sketch) I’m using.

//RTC library link: https://github.com/jcw/rtclib
//LCD I2C library link: http://www.dfrobot.com/wiki/index.php/I2C/TWI_LCD1602_Module_(Gadgeteer_Compatible)_(SKU:_DFR0063)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>

#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif

uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};

RTC_DS1307 RTC;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.print(“Initialising…”);
lcd.createChar(2, clock);
Wire.begin();
RTC.begin();

// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(DATE, TIME));
}

void loop()
{
lcd.clear();
DateTime now = RTC.now();
lcd.printByte(2);
lcd.print(" ");
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);
lcd.setCursor(0, 1);
lcd.print(now.month(), DEC);
lcd.print(’/’);
lcd.print(now.day(), DEC);
lcd.print(’/’);
lcd.print(now.year(), DEC);
lcd.print(’ ');
delay(1000);
}

Could someone explain what I’m doing wrong.

Thanks

Bob

Could someone explain what I’m doing wrong.

if (now.hour()<10);
if (now.minute() < 10);
if (now.second()<10);

Remove the semicolons from after each of the conditional statements.

You gotta be kidding me - that was too easy. I've looked at a dozen 'if statements' and never noticed that they don't have the semicolons.

Thanks cattledog, you taught me something today. First of my many lessons.

Bob

Removing the semicolons worked great so I got brave and made a few more changes. I got it to print “AM or PM” at the correct time of day but I wanted it to read 1:15 PM instead of 13:15 PM. I think I’m close because the “if” statement seems to work. I think the problem is in my formula.

(now.hour()==(now.hour()-12));

It complies OK but my hours are still 12 hours too much.
Can someone tell me what I’m doing wrong?

void loop()
{
lcd.clear();
DateTime now = RTC.now();
//lcd.printByte(2);
lcd.print("TIME ");
if (now.hour()>12)
(now.hour()==(now.hour()-12));
// if (now.hour()<10)
// lcd.print(“0”);
lcd.print(now.hour(), DEC);
lcd.print(’:’);

Thanks

Bob

(now.hour()==(now.hour()-12));This tests the value of now.hour() which is not what you want to do. I assume that you meant to use = rather than ==, but that is wrong too because it would try to set the value of now.hour()

Instead, test whether now.hour() is greater than 11 and if so print now.hour() - 12 otherwise print now.hour()

UKHeliBob:
Instead, test whether now.hour() is greater than 11 and if so print now.hour() - 12 otherwise print now.hour()

Won't that make the hour after 12 noon show as zero? I think you need to test for greater than 12 rather than 11.

Also, the sketch needs to check when now.hour() is zero, and print 12 (for the hour after midnight).

Yes, the maths logic needs more work but the principle is sound

I've never used the RTC library that you have, but the one I do use has a setting for 12 or 24 view- no need to do any sums.

Are you sure yours doesn't have such a feature?

The testing for greater than 12 seemed to work fine. It's 3 AM here so I changed my computer to PM and re-downloaded the sketch. That worked too. Then I changed the time to 1 AM and now the Arduino clock won't change. I changed the computer clock a couple more times and it seems the upload won't read the computer time. I thought it was supposed to read the computer time each time the sketch is uploaded.

The RTC I'm using has a place for a coin cell battery but I don't have one installed.

I don't know if my library has the 12 or 24 view. How can I check that?

One thing I've learned is I gotta learn a lot more about this programming stuff. Is there a good book or website you can suggest where I can learn more? I'm sure you guys weren't born with this talent. I really would like to learn. I'm 73 but I think I can still learn new tricks.

Thanks

Bob

Try using an additional variable

  bool isPM = false;
  if (now.hour()>12) {
   byte h = now.hour()-12;
   if (h<10) lcd.print("0");
   lcd.print(h, DEC);
   isPM = true;
  } else {
   if (now.hour()<10) lcd.print("0");
   lcd.print(now.hour(), DEC); 
  }

Then after printing the seconds add

lcd.print (isPM? " PM":" AM");

These lines should tell the RTC to set its time to the compilation time:

   // following line sets the RTC to the date & time this sketch was compiled
   RTC.adjust(DateTime(__DATE__, __TIME__));

So what you’re doing should work fine.

But you can afford to buy a battery. This will prevent the RTC from losing the time when you don’t connect your Arduino to the computer. You’ll have to initialize the time using one of the example sketches of the library, once for all (until the battery wears out)