Start Eeprom Value

Hi everyone.

I need to write to the EEPROM of the Arduino a value of manufactures pre defined, how can I make this recording values ??uploading the program to the arduino?

Thank you

You can write a simple sketch with "eeprom write anything" library. Then load your real sketch and have it access the address(es) you wrote to.

why not have a set of default values defined as constants in your code?

You could then check if a certain flag in the flash is set (e.g EEPROM position 0 == 132 ← just some random value), and if it is not set, then write the default values to the EEPROM, and set the flag (If the flag is set, then obviously you use the values in the EEPROM and not the default constants). This will only ever be used once, when the code runs for the very first time, but you could also have a “Factory reset” option in your code that would once again overwrite the EEPROM with the default constants.

Here’s some code to explain what I mean:

#include <EEPROM.h>

const byte FactorySettings[6] = { 5, 6, 2, 1, 3, 9 };
byte settings[6]; 

void setup() {
  delay(1200);
  Serial.begin(9600);
  byte b = EEPROM.read(0);
  if (b == 132) {
    //EEPROM contains values for our settings, read them
    Serial.println("Settings used:");
    for (byte i = 0; i < 6; i++) {
      settings[i] = EEPROM.read(i + 1); //start at position 1 in EEPROM, because position 0 contains our set flag
      Serial.println(settings[i]);
    }
  } else {
    //EEPROM has not been initialised yet! burn the constant values, and use them
    for (byte i = 0; i < 6; i++) {
      settings[i] = FactorySettings[i];
      EEPROM.write(i + 1, FactorySettings[i]); //start at position 1 in EEPROM, because position 0 contains our set flag
    }
    EEPROM.write(0, 132); //Set our flag
    Serial.println("Factory Settings used.");
  }
    Serial.println("Enter 6 random characters for new settings:");
  //Report on settings used
}

byte index = 0;
void loop() {
  if (Serial.available() > 0) {
    settings[index++] = Serial.read(); //Just grabbing the ASCII value of the characters as settings.
  }
  if (index == 6) {
    //Save the values, we could also do this as we read them, but sometimes you only want to commit at certain stages.
    for (byte i = 0; i < 6; i++) {
      EEPROM.write(i + 1, settings[i]); //start at position 1 in EEPROM, because position 0 contains our set flag
    }
    Serial.println("Reset the arduino to see new values, or press 'Y' to factory reset");
    while (1) {
      if (Serial.available() > 0) {
        if (Serial.read() == 'Y') {
          //Factory Reset. You could set all the settings, or do it "Microsoft Windows" style, and require a reboot.
          EEPROM.write(0, 0); // Clear our flag. This will force factory settings on the next power cycle.
          Serial.println("Factory Reset initiated. Please Reboot your microcontroller to apply.");
        }
      }
    }
  }
}

And here’s the output:

Factory Settings used.
Enter 6 random characters for new settings:
123456
Reset the arduino to see new values, or press ‘Y’ to factory reset
Press Reset
Settings used:
49
50
51
52
53
54
Enter 6 random characters for new settings:
111111
Reset the arduino to see new values, or press ‘Y’ to factory reset
Y
Factory Reset initiated. Please Reboot your microcontroller to apply.
Press Reset
Factory Settings used.
Enter 6 random characters for new settings:

Trex +1

additional: - place the "factory reset array" in PROGMEM (Flash) to save precious RAM. - http://arduino.cc/en/Reference/PROGMEM -

EEPROM reads as all high bits in the initial state, ie EEPROM.read() returns 0xFF.