(1)
if(Serial.available() > 0)
{
...
}
(2)
if(Serial.available() <= 0) return;
{
...
}
Is there a difference between the two approaches??
(1)
if(Serial.available() > 0)
{
...
}
(2)
if(Serial.available() <= 0) return;
{
...
}
Is there a difference between the two approaches??
With the second approach, the only way code after the if statement will be executed is if your sketch receives serial data.
So it has unintended consequences.
The first approach is the way to go about it
Serial Input Basics - simple reliable ways to receive data.
...R
Thanks for the reply.
then, is it the same in both ways if I only want to excute {...} after received data?
(speed, efficiency,..etc.)
With the very little information you have provided I can't see how it would make any difference.
I sometimes write a function like this pseudo code
void myFunction() {
if (xx is not ready) {
return;
}
// other code
}
simply because I think it makes things clearer when I am reading the program.
I have never written a program where the marginal performance difference (if there is one) between the two approaches would be important - the time difference is probably measured in nano-seconds rather than microseconds.
Writing code so it can be easily understood is almost always more important than being clever.
...R
The second approach won't work if you also want to check your other serial devices.
Or indeed, any other devices.
AWOL:
The second approach won't work if you also want to check your other serial devices.Or indeed, any other devices.
Doesn't that depend on where the "return" brings you?
...R