Writing to/reading from the EEPROM only once or an alternate way

Hi, I'm working on a project which turns on a fan(connected to a relay) when a certain threshold in temperature is reached. I have included a piece of code where the user can set the threshold value for. But it has to be done only once or twice.

I know the value has to be stored in the EEPROM since I need the value to hold when the power to the arduino is gone and comes back. Now my question is there way to write to the EEPROM once and not have to read from it all the time when the power is interrupted, or better yet do it without having to write to the EEPROM.

Yami89:
Now my question is there way to write to the EEPROM once and not have to read from it all the time when the power is interrupted, or better yet do it without having to write to the EEPROM.

Impossible.
You have to write to the EEPROM if you need persistent data (but what you can do, is to avoid an attempt of overwritting the same existing value); if something changes, you'll have to save those changes by writing to the EEPROM once again. THERE'S NO ESCAPE ON THAT!.
However, if you insist in writing only once ever (which kinda beats up the purpose of the EEPROM), then you'll have to write the corresponding "flag" (aka boolean value) as well.

Same for the retrieval (reading), don't expect that the Arduino will "magically" recover those values after a power cycle, not without calling the respective procedure (which is reading from the EEPROM, at least once in the setup()).

To understand why all this, then look for the definition of the terms "volatile memory" and "non-volatile memory"

Thanks very much for the help. Isn't there any way to change the value of a global variable within the code and make it "permanent" hehehehe. As I feel that writing the values into the EEPROM is quite a lot to do for a very simple task that I have to achieve.

What is the problem with reading the eeprom back once in setup?

Yami89:
As I feel that writing the values into the EEPROM is quite a lot to do for a very simple task that I have to achieve.

Well, it's time-consuming (in the milliseconds range) and decreases it's endurance; so you're mostly right. What is not "a lot", is the effort to use the EEPROM in code.

First of all, why you're concerned about it? Have to write data too often or too quickly?

You can read from EEPROM indefinitely without hurting it.

You can also set a second location as a "calibrated" flag, and once you write to one location, write to the calibrated flag location so you know that it's set.

Adding EEPROM functionality is reasonably simple:

#include <EEPROM.h>

...

Read from:

EEPROM.get( location, variable );

Write to:

EEPROM.update( location, variable );

So if you wanted to set a threshold based on what it was before you lost power, here's some pseudo-code:

#include <EEPROM.h>

const int thresholdStorage = 55; //arbitrary number
unsigned int threshold = 65535; //Warning: 65k degrees will melt equipment but this is the default

void setup()
{
  //Upon power-up or reset, will grab the last known value from thresholdStorage 
  //and assign it to the int "threshold". 
  EEPROM.get( thresholdStorage, threshold );
}

void loop()
{
  if( condition for resetting threshold, say a button or whatever )
  {
     EEPROM.update( thresholdStorage, threshold );
  }
  //you'd have to define the pin configuration.  You probably also want a hysteresis value there, 
  //otherwise if you're right at the threshold you'll click the relay on and off like a castanet dancer.  
  if( analogRead( temperatureSensor ) >= ( threshold + 10 ) )
  {
     digitalWrite( fanControlPin, HIGH );
  }
  if( analogRead( temperatureSensor ) <= ( threshold - 10 ) )
  {
     digitalWrite( fanControlPin, LOW );
  }
}