Maybe my problem is simple, but it's hard for me.
I like programming, but I have limited programming knowledge.
I have a relatively large project in which I use the "String" format for many variables.
I often see comments recommending not using Strings for technical reasons.
So I decided to change my Strings to strings.
(strings: I think that's how they refer to variables of the const char* xxxx type) Is that correct?
I'm going to post a very simple code here that reflects my problem:
I have this:
String timerOn[] {"10:15:32", "14:22:47", "17:19:12"};
and I separate the values of hours, minutes and seconds like this:
hours = (timerOn[i].substring(0, 2)).toInt();
minutes = (timerOn[i].substring(3, 6)).toInt();
seconds = (timerOn[i].substring(6, 8)).toInt();
but I can't separate the values using this:
const char* timerOn[] {"10:15:32", "14:22:47", "17:19:12"};
I can only get the values of each index of timerOn[].
I appreciate any help that teaches me how to separate the values of hours, minutes and seconds
from the content of each index of timerOn[].
Here is what I wrote using String and string.
Of course the values printed by string were "forced", because I don't know how to put them in a variable.
Below is also a copy of the code I used in wokwi.
Tks for all
//-----------------------------------------------------------------
void setup() {
Serial.begin(115200);
delay(100);
my_old();
Serial.println("");
my_new();
}
//-----------------------------------------------------------------
void loop() {}
//-----------------------------------------------------------------
void my_old() {
String timerOn[] {"10:15:32", "14:22:47", "17:19:12"};
int hours;
int minutes;
int seconds;
for (int i = 0; i < 3; i++) {
Serial.println(timerOn[i]);
}
for (int i = 0; i < 3; i++) {
hours = (timerOn[i].substring(0, 2)).toInt();
minutes = (timerOn[i].substring(3, 6)).toInt();
seconds = (timerOn[i].substring(6, 8)).toInt();
Serial.print("hours: " ); Serial.print(hours);
Serial.print(" minutes: " ); Serial.print(minutes);
Serial.print(" seconds: " ); Serial.println(seconds);
}
}
//-----------------------------------------------------------------
void my_new() {
const char* timerOn[] {"10:15:32", "14:22:47", "17:19:12"};
for (int i = 0; i < 3; i++) {
Serial.println(timerOn[i]);
}
Serial.print("hours: " ); Serial.print(10);
Serial.print(" minutes: " ); Serial.print(15);
Serial.print(" seconds: " ); Serial.println(32);
Serial.print("hours: " ); Serial.print(14);
Serial.print(" minutes: " ); Serial.print(22);
Serial.print(" seconds: " ); Serial.println(47);
Serial.print("hours: " ); Serial.print(17);
Serial.print(" minutes: " ); Serial.print(19);
Serial.print(" seconds: " ); Serial.println(12);
}