Sometimes we want to store data on the arduino that will survive a reset, calibration settings and set points are an example of this. EEPROM is a non volatile memory on the arduino that we can write values to and read in the setup loop to keep the values after a reset.

 

There are many blogs that will talk about, writing and reading EEPROM, but not many [if any] talk about checking if the memory slot has ever been written to in the past. We want to be able to use the value of a variable [ie setpoint] from the eeprom if there is one there, if there is not we want to use a defined value from the sketch.

 

What this code does, it reads the eeprom to see if there is a value to read, if not it ignores the eeprom and substitutes the one from the top of the code.

 

here is a serial screenshot showing it in action, resetting makes it move onto the next memory slot:

 

g

 

 

Header 1

/*

Script to test how to check if eprom is written or not

it will start at memory slot 1 and print value if it has ever been written

it will then write the next empty slot to the value you put at the top of the code

 

 

   28/8/2015  Michael Ratcliffe  Mike@MichaelRatcliffe.com

   

   

          This program is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.

 

 

    This program is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.

 

 

    You should have received a copy of the GNU General Public License

    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

 

 

#include <EEPROM.h>

int backupvalue=512;

 

 

 

 

int i=1;

int value=100;

 

 

void setup() {

  Serial.begin(9600);

Serial.print("Arduino Reset button pressed");

  while (value>=2) {

     i++;

     value = EEPROM.read(i);

     Serial.print("Memory _Slot:");

     Serial.print(i);

     Serial.print("  has saved value:");

     Serial.println(value*4);

     delay(10);

 

 

     //We will stop at 100, you must have got the point by now and I dont know what happens if you write a eprom memory slot that is out of range

     if(i>=100) return;

  };

value=backupvalue/4;

    EEPROM.write(i, value);

 

 

  // turn the LED on when we're done

  digitalWrite(13, HIGH);

}

 

 

void loop() {

  /** Empty loop. **/

}

 

 

 

Want to clear your EEPROM for your next project, here is the code to do it:

 

Header 1

/*

* EEPROM Clear

*

* Sets all of the bytes of the EEPROM to 0.

* Please see eeprom_iteration for a more in depth

* look at how to traverse the EEPROM.

*

* This example code is in the public domain.

*/

 

 

#include <EEPROM.h>

 

 

void setup() {

 

 

  /***

    Iterate through each byte of the EEPROM storage.

 

 

    Larger AVR processors have larger EEPROM sizes, E.g:

    - Arduno Duemilanove: 512b EEPROM storage.

    - Arduino Uno:        1kb EEPROM storage.

    - Arduino Mega:       4kb EEPROM storage.

 

 

    Rather than hard-coding the length, you should use the pre-provided length function.

    This will make your code portable to all AVR processors.

  ***/

 

 

  for (int i = 0 ; i < EEPROM.length() ; i++) {

    EEPROM.write(i, 0);

  }

 

 

  // turn the LED on when we're done

  digitalWrite(13, HIGH);

}

 

 

void loop() {

  /** Empty loop. **/

}

 

 

This will come in handy in our later projects.