Can int also be boolean?

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();
  }

You can use an int as a boolean if you decide that 0 = false and 1 = true.

FYI, the DS3231 supports 12-hours and 24-hours modes. Why not set it to 12-hours mode and read the AM/PM bit ?

See pages 11 and 12 of the datasheet

I don't understand your question...

It would be much easier if you used the modulo operator, like in this example:

uint8_t hour = 15;
bool am = hour < 12 ? true : false;
hour %= 12;
if ( hour == 0 ) hour = 12;

Serial.print( hour );
Serial.println( am ? (" AM") : (" PM") );

That would show "3 PM"

Maybe this is not the answer to your question and maybe you want to reinvent the wheel by yourself :slight_smile: , but do you know the Time Library? there are also functions to return the hour in 12 hour format.

I am pretty new to Arduino and have not learned that material yet. The math I am able to do right in the sketch and mentally know what is happening which is important to me to know. Most people would likely laugh at my sketch, and I will definitely submit it for critiquing, but learning how to code is not my strongest suit.

Still being new, I am nervous about adding too many libraries as I've read that they can sometimes conflict with one another opening a can of worms that I'm just not ready to eat.

As general practice, I never declare anything INT unless that is the precision I want to carry. As your programs get larger and RAM space becomes important, why throw it away?

I see a lot of examples of users declaring device pins as type INT. The program still functions but uses TWO BYTES to declare each pin. Using BYTE or UNSIGNED CHAR uses only one byte. A pin is in fact UNSIGNED because there is no such thing as a negative pin no.

rmetzner49:
As general practice, I never declare anything INT unless that is the precision I want to carry. As your programs get larger and RAM space becomes important, why throw it away?

I see a lot of examples of users declaring device pins as type INT. The program still functions but uses TWO BYTES to declare each pin. Using BYTE or UNSIGNED CHAR uses only one byte. A pin is in fact UNSIGNED because there is no such thing as a negative pin no.

LIKE!

Wherever did you get the idea that writing

Serial.print(' ');
Serial.print('-');
Serial.print(' ');

over and over again , was a good idea ?

michinyon:
Wherever did you get the idea that writing

over and over again , was a good idea ?

Trial and error. I was very dissatisfied with the way the data was being displayed in serial monitor so I restructured it accordingly. I took alot of those out, but they were handy.