Go Down

Topic: Convert 24 to12 hours on lcd Display. (Read 391 times) previous topic - next topic

BenBenBenBen

I am trying to convert 24 hours to 12 hours, I know there is a way to do it on bit six of the <RTClib.h> But I'm not sure about how to do that.


I think there is something wrong with the 14 lines when I compile this in the code that is not subtracting 12 hours from 24.

So if you can give me some hints that would be great.

Something wrong with the double equals in this bit of code.

/////////////////////////////////////////////////////////////
now.hour() == now.hour()-12;
 if (now.hour() == 0) {
 now.hour() == 12; // Day 12 PM
 }
 if (now.hour()<=9){
 lcd.setCursor(4,1);
 lcd.print("0");
 lcd.setCursor(5,1);
 lcd.print(now.hour(), DEC);
 }
 else {
 lcd.print(now.hour(), DEC);}}
 else { if(now.hour()==0){
 (now.hour()==12); // Night 12 AM
 }
//////////////////////////////////////////////////////////////






Code: [Select]

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

// Set the LCD address to 0x3F for a 16 chars and 2 line display // note: it may be different for your LCD please find it.
LiquidCrystal_I2C lcd(0x27,16,2); // Display  I2C 16 x 2
RTC_DS1307 RTC;


void setup()
{

  lcd.begin();
  lcd.backlight();
  lcd.clear();

 
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // Set the date and time at compile time
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }

   //for example to set January 27 2017 at 12:56 you would call:
     RTC.adjust(DateTime(2019, 2, 16, 12, 59, 0));

  //RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time
    // The default display shows the date and time
  int menu=0;
}

void loop()
{
 // We show the current date and time
  DateTime now = RTC.now();

 
 lcd.setCursor(0, 0);
 if (now.month()<=9){
 lcd.setCursor(0,0);
 lcd.print("0");
 lcd.setCursor(1,0);
 lcd.print(now.month(), DEC);
 }
 else {
 lcd.print(now.day(), DEC);}
 
 lcd.setCursor(3, 0);
 lcd.print(now.day());
 lcd.setCursor(12,0);
 lcd.print(now.year(), DEC);
 lcd.setCursor(4,1);
 
 {if (now.hour()>=12){
 lcd.setCursor(13,1);
 lcd.print("PM");
 lcd.setCursor(4,1);
 now.hour() == now.hour()-12;
 if (now.hour() == 0) {
 now.hour() == 12; // Day 12 PM
 }
 if (now.hour()<=9){
 lcd.setCursor(4,1);
 lcd.print("0");
 lcd.setCursor(5,1);
 lcd.print(now.hour(), DEC);
 }
 else {
 lcd.print(now.hour(), DEC);}}
 else { if(now.hour()==0){
 (now.hour()==12); // Night 12 AM
 }
 lcd.setCursor(13,1);
 lcd.print("AM");
 lcd.setCursor(4,1);
 if (now.hour()<=9){
 lcd.setCursor(4,1);
 lcd.print("0");
 lcd.setCursor(5,1);
 lcd.print(now.hour(), DEC);
 }
 else {
 lcd.print(now.hour(), DEC);}} }
 lcd.setCursor(6,1);
 lcd.print(":");
 lcd.setCursor(7,1);
 if (now.minute()<=9){
 lcd.setCursor(7,1);
 lcd.print("0");
 lcd.setCursor(8,1);
 lcd.print(now.minute(), DEC);
 }
 else {
 lcd.print(now.minute(), DEC);}

 lcd.setCursor(9,1);
 lcd.print(":");
 lcd.setCursor(10,1);
 if (now.second()<=9){
 lcd.setCursor(10,1);
 lcd.print("0");
 lcd.setCursor(11,1);
 lcd.print(now.second(), DEC);
 }
 else {
 lcd.print(now.second(), DEC);}

delay (1000);
 lcd.clear();
}
[\code]



6v6gt

You have to post your entire sketch for any meaningful comment on it.
What I can say is this:

