proxmty counter 999999 eeprom problem,How to store a 6 digit value in EEPROM?

I need to store 5 digits or more to store it safely in EEPROM. How to achieve it. Is there any solution for this or any hardware we can addHow to store a 6 digit value in EEPROM? But it is holding only 256.
help plese??

#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// pin definitions
int ledPin = 13;
int buttonPin = 7;
const int resetPin = 2;
int resetValue = 0;

// global variables
int lastButtonState = 1;
long unsigned int lastPress;
int debounceTime = 20;
int counter;

void setup() {
// setup pin modes
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(resetPin, INPUT);

lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
//initialise Serial port
Serial.begin(9600);

byte value = EEPROM.read(0);
lcd.print("Last Count");
lcd.setCursor(7, 1);
lcd.print(value);
//assign counter the value of address 0
counter = EEPROM.read(0);
//write a 0 to address 0. This allows for consecutive resets to reset the counter
EEPROM.write(0, 0);
}

void loop() {
int resetCount = digitalRead(resetPin);

if (resetCount == LOW) {

counter = 0;
EEPROM.write(0, counter);
delay(100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Product Count");
lcd.setCursor(7, 1);
lcd.print(counter);
}
int buttonState = digitalRead(buttonPin); //read the state of buttonPin and store it as buttonState (0 or 1)

if ((millis() - lastPress) > debounceTime) //if the time between the last buttonChange is greater than the debounceTime
{
lastPress = millis(); //update lastPress
if (buttonState == 0 && lastButtonState == 1) //if button is pressed and was released last change
{
counter++;
EEPROM.write(0, counter); //write counter to address 0
digitalWrite(ledPin, HIGH); //momentary LED
lastButtonState = 0; //record the lastButtonState

//print the results
Serial.print("Counter: ");
Serial.println(counter);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Product Count");
lcd.setCursor(7, 1);
lcd.print(counter);
}
if (buttonState == 1 && lastButtonState == 0) //if button is not pressed, and was pressed last change
{
lastButtonState = 1; //record the lastButtonState
digitalWrite(ledPin, LOW); //momentary LED
}
}
}

999999Eeprom.ino (2.28 KB)

Use the EEPROM library "put" and "get" methods.

Please remember to use code tags when posting code

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Have to googled?

arduino storing large numbers in eeprom

Tom... :slight_smile:

Please edit your post to add code tags, as described in "How to use this forum".

Also, please be aware that EEPROM has limited erase/write cycles - you may well wear-out your EEPROM if you're not careful.

1. This is (Fig-1) the conceptual view of the EEPROM memory block of ATmega328P MCU of UNO Board.
eeprom-1.png
Figure-1:

2. Each location of the EEPROM of Fig-1 can hold only 1-byte (8-bit) data.

3. The data item (999999 = 0x0F423F) you are interested to store is of 3-byte long. So, you need three locations to store this data item, you need to separate the bytes from the composite data item.

4. You can use the following codes to separate the bytes using shift (>>) and & (AND) operators.

long  x = 999999;                                       // x will automatically hold 0F423F
byte x1 = (byte) x & 0x0000FF;                   //x1 will hold 3F
byte x2 = (byte) (x>>8) & 0x0000FF;          //x2 will hold 42
byte x3 = (byte) (x>>16) & 0x0000FF;        //x3 will hold 0F

5. You can separate the bytes using % and / operators.

do
{
    myByte[i] = x%256;
    x = x/256;
    i++;
}
while( x != 0);

6. You can also use union structure to separate the bytes.

union
{
     long x;
     byte myByte[4];

}myData;

myData.x = 999999;   //0x0F423F

Once x is initialized in setup() function, the bytes are automatically stored in the array named myByte[]; where, lower byte (0x3F) takes place in the lower array element (myByte[0]).

7. You can use EEPROM.write(address, dataByte); instruction three times to store the data bytes of Step-4/5/6 into three different locations.

(1) Storing data bytes of Step-4

EEPROM.write(0x0000, x1);
EEPROM.write(0x0001, x2);
EEPROM.write(0x0002, x3);

(2) Storing data bytes of Step-5

for(int i=0; i<3; i++)
{
    EEPROM.write(address+i, myByte[i]);
}

(3) Storing data bytes of Step-6

for(int i=0; i<3; i++)
{
    EEPROM.write(address+i, myData.myByte[i]);
}

8. You can use EEPROM.put(address, x); instruction to store the data item 999999; where, the instruction automatically separates the data bytes and store them into three consecutive locations with lower byte being stored in lower memory location. Edit:The put() method will store one more data item (here it is 0x00) into the 4th memory location as the declared size of x is long (32-bit = 4-byte).

eeprom-1.png

1 Like

GolamMostafa:
7. You can use EEPROM.put(address, x); instruction to store the data item 999999; where, the instruction automatically separates the data bytes and store them into three consecutive locations with lower byte in the lower memory location.

Or, much more likely, four consecutive locations.

TheMemberFormerlyKnownAsAWOL:
Or, much more likely, four consecutive locations.

Right. (+K).

This how I do it

//EEPROM_LONG//
void EEPROMWritelong(int address, unsigned long value)
{
  //Decomposition from a long to 4 bytes by using bitshift.
  //One = Most significant -> Four = Least significant byte
  byte four = (value & 0xFF);
  byte three = ((value >> 8) & 0xFF);
  byte two = ((value >> 16) & 0xFF);
  byte one = ((value >> 24) & 0xFF);
  
  //Write the 4 bytes into the eeprom memory.
  EEPROM.write(address, four);
  EEPROM.write(address + 1, three);
  EEPROM.write(address + 2, two);
  EEPROM.write(address + 3, one);
  EEPROM.commit();
}

//This function will return a 4 byte (32bit) long from the eeprom
//at the specified address to adress + 3.
unsigned long EEPROMReadlong(long address)
{
  //Read the 4 bytes from the eeprom memory.
  long four = EEPROM.read(address);
  long three = EEPROM.read(address + 1);
  long two = EEPROM.read(address + 2);
  long one = EEPROM.read(address + 3);
  
  //Return the recomposed long by using bitshift.
  return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}

//END///code]



Then when you want write and read



[code]
EEPROMWritelong(0, CT);/code]


