I am writing a sketch that frequently queries a DS3231 RTC clock to effect the triggering of events. I find this way to be easier for me than using timeAlarms libraries and is more hands on which helps my learning. I was able to effectively get my serial monitor to display in 12 hours, but I want that display to show the displayed items a specific way. With that said, I am wanting the AM or PM to be displayed "after the time on the same line. At the beginning of this block I have declared 2 int variables that are used to test if the now.hour reads 0 (zero) or 13, and if so, do the math to get back to displaying in 12 hour format. This works good.
In the beginning of the block, can I define those int variables to also (return?) a boolean state of true or false? If no, can someone suggest a simple method to declare something in the beginning of the block that I can refer back to with print() or println() later on in the block after hours, minutes, and seconds are displayed? Right now my serial monitor displays it as " - AM - 9:14:xx - ". Below is that block of code and TYIA for any help.
void loop()
{ //******************************************************************************//
//****** Setting Time and testing to display 24 hour time as 12 hour time ******//
//******************************************************************************//
DateTime now = RTC.now();
int twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
int zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
Serial.print('-');
Serial.print(' ');
if (now.hour() == 0){ // First we test if the hour reads "0"
Serial.print (F("AM"));
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
Serial.print(zeroHour); // if yes, serial print a "12" instead
}
else if (now.hour() >= 13){ // if no, Second we test if the hour reads "13 or more"
Serial.print (F("PM"));
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
Serial.print(twelveHour); // if yes, serial print that current hour minus 12
}
else
{
Serial.print (F("AM"));
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
Serial.print(now.hour(), DEC); // if no, Third we conclude that the am hours are being displayed correctly.
}
{
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
//Serial.print(F("am"));
//Serial.print(F("PM"));
Serial.println();
Edit - Sorry, I solved my own problem just a few minutes later. Here is the resulting code that produces 12 hour time - am or PM
newline date
void loop()
{ //******************************************************************************//
//****** Setting Time and testing to display 24 hour time as 12 hour time ******//
//******************************************************************************//
DateTime now = RTC.now();
int twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
int zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
if (now.hour() == 0) // First we test if the hour reads "0"
{
Serial.print(zeroHour); // if yes, serial print a "12" instead
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.println (F("AM"));
}
else if (now.hour() >= 13) // if no, Second we test if the hour reads "13 or more"
{
Serial.print(twelveHour); // if yes, serial print that current hour minus 12
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.println (F("PM"));
}
else
{
Serial.print(now.hour(), DEC); // if no, Third we conclude that the am hours are being displayed correctly.
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.println (F("AM"));
}
{
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.println(now.year(), DEC);
Serial.println();
}