Go Down

Topic: How to store a 6 digit value in EEPROM? But it is holding only 256. (Read 585 times) previous topic - next topic

sarathkumar341

I build a project with Inductive Proximity Sensor that counts the products and store them in Arduino EEPROM. The problem is it can hold up to 255 only (because of 1 byte). Normally the count can go even up to 1000 but the EEPROM Value becomes reset and starts from the beginning. 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 add (but cost-effective).

This is the code I used:

Code: [Select]

#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
    }
  }
}


Lucario448


GolamMostafa

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 add (but cost-effective).
Assume that we wish to save 999999 which is 0F423F in hex representation into the EEPROM. As one location of the EEPROM can hold only 8-bit (2-digit hex) data, we need 3 consecutive memory locations to store 999999 (= 0F423F). The following codes will store the data in EEPROM stating from location: 0x0100 (arbitrary choice).

Code: [Select]
unsigned long x = 999999; //0F423F
EEPROM.put(0x0100, x); //low order byte will be stored into low order memory location

AWOL

Assume that we wish to save 999999 which is 0F423F in hex representation into the EEPROM. As one location of the EEPROM can hold only 8-bit (2-digit hex) data, we need 3 consecutive memory locations to store 999999 (= 0F423F). The following codes will store the data in EEPROM stating from location: 0x0100 (arbitrary choice

....and because we're using an unsigned long variable, we'll write to four consecutive locations, not just the three we need
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

GolamMostafa

....and because we're using an unsigned long variable, we'll write to four consecutive locations, not just the three we need
If I would execute this instruction: EEPROM.put(0x0100, 999999);, do you think that the 'data writing' would have been done into 3 locations?

AWOL

If I would execute this instruction: EEPROM.put(0x0100, 999999);, do you think that the 'data writing' would have been done into 3 locations?
No - why would I think that?

However, if we could persuade the C standard to accept gcc's __int24 or __uint24 datatypes as minimal native types, then this ought to be possible.

Even a simple cast:
Code: [Select]
EEPROM.put(0x0100, (__int24)999999); does the trick.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

GolamMostafa

No - why would I think that?

However, if we could persuade the C standard to accept gcc's __int24 or __uint24 datatypes as minimal native types, then this ought to be possible.

Even a simple cast:
Code: [Select]
EEPROM.put(0x0100, (__int24)999999);
 does the trick.
1.  __uint24 data type perfectly writes into 3 consecutive memory locations.
Code: [Select]
#include<EEPROM.h>

void setup()
{
  Serial.begin(9600);
  EEPROM.write(0x0103, 0x67);
  EEPROM.put(0x0100, (__uint24)999999);//0F423F
  Serial.println(EEPROM.read(0x0103), HEX ); //shows: 67
}

void loop()
{
 
}


2. unsigned long x = 999999; style writes into 4 consecutive memory locations (wastage of 1 memory location).
Code: [Select]
#include<EEPROM.h>

void setup()
{
  Serial.begin(9600);
  EEPROM.write(0x0103, 0x67);
  unsigned long x = 999999;
  EEPROM.put(0x0100, x);//0F423F
  Serial.println(EEPROM.read(0x0103), HEX );//shows: 0
}

void loop()
{

}


3.  This style: EEPROM(0x0100,999999); also writes into 4 consecutive memory locations.
Code: [Select]
#include<EEPROM.h>

void setup()
{
  Serial.begin(9600);
  EEPROM.write(0x0103, 0x67);
//  unsigned long x = 999999;
  EEPROM.put(0x0100, 999999);//0F423F
  Serial.println(EEPROM.read(0x0103), HEX );//shows: 0
}

void loop()
{

}

GolamMostafa

@AWOL

Something very strange issue with the data type __int24 and __uint24, which is: Compilation Error?
Code: [Select]
void setup()
{
  Serial.begin(9600);
  uint32_t x = 999999;
  Serial.print(x, DEC); //shows: 999999
  //--------------------
  __uint24 x1 = 999999;
 // Serial.println(x1, DEC); //compilation error; Why?
}

void loop()
{

}

 

AWOL

Quote
Compilation Error?
@AWOL

Something very strange issue with the data type __int24 and __uint24, which is: Compilation Error?
No known promotion for __uint24, or something like that?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

GolamMostafa

No known promotion for __uint24, or something like that?
I wanted to apply this data type (__int24) in the solution of the problem of this thread for processing and displaying of signed 24-bit data; but I failed. After that, I wanted to hear more about this data type in connection with Serial.print();.

AWOL

I wanted to apply this data type (__int24) in the solution of the problem of this thread for processing and displaying of signed 24-bit data; but I failed. After that, I wanted to hear more about this data type in connection with Serial.print();.
If you want to print a 24 bit integer type, you'll either need another overload of the print method, or do the type promotion manually.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

GolamMostafa

If you want to print a 24 bit integer type, you'll either need another overload of the print method, or do the type promotion manually.
Researching since 24 February 2019 to find a practical meaning of your post.....!

sarathkumar341

Hey Team, I have fixed that length issue using put and get methods. That works perfect, but I need any alternative way to hold these counts without any fail (as EEPROM has some write life cycles). That would be cheaper too. Is there any good EEPROMs or any alternative way to store like SD Card like that (but SD Card mehod will be costly I think)

Budvar10

The EEPROM has an endurance 100k/1M write cycles. It is good enough for some setup data which are rarely changed but it is not feasible for frequent writes. Use SD card for frequent writes.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

Go Up