Write a counter to different EEPROM addresses

Good afternoon everyone,

I am preparing a test with the EEPROM memory of my arduino uno. The test consists of 2 buttons, Button 1 each time I press prints an integral number, it starts with 0, and each time I press it adds 1 and I keep it in the consecutive address of the EEPROM memory.
Button2 prints the total number of times I have pressed Button1, resets the counter to 0, and saves it to my EEPROM.

I hope I have explained myself well, I leave you the code that works well for me:

#include <EEPROM.h>

const int boton1 = 8;
const int boton2 = 9;
bool valorBoton1, valorBotonAnt1 = false;
bool valorBoton2, valorBotonAnt2 = false;
int count = 0;
byte direction = 0;

void setup() {
Serial.begin(9600);
pinMode(boton1,INPUT);
pinMode(boton2,INPUT);


}

void loop() {

    EEPROM.get(direction, count);
    valorBoton1 = digitalRead(boton1);
    valorBoton2 = digitalRead(boton2);

  if (valorBoton1 && !valorBotonAnt1){

  Serial.println(count);

  count++;

  direction++;

  EEPROM.update(direction, count);
  }
valorBotonAnt1 = valorBoton1;
delay (5);




  if (valorBoton2 && !valorBotonAnt2){

  Serial.println("The Total is:");
  Serial.println(count);

  count = 0;

  direction++;

  EEPROM.update(direction, count);
  }
valorBotonAnt2 = valorBoton2;
delay (5);
}

And when I turn off and turn on the arduino, I get totally different values, style 162, or 470 ... without being consecutive.

If you have any suggestions or hints that can guide me, I will be very grateful.

An int (count) occupies two bytes. The first one is stored at EEPROM 0 and EEPROM 1 (given by direction). You then increment direction by one.

  1. Where will the next version of count be stored in the EEPROM?

b. Have you read how to use update()?

Do you have pullup or pulldown resistors on your boton pins?

1. Your posted sketch shows the following output (Fig-1) which is not right; get it fixed a should be like Fig-2.
smq.png
Figure-1:

2. The sketch is adjusted as follows to get output of Fig-2.

#include <EEPROM.h>
const int boton1 = 8;
const int boton2 = 9;
bool valorBoton1, valorBotonAnt1 = false;
bool valorBoton2, valorBotonAnt2 = false;
byte count = 0;  //variable to hold 1-byte data from one location
int direction = 0;//address range: 0x0000 - 0x03FF (Fig-3) // not  byte direction = 0;

void setup() 
{
  Serial.begin(9600);
  pinMode(boton1, INPUT);  //assume external pulldown
  pinMode(boton2, INPUT); //assume external pulldown
}

void loop() 
{
  EEPROM.get(direction, count); //address, variable
  valorBoton1 = digitalRead(boton1);
  valorBoton2 = digitalRead(boton2);

  if (valorBoton1 && !valorBotonAnt1) //boton1 is pressed
  {
    Serial.println(count);
    count++;
    direction++;   //next address
    EEPROM.update(direction, count);   //address, 2-byte
  }
  valorBotonAnt1 = valorBoton1;
  delay (5);

  if (valorBoton2 && !valorBotonAnt2) 
  {

    Serial.println("The Total is:");
    Serial.println(count);

    count = 0;

    direction++;

    EEPROM.update(direction, count);
  }
  valorBotonAnt2 = valorBoton2;
  delay (5);
}

smw.png
Figure-2:

eeprom-1.png
Figure-3: EEPROM layout of ATmega328P

smq.png

smw.png

eeprom-1.png

Thank you so much GolamMostafa, it's works very well, but for me is very important 2 things, the counter can be a number from 0 to 20 000, and the memory of my arduino uno can work more than 100 000 writings.

the is my new code to store INT counter, but when i restart the arduino, it's forget the last number registred.

#include <EEPROM.h>
const int boton1 = 8;
const int boton2 = 9;
bool valorBoton1, valorBotonAnt1 = false;
bool valorBoton2, valorBotonAnt2 = false;
int count = 0;  
int direction = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(boton1, INPUT);  //assume external pulldown
  pinMode(boton2, INPUT); //assume external pulldown
}

