do something once, but not on startup ( setup() )

I want Arduino to do something once, after PC prepares something. SO I can't put that code in setup(). I can put it in the loop() function in an if statement and after the first time it runs change a flag variable in the if statement so it wont run again, but not sure if this is the fastest or best way to do it.
Any opinions?

Using a flag variable to determine whether something has been completed is very common.

...R

Sure I just mean if there's a way to not have Arduino check the flag in every loop that would be faster. I know that's not a performance issue on PCs but don't know about Arduino.

stuka:
Sure I just mean if there's a way to not have Arduino check the flag in every loop that would be faster. I know that's not a performance issue on PCs but don't know about Arduino.

I can't think of any alternative.
Checking futile things in every iteration of loop() is normal :slight_smile: See for example how millis() is used in Several Things at a Time

...R

I don't think the overhead of a conditional is all that great, but couldn't you wait in "setup" until the PC has completed its bit, execute your one-time logic as part of "setup", and then pass control to "loop"?

How? the only way Arduino can know the PC is ready is to establish a Serial connection as far as I know. Then what could I do in the setup?

stuka:
I want Arduino to do something once, after PC prepares something. SO I can't put that code in setup().

Not really. Why would you think so? Setup doesn't have to finish the month it starts.

How? the only way Arduino can know the PC is ready is to establish a Serial connection as far as I know. Then what could I do in the setup?

Connection should be complete when Serial.begin() finishes. You don't print in setup?

You can do anything in setup() that you can do in loop(). However the general idea is that setup() is for something that needs to be done once before loop() starts.

It would be quite normal to have code in setup() which checks that there is working communication with a PC before setup() is allowed to finish.

If you put your serial code into a function it can be called from setup() and also from loop(). The examples in Serial Input Basics can be used like that.

...R

Robin2:
Using a flag variable to determine whether something has been completed is very common.

...R

... and checking it takes only about a millionth of a second.

You could get clever with function pointers, and not have to test any flags.

You could get clever with function pointers, and not have to test any flags.

Not have to explicitly check any flags. There needs to be a text that the function pointer is assigned a value (unless the pointer is initialized to something safe to call any time) before the function is called.

AWOL:
You could get clever with function pointers, and not have to test any flags.

I had not thought of that.

But would it be worth the trouble for the small time saving? Debugging function pointer errors can be fun.

And I presume a function pointer requires two "jumps" which may take just as long as the IF.

...R

Using class objects can fix that and overuse of globals.