TimeLib.h : Combine hour() and minute() into HHMM format

Hey folks, anybody happen to know why my hhmm variable prints 0 to prompt?

#include <TimeLib.h>
.....
int hhmm = (hour() * 100) + minute();
.....
Serial.println(hhmm); //it prints 0.

int hhmm = (int(hour()) * 100) + int(minute()); // doesn't work either

Thanks!!

use code tags and post full example we could compile (➜ Don't post snippets (Snippets R Us!) )

what do you see if you print

#include <TimeLib.h>

...

Serial.println(hour()); 
Serial.println(minute()); 

if both are 0 then you have your answer...

PS/ how do you get the time in the first place?

Hi
I don't know if I understood your difficulty well.
But test this code and see if it's what you need.

I used the DEFAULT_TIME += 60; to simplify the example, but
it is not the correct way to count time.

I also used a delay of 400 just to make it easier to see the serial.

#include <TimeLib.h>
int hhmm;
unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
void setup() {
  Serial.begin(115200);
  delay(10);
}

void loop() {
  setTime(DEFAULT_TIME);
  delay(400);
  DEFAULT_TIME += 60;
  hhmm = (hour() * 100) + minute();
  Serial.println(hhmm); 
}

cheers very fair mate thanks!

time is enter through serial, hour()/minute() print just fine
23:1:42:0:230
hour(),: minute(),: second(),: hhmm,: H,M

#include <TimeLib.h>

time_t t = now();
int H, M;
int hhmm = (hour() * 100) + minute();

unsigned long currentTime = millis();
unsigned long previousTime = 0;

//int hhmm = (int(hour()) * 100) + int(minute());
/*
int hh = hour();
String hhStr = (hh < 10) ? "0" + String(hh) : String(hh);

int mm = minute();
String mmStr = (mm < 10) ? "0" + String(mm) : String(mm);

int hhmm = (hhStr + mmStr).toInt();

float hhour = hour();
float mminute = minute();

int hh = hour();
String hhStr;
if (hh < 10) {
  hhStr = "0" + String(hh);
} else if {
  hhStr = String(hh);
}

int mm = minute();
String mmStr;
if (mm < 10) {
  mmStr = "0" + String(mm);
} else if {
  mmStr = String(mm);
}

String hhmmStr = hhStr + mmStr;
int hhmm = hhmmStr.toInt(); */


void setup() {
Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
  
  // Read the hour and minute from the serial port
  while (Serial.available() < 4) {
    Serial.println("Enter the time in HHMM format: ");
    delay(1000);
  }
  H = Serial.parseInt();
  M = Serial.parseInt();
  H = H / 100;
  M = M % 100;
    setTime(H, M, 0, 1, 1, 2022);

    Serial.print(hourFormat12());
  Serial.println(isAM() ? "AM" : "PM");
}

void loop() {
    currentTime = millis();
    if (currentTime - previousTime >= 3000) {
    previousTime = currentTime;
    //Serial.print(hourFormat12());
    //Serial.print(":");
    Serial.print(hour());
    Serial.print(":");
    Serial.print(minute());
    Serial.print(":");
    Serial.print(second());
    Serial.print(":");
    Serial.print(hhmm);
    Serial.print(":");
    Serial.print(H);
    Serial.println(M);
}
}

Suppose you enter "1234" to indicate the time 12:34.

The first parseInt assigns the value 1234 to variable 'H'.

I will guess that is not what you want to do.

indeed it is not, happen to have a remedy? I'm very new with arduino, chatgpt has paved most of the way haha

The whole thing hinges on concepts and definitions. The way to solve a problem is to first fully define it. Then, develop solutions using concepts. The definition is, we want to separate 4 digits into 2 sets of 2 digits.

In coding there can be almost infinite ways to express a solution. But a good starting point for a beginner is an expression that most closely follows a mathematical one. For many reasons.

I saw an example of this in someone else's post, did it not work? Or, are you unsure how it works?

A unique difference between a character string e.g. "1234" and a number, is that it is computationally faster to manipulate them than with a decimal representation of a digital number, 1234. With strings (char arrays) it is only a matter of moving characters. In this case you are already off the hook because it is a user entered string.

However, I recommend analyzing the input string before converting it to a number. If it has invalid characters, e.g. "12P4" some numeric conversions will produce an invalid result, and sometimes with an error, but often checking the error is optional and some coders don't bother.

Also, if you are entering a time of day, do you really want to try and process the number 4321 for example?

Note that the Serial class has a read() method, so you can read the four digits in, one at a time, using the available() method to check first to see (you guessed it) if there are any characters available.

chatGPT probably chose parseInt() because among probably complex reasons, that many people use it for input. Also that it bypasses the need for programmed solutions. Remember, AI is a mimic.

Well I've thoroughly enjoyed reading that through 3 times now. Much thanks for the wisdom. "someone else's post, did it not work?" - you're referring to the unix #? Certainly an option, one that I've certainly considered might have saved some headaches, but alas.

error checking isn't a worry for now, is 4321 a potential error, you mean right? only tryna process upto 2359 for now!

as for the last two paragraphs, I've got some more reading to do. you've sent me down the right path, but I'm sure I'll be back :saluting_face:

One very cheesy way, but should work

pseudocode, key is a one character input, converted to numeric

hour = key * 10
hour = hour + key
min = key * 10
min = min + key

You can convert to numeric like this

int keyValue = Serial.read() - '0';