Code: [Select]
now.hour() == now.hour()-12;

It is bad news if now.hour() is already less than 12 (say early in the morning).

david_2018

You have to post your entire sketch for any meaningful comment on it.
What I can say is this:

Code: [Select]
now.hour() == now.hour()-12;

It is bad news if now.hour() is already less than 12 (say early in the morning).
Might work with the modulo operator

Code: [Select]
now.hour() = now.hour()%12;

in any case, using the double equal sign isn't going to assign the value to the variable, there are a few more instances of that in the code posted.

BenBenBenBen

 I don't know what to say to that question either?
 Anyway here is the webpage where I got the idea to try to take and convert 24 hours to 12.
http://roboticadiy.com/how-to-make-12-hour-format-clock-ds1302-with-arduino/


david_prentice

#4
Feb 17, 2019, 01:44 am Last Edit: Feb 17, 2019, 01:47 am by david_prentice
Are you using a real hardware RTC chip e.g. DS1302, DS1307, DS3231, ... ?

These can be configured for 12-hour or 24-hour format by setting a hardware register bit.

If you are using a software RTC,   you can convert from 24-hour in software.

If 0 - 11 it is AM e.g. 12AM, 1-11AM
If 12 - 23 it is PM e.g. 12PM, 1-11PM

The website seems to do the conversion properly.

12-hour format seems a very un-intuitive format but hey-ho some countries still use bushels.

David.

BenBenBenBen

 I am using the DS 1302 module,   I do realize you can use the register bit (#6).  The problem is I do not know where this bit is in the hardware register. 

I still think that the snippet of code in the first question, I still think the double equals, isn't converting the 24 to 12 correctly.  Why I think this is because and the code on the webpage they only have one equals in for some reason the Arduino sketch,  would not compile and tell I added the double equals.

Code: [Select]

now.hour() == now.hour()-12;
 if (now.hour() == 0) {
 now.hour() == 12; // Day 12 PM
 }
 if (now.hour()<=9){
 lcd.setCursor(4,1);
 lcd.print("0");
 lcd.setCursor(5,1);
 lcd.print(now.hour(), DEC);
 }
 else {
 lcd.print(now.hour(), DEC);}}
 else { if(now.hour()==0){
 (now.hour()==12); // Night 12 AM
 }
[\code]

bperrybap

Code: [Select]
now.hour() == now.hour()-12;

I'm assuming you think that this modifies the value of now.hour()
But that isn't what that does.

It takes the return value from now.hour() subtracts 12, then compares that value to the return value of now.hour()
Then does nothing.

Then you  do this:
Code: [Select]
if (now.hour() == 0) {
 now.hour() == 12; // Day 12 PM
 }

Which once again is not doing what you likely think it is doing.

It is comparing the return value from now.hour() to 0 and if is equal to 0, then it compares the return value of now.hour() to 12 and then does nothing.

The net result is that all of this:
Code: [Select]
now.hour() == now.hour()-12;
 if (now.hour() == 0) {
 now.hour() == 12; // Day 12 PM
 }


Does nothing but waste cycles.

For what you want you need to be using a local variable and need to assign a value to it based on your checks.
i.e. something like:

Code: [Select]

int hour;

hour = now.hour()-12;
 if (hour == 0) {
 hour = 12; // 0 is 12 am
 }


Then print out the value of hour not now.hour()

--- bill





Paul__B

So do you understand what the single and double "equals" mean in "C'?

A single "=" is an assignment; meaning set the variable on the left to the value of the expression evaluated on the right.

A double, "==" is a comparison; meaning evaluate the expression on the right and compare it to the evaluated expression on the left, generating a Boolean truth value which may then be used to control a decision such as an "if" statement or could in itself be assigned to a variable on the left of an "=".  If there is no such decision or assignment as in the construct
Code: [Select]
now.hour() == 12; // Day 12 PM
 or
Code: [Select]
(now.hour()==12) then that value is simply void - nothing happens as a result of the statement.
[/code]

Go Up