[code]
svCycleTime = EEPROMReadlong(0);/code]


Just remember to allocate the appropriate space for this, I always use 0, 5, 10,....ect.

Proietti:
This how I do it

//EEPROM_LONG//

void EEPROMWritelong(int address, unsigned long value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);

//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
EEPROM.commit();
}

//This function will return a 4 byte (32bit) long from the eeprom
//at the specified address to adress + 3.
unsigned long EEPROMReadlong(long address)
{
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);

//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}

//END///code]

Then when you want write and read

[code]
EEPROMWritelong(0, CT);/code]

[code]
svCycleTime = EEPROMReadlong(0);/code]

Just remember to allocate the appropriate space for this, I always use 0, 5, 10,....ect.

Why do you make it so difficult for yourself?

Proietti:
This how I do it

EEPROM.commit();

If data are to be stored into EEPROM of UNO, then do we need this instruction: EEPROM.commit();?

Is this not true or I couldn't help please?

#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// pin definitions
int ledPin = 13;
int buttonPin = 7;
const int resetPin = 6;
int resetValue = 0;

// global variables
int lastButtonState = 1;
long unsigned int lastPress;
int debounceTime = 20;
int counter;

long  x = 999999;                                       // x will automatically hold 0F423F
byte x1 = (byte) x & 0x0000FF;                   //x1 will hold 3F
byte x2 = (byte) (x>>8) & 0x0000FF;          //x2 will hold 42
byte x3 = (byte) (x>>16) & 0x0000FF;        //x3 will hold 0F

void setup() {
  // setup pin modes
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(resetPin, INPUT);

  lcd.begin();
  // Turn on the blacklight and print a message.
  lcd.backlight();
  //initialise Serial port
  Serial.begin(9600);

  byte value = EEPROM.read(0);
  lcd.print("Last Count");
  lcd.setCursor(7, 1);
  lcd.print(value);
  //assign counter the value of address 0
  counter = EEPROM.read(0);
  //write a 0 to address 0. This allows for consecutive resets to reset the counter
      EEPROM.write(0x0000, x1);
      EEPROM.write(0x0001, x2);
      EEPROM.write(0x0002, x3);
}

void loop() {
  int resetCount = digitalRead(resetPin);

  if (resetCount == LOW) {

    counter = 0;
    EEPROM.write(0, counter);
    delay(100);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Product Count");
    lcd.setCursor(7, 1);
    lcd.print(counter);
  }
  int buttonState = digitalRead(buttonPin);   //read the state of buttonPin and store it as buttonState (0 or 1)

  if ((millis() - lastPress) > debounceTime)  //if the time between the last buttonChange is greater than the debounceTime
  {
    lastPress = millis();   //update lastPress
    if (buttonState == 0 && lastButtonState == 1)   //if button is pressed and was released last change
    {
      counter++;
      
      digitalWrite(ledPin, HIGH); //momentary LED
      lastButtonState = 0;    //record the lastButtonState

      //print the results
      Serial.print("Counter: ");
      Serial.println(counter);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Product Count");
      lcd.setCursor(7, 1);
      lcd.print(counter);
    }
    if (buttonState == 1 && lastButtonState == 0)   //if button is not pressed, and was pressed last change
    {
      lastButtonState = 1;    //record the lastButtonState
      digitalWrite(ledPin, LOW);  //momentary LED
    }
  }
}

@GalamMostafa you are correct you do not need
EEPROM.commit(); if using an arduino. If using an ESP8266 you need it