Delete EEPROM every time Arduino is turned ON

Where should I put the following code (if it is right) to make Arduino set values at each EEPROM adress on zero every time Arduino is turned ON:

#include "EEPROM.h"
int adress = 0;

void setup () {
}

void loop () {
  EEPROM.write(adress, 0);
  adress++;
  if (adress == 512) {
    adress = 0;
  }
}

If you put it in setup() it will run once and go on to loop().

Excellent way of burning out your EEPROM in a few minutes.
Put the code in setup, but I honestly can't see the point.

AWOL:
Excellent way of burning out your EEPROM in a few minutes.
Put the code in setup, but I honestly can't see the point.

groundfungus:
If you put it in setup() it will run once and go on to loop().

Thank you both for replies. I need to save values between 0 and 6 where 0 means "do nothing" which are relevant only in current session and I need a little bit bigger storage.

which are relevant only in current session

Therefore it does not need to be in EEPROM.

You probably need to store your data more efficiently.

I need to save values between 0 and 6 where 0 means "do nothing"

How much of this data is there in any one session ?

Even if you use EEPROM why do you need to set all of it to zero anyway ?

UKHeliBob:

I need to save values between 0 and 6 where 0 means "do nothing"

How much of this data is there in any one session ?

Even if you use EEPROM why do you need to set all of it to zero anyway ?

These numbers represent instructions. When Arduino reads 1, it will do one thing (spin two small DC motors in the same way to move forward), when 2, other thing (spin two small DC motors in the same way to move backwards). These instructions would be given through push-buttons. When it would read 0, it would mean that none of the buttons was pressed and that it sholud keep watching for button pushes.

I think it's time to post the code....

When Arduino reads 1,

From where? It won't read 1 from EEPROM after you set all the values to 0. If it's reading from somewhere else, why does what's in EEPROM matter?

#include "EEPROM.h"
#define MAX 512
const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int LED4 = 5;
int val;
int i;
int adress = 0;
int old_b = 0;
int L1;
int L2;
int counter = 0;
int temp;
int reading;
int x = 0;

void setup() {
  Serial.begin(9600);
  pinMode(A5, INPUT_PULLUP); 
  EEPROM.write(x, 0);
  adress++;
  if (x == 512) {
    x = 0;
  }
}
 
int readButtons(int pin) {
  int b, c;
  c = analogRead (pin);
  Serial.print ("analogRead = ");
  Serial.println (c); 
  if (c > 1015) b = 0; 
  else if (c > 70 && c < 76) b = 1; 
  else if (c < 128 && c > 122) b = 2; 
  else if (c > 169 && c < 175) b = 3; 
  else if (c > 209 && c < 217) b = 4;
  else if (c > 247 && c < 256) b = 5;
  else if (c > 280 && c < 291) b = 6;
  else b = 0;
  if (b == old_b) {
    return 0;
    old_b = b;
  } else {
    return b;
    old_b = b;
  }
}
 
void loop () {
 
 while ((val = readButtons(5)) != 5) {
  if ((val == 1) || (val == 2) || (val == 3) || (val == 4)) {
    EEPROM.write(adress, val); 
    Serial.print("In ");
    Serial.print(adress);
    Serial.print(" saving ");
    Serial.println (val);
    delay (500);
    adress++;
    if (adress == MAX) {
      adress = 0;
    }
  } 
 }
 
 temp = adress;
 adress = 0;

 for (i = 0; i < temp; i++) {
   
   reading = EEPROM.read(i); 
   if (reading % 2 == 0) {
      L1 = 2;
      L2 = reading / 3 + 3;
   } else {
      L2 = 5;
      L1 = reading % 3 + 3;
   }
   
   
   if  (readButtons(5) != 5) {    
    digitalWrite (L1, HIGH);                        
    if (readButtons(5) != 5) {    
     digitalWrite (L2, HIGH);          
     delay (1000);                     
     digitalWrite (L1, LOW);      
     digitalWrite (L2, LOW);        
     if (readButtons(5) == 5) {    
      i = temp;
      }
     } else {
        digitalWrite (L1, LOW);  
        i = temp;
     }  
    }    
 } 
}

Sigh!

So would you like to explain what you are trying to achieve with this code?

EEPROM.write(x, 0);
  adress++;
  if (x == 512) {
    x = 0;
  }

That's quite a long-winded way of erasing one byte of EEPROM.

Grumpy_Mike:
Sigh!

So would you like to explain what you are trying to achieve with this code?

When none of the buttons is pressed, Arduino keeps watching for presses. If 1,2,3 or 4th button is pressed Arduino stores 1,2,3 or 4 in EEPROM and keeps doing that until 5th button is pressed. When that happens, Arduino from EEPROM reads values and according to them blinks LEDs. If during blinking fifth button is pressed again, blinking should stop and again wait for press of one of buttons (1,2,3,4). Storing in EEPROM starts from the first (0) adress. When turned ON arduino should delete old entries. Could it maybe be made simpler and there is a problem when I'm trying to stop blinking because of the delay(1000).

The whole point of EEPROM is to persist between power cycles. Why not use RAM?

AWOL:

EEPROM.write(x, 0);

adress++;
 if (x == 512) {
   x = 0;
 }


That's quite a long-winded way of erasing one byte of EEPROM.

If you know, would you tell me a better way?

KeithRB:
The whole point of EEPROM is to persist between power cycles. Why not use RAM?

If I use RAM, should it be over array? If yes, how to know how much is enough for RAM to suffice?

An array would seem to be a good idea. See my earlier question. How many values do you need to save ?

UKHeliBob:
An array would seem to be a good idea. See my earlier question. How many values do you need to save ?

Up to 50 instructions that is number of buttons presses.

If yes, how to know how much is enough for RAM to suffice?

How do you know how much EEPROM you will use? The answer is the same.

As lots of us are saying there is absolutely no need to use EEPROM for this problem, which sounds quite convoluted to be anything but a piece of homework.

Arduino stores 1,2,3 or 4 in EEPROM and keeps doing that until 5th button is pressed

What does it store? is it the time it was pressed or simply the fact it was pressed?

Up to 50 instructions that is number of buttons presses.

then

byte presses[50];

will do it.

there is a problem when I'm trying to stop blinking because of the delay(1000).

Then remove it.

Grumpy_Mike:

Arduino stores 1,2,3 or 4 in EEPROM and keeps doing that until 5th button is pressed

What does it store? is it the time it was pressed or simply the fact it was pressed?

Up to 50 instructions that is number of buttons presses.

then

byte presses[50];

will do it.

there is a problem when I'm trying to stop blinking because of the delay(1000).

Then remove it.

On the beginning of the program I have made a function which returns if a button was pressed, it looks for raising edge. If I reomve delay LEDs will only blink and not stay ON for a second, which is my goal (look at the code).