[SOLVED] how to make this run only once EVER

This is a bit of a generic question but I hope that there a way of achieving this on an arduino

for basically as a general code you have:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

where in 'setup' you would usually put in the initialisation code for your hardware/shield/etc

but assuming the hardware/shield/etc retains the initialisation parameters even after powered down, is there anyway to stop the arduino from running the initialisation code next time its powered up?

Store a flag in EEPROM indicating that the code has been run once. Check that flag first thing in setup().

gfvalvo:
Store a flag in EEPROM indicating that the code has been run once. Check that flag first thing in setup().

That is an option I thought of as well! :slight_smile:

As I said this is a generic (for now) question.

interested to see if anybody may have thought of alternate solutions to achieving this.

Where in 'setup' you would usually put in the initialisation code for your hardware/shield/etc

Assuming the hardware/shield/etc retains the initialisation parameters even after powered down, is there anyway to stop the Arduino from running the initialisation code next time its powered up?

Why do you think the hardware only needs to be initialised once ever, even after power down/up?

If that really is the case would initialising it every time do any harm?

If you only run the initialisation code once ever what happens if there is a hardware problem and it needs reinitialising?

Can you query the hardware to see if it has been initialised and only initialise it if not?

gfvalvo:
Store a flag in EEPROM indicating that the code has been run once. Check that flag first thing in setup().

Seems like the most sensible way to me too. :shrug:

When you power up the Arduino, check the state of the hardware. If it is not appropriate to the task at hand, initialize the hardware to make it so.

Any other approach is guaranteed to fail at some point.

Is there some way to ask the hardware if it has been initialized? If it has, skip that part of the initialization.

jremington:
When you power up the Arduino, check the state of the hardware. If it is not appropriate to the task at hand, initialize the hardware to make it so.

Any other approach is guaranteed to fail at some point.

johnwasser:
Is there some way to ask the hardware if it has been initialized? If it has, skip that part of the initialization.

Thank you for your input. That does indeed seem to be the more sensible way (where possible). if not the next best alternative is probably the eeprom solution