void loop()
{
  EEPROM.get(direction, count); //address, variable
  valorBoton1 = digitalRead(boton1);
  valorBoton2 = digitalRead(boton2);

  if (valorBoton1 && !valorBotonAnt1) //boton1 is pressed
  {
    Serial.println(count);
    count++;
    direction += sizeof(int);   //next address
    EEPROM.update(direction, count);   
  }
  valorBotonAnt1 = valorBoton1;
  delay (5);

  if (valorBoton2 && !valorBotonAnt2)
  {

    Serial.println("The Total is:");
    Serial.println(count);

    count = 0;

    direction += sizeof(int);

    EEPROM.update(direction, count);
  }
  valorBotonAnt2 = valorBoton2;
  delay (5);
}

use EEPROM.put() instead of EEPROM.update(); EEPROM.update only updates one byte, not the complete int.

Thank you sterretje, i used your recomendation:

#include <EEPROM.h>
const int boton1 = 8;
const int boton2 = 9;
bool valorBoton1, valorBotonAnt1 = false;
bool valorBoton2, valorBotonAnt2 = false;
int count = 0; 
int direction = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(boton1, INPUT);  //assume external pulldown
  pinMode(boton2, INPUT); //assume external pulldown
}

void loop()
{
  EEPROM.get(direction, count); //address, variable
  valorBoton1 = digitalRead(boton1);
  valorBoton2 = digitalRead(boton2);

  if (valorBoton1 && !valorBotonAnt1) //boton1 is pressed
  {
    Serial.println(count);
    count++;
    direction += sizeof(int);   //next address
    EEPROM.put(direction, count);   
  }
  valorBotonAnt1 = valorBoton1;
  delay (5);

  if (valorBoton2 && !valorBotonAnt2)
  {

    Serial.println("The Total is:");
    Serial.println(count);

    count = 0;

    direction += sizeof(int);

    EEPROM.put(direction, count);
  }
  valorBotonAnt2 = valorBoton2;
  delay (5);
}

But still have the same problem, if i disconect the Arduino and reconected, he start by number 256

You keep on writing to consecutive locations in EEPROM; why?

because each write address in the EEPROM has a life of 100,000 writes, so if I write in consecutive addresses, it would multiply the life of the EEPROM, I'm an arduino novice, I hope you can guide me to solve this problem.

i think the Arduino when it restart, it load always the first adress.

i think the Arduino when it restart, it load always the first adress.

Yes, if you are wear leveling like you are doing, you need to go to the last address written.

Tracking where the last data is located is a complication, but because your count data is monotonically increasing, the data becomes its own index to the location, and there is a simple wear leveling routine

Your cycle count data is an int, and you write it to 2 bytes in the eeprom. With each write, you will move this storage chunk progressively around in the eeprom to 512 possible locations.

There will be a data value in eeprom bytes 0:1, then the next at bytes 2:3, then the next in 4:5 etc. When you get to the end of the eeprom you start around again. When the power is on and the program is running the management of the next write location is straight forward.

When power is cycled, the program needs to find where the last written chunk was located, maybe use that value for something, and to start writing again from that location.

The way to do this is to start reading the eeprom from address 0 in 2 byte chunks and test each value against the next. When the value at an address is not bigger than the value in the previous address (remember to be incrementing addresses by 2), that is where to write the next value. You will need to start off with the eeprom at all 0's so that the comparison of each stored value to the next will work on the first pass. If you find that all 512 locations contain increasing data values, then your next write will be at address 0.

Your first task in developing this is to write your data 512 times, at sequential locations 2 bytes apart. Then practice reading what you have stored.

When you are comfortable writing the data to sequential locations and sequentially reading it out, you can move on to comparing each pair of sequential values. Test if a value is larger than the last one, and if it isn't you have found where the next write should be. The testing routine should be part of startup() in your final program.

It has been a really useful explanation, thank you very much for the explanation, you have opened an address for me to develop the code I need.
Thank you so much.