pcace
November 5, 2022, 10:02am
1
Hi there, i am quite new to strings in Arduino, read the man page but cannot figure out why this does not create a string like so: "12:13:41:32"
`String currTime = String(hours < 10 && '0' + hours + ':' + minutes < 10 && '0' + minutes + ':' + seconds < 10 && '0' + seconds);`
When i Serial.println(currTime) i just get a huge int number back...
Cheers and Thanks!
What does
`String currTime = String(hours < 10 && '0' + hours + ':' + minutes < 10 && '0' + minutes + ':' + seconds < 10 && '0' + seconds);`
this hours < 10 and this minutes < 10 and this seconds < 10 supposed to do?
String currTime =""
if ( hours < 10 )
currTime = "0" + String(currTime);
else {
currTime= string(hours);
}
currTime = currTime + ":";
///do the same with minutes and seconds.
might be or might not be a clue for you.
You code shows a lack knowledge of these things,
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
and
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
and
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
pcace
November 5, 2022, 10:42am
3
Thanks - but please look at that:
int main()
{
int value = 1;
//ternary
int check = value < 2 ? 1 : 0;
printf("%i", check);
//check inline
int check2 = value < 2 && 1;
printf("%i", check2);
return 0;
}
made this available here:
Online GDB is online ide with compiler and debugger for C/C++. Code, Compiler, Run, Debug Share code nippets.
my version + ternary operators should totally work... i guess...^^
Cheers
pcace
November 5, 2022, 10:45am
5
Please tell me if i am wrong - i am just starting with arduinos.
Is there a reason not to use this code above?
i mean your code does do the trick too - but with way more writing..
b707
November 5, 2022, 10:05pm
6
You are wrong if you think that your code in post #1 has something similar with example code in post #3 ...
Is the fact that your code doesn't work not enough reason not to use it?
gcjr
November 5, 2022, 10:11pm
7
consider
the time is 11:59:59
char s [80];
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (9600);
byte hr = 11;
byte min = 59;
byte sec = 59;
sprintf (s, " the time is %2d:%02d:%02d", hr, min, sec);
Serial.println (s);
}
// -----------------------------------------------------------------------------
void loop ()
{
}
system
Closed
May 4, 2023, 10:12pm
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.