Translate

Tuesday 5 January 2016

Arduino EEPROM eraser.

I'm working on a small commercial project at the moment, which requires me to store some data into the Arduino's EEPROM. When you upload a new sketch to the Arduino, the EEPROM remains unchanged.

Now a "virgin" arduino has every address in the EEPROM set to 0xFF. Here's a quick sketch to reset the EEPROM to as new, so you can test your code on a fresh arduino.

It outputs to the serial monitor, and blinks Pin 13 on completion.

Apologies for lack of comments, but it's straightforward enough...

 #include <EEPROM.h> //EEPROM interface
 int addr = 0;
 float len;
 int pcComplete;

void setup() {

 Serial.begin (9600);
 Serial.println ("EEPROM Eraser");
 Serial.print("Size of EEPROM =");
 len = EEPROM.length();
 Serial.print (len);
 Serial.println (" bytes");
 delay (1000);
 pinMode(13, OUTPUT);
 }



void loop() {
 EEPROM.write (addr,255);
 Serial.print (addr);
 Serial.print ("  ");
 pcComplete = (addr / len)*100;
 Serial.print (pcComplete);
 Serial.println ("%");
 addr ++;
 if (addr == len+1){
  addr = 0;
  Serial.println ("Complete");
  blinkPin();
  
 }
 }

 void blinkPin () {
  while (1) {
  digitalWrite(13, HIGH);  
  delay(1000);             
  digitalWrite(13, LOW);   
  delay(1000);             
  }
 }

No comments:

Post a Comment