Determining first time code is run

Hi there,

I am trying figure out a way to determine if this is the first power on of the board. Basically I want to run a configuration part if it hasn't been configured before. I was thinking of checking EEPROM to see if it 0xFF, but that seems not very reliable. Is there a way I can write to EEPROM a specific value when I program the device?

I am working with an Arduino UNO board.

Best,
Ilya

(deleted)

reviverco:
I am trying figure out a way to determine if this is the first power on of the board.

You will have to power the board in order to program it. And it may well have been powered up for testing during manufacturing. i think it would be safe to assume that when your program runs it is NOT the first time the board is powered up.

On the other hand if what you want is for your program to go through a one-time procedure every time the Arduino is started then just put that code in setup().

...R

I am looking for the user to go through a one time set up. Basically if the chip receives certain inputs (e.g. commands through serial) setup is complete and I can write to EEPROM the set up parameters. I want to wait for those commands on only if it hasn't been done before. So I need a way to tell if a sequence was run before on this board.

I need a way to tell if a sequence was run before on this board.

Write a small program to write specific values to several EEPROM locations, Have your main program check whether the values are present. If so, run the user setup portion of your program, save the values and change the values previously written by the small program.

When your main program is run again check the special values. They will not be present so the user setup portion of the program can be skipped.

Another option: When you do the one-time setup and determine your configuration parameters, do a one or two byte checksum on them. Even better, do a CRC. Then write both the parameters and checksum (CRC) to EEPROM.

So, every time your sketch powers up and runs, it should first read the EEPROM addresses where the parameters and checksum / CRC are stored. Then, recompute the checksum / CRC on the retrieved "parameters". If that computed value doesn't match the one you read, then it's the first time you've run and you should do the one-time setup.

So, before sending it out to your client you'd upload and run a short sketch to write a value to EEPROM, and then upload the real sketch which would look at that value.

reviverco:
Hi there,

I am trying figure out a way to determine if this is the first power on of the board. Basically I want to run a configuration part if it hasn't been configured before. I was thinking of checking EEPROM to see if it 0xFF, but that seems not very reliable. Is there a way I can write to EEPROM a specific value when I program the device?

I am working with an Arduino UNO board.

Best,
Ilya

You know for sure this is not the first time the board is powered up, because you must have powered it
up to load the sketch!!

You are asking about determining the first time the sketch runs after its upload.

Anyway the issue is that previous sketches might have used the EEPROM and changed its state from
the factory-values of all 0xFF.

The solution is write a sketch that writes the relevant EEPROM locations (all of them if you want) to 0xFF.

Load and run that sketch before uploading the sketch of interest, then it can initialize its persistent state
reliably.

The EEPROM clearing sketch can be used for any sketch that uses EEPROM, no need to have a bespoke initialization sketch for each and every sketch, keep that code in the actualy sketch itself so all the EEPROM
code is in one place for that sketch, much easier to maintain.

The added advantage of a separate sketch for clearing the EEPROM is that it can verify the EEPROM is
working byte-by-byte too if you want and report on any failed bytes.

Thanks all. I will use the approach of writing my own EEPROM values for default state, but instead of using another sketch I will have it part of the main sketch and use UART to call that function.