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:
#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
}
}
}
sarathkumar341:
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).
unsigned long x = 999999; //0F423F
EEPROM.put(0x0100, x); //low order byte will be stored into low order memory location
GolamMostafa:
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
GolamMostafa:
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: EEPROM.put(0x0100, (__int24)999999); does the trick.
AWOL:
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();.
GolamMostafa:
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.
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)
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.