Hello and sorry that this is most likely a noob question, but I just can't figure it out.
I am trying to grab some time data from the NTP servers, pull the hour and minutes out into char variables and then concatenate them into a single variable that I can print on an OLED.
I can do everything except concatenate the chars into a single variable that I can work with. Here is some sample code that I am using as a POC:
When I try and compile the sketch I get this error:
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, Core 1, Core 1, None, Disabled"
C:%path%\Char_Format_test.ino: In function 'void loop()':
Char_Formate_test:11:28: error: incompatible types in assignment of 'char' to 'char [20]'
displayTime = formatTime();
^
C:%path%\Char_Format_test.ino: In function 'char formatTime()':
Char_Formate_test:30:10: error: invalid conversion from 'char*' to 'char' [-fpermissive]
return HH;
^~
exit status 1
incompatible types in assignment of 'char' to 'char [20]'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I have modified the size of the variable displayTime but I get different errors, I have also assigned a size to the formatTime function but that didn't work ether.
I am sure it's just a combination of getting the definitions right but I am at a lose. Thanks in advance.
your function needs to return a pointer to the c-string (the null terminate char array), not just a char so it should be
char* formatTime() {
but your real issue that the HH variable is going to disappear when you leave the function ➜ is it's time to read about scope
usually you pass the buffer into which you want the concatenation to happen to the function so that the caller is responsible for allocating (and freeing) the memory.
try this code which displays random time formatted as HH:MM:SS
click to see the code
char* buildTimeString(char * dest, size_t maxSize) {
// here I use random values, obviously you would extract that from your time source like NTP
int hour = random(0, 24);
int min = random(0, 60);
int sec = random(0, 60);;
snprintf(dest, maxSize, "%02d:%02d:%02d", hour, min, sec);
return dest;
}
void setup() {
Serial.begin(115200);
}
void loop() {
char tmpBuffer[9]; // we allocate space, enough for HH:MM:SS 8 chars + 1 trailing null
Serial.println(buildTimeString(tmpBuffer, sizeof tmpBuffer));
delay(1000);
}
So it looks like a NULL value which would make sense to your other comment about scope. But I don't understand why.
The "return HH" should pass the value back to "displayTime" and it shouldn't matter that HH goes away because it has already passed that value off and can be forgotten until the next time it's needed.
I am looking at this code as a guide:
void setup(){
Serial.begin(9600);
}
void loop() {
int i = 2;
int j = 3;
int k;
k = myMultiplyFunction(i, j); // k now contains 6
Serial.println(k);
delay(500);
}
int myMultiplyFunction(int x, int y){
int result;
result = x * y;
return result;
}
In this the "result" variable is only at in the function but is able to pass its value back out to k without have the global scope.
I think I am doing the same thing, just with chars and not ints.
But I am most likely wrong, because it's clearly not working. Again thank you for your help.