Need help for EEPROM

Greetings and wishes of New Year!

I'm working on a project where I need to measure a weight of petrol/diesel using loadcell. To have proper output values from the loadcell I need to calibrate the loadcell first which saves the calibration factor into the UNO eeprom.

Now my question is, if I upload the calibration code to my UNO and save the calibration factor in its eeprom and then I upload my project sketch on the same UNO, will it be able to retain the value stored in EEPROM? or the eeprom gets cleared off when the new code is uploaded?

The next thing I want to store onto the eeprom is if the user selected petrol option or diesel option, once the user selects it, it will be saved onto the eeprom and the system will by default keep on selecting the same option unless the user changes it manually by himself.

So do I need to worry about the write/read cycles on the EEPROM? I read it that eeprom read/write cycles have finite cycles.

Any suggestions? Thanks in advance

I forgot to add the code!OOps! Sorry!

#include <LiquidCrystal.h>
LiquidCrystal lcd (0,1,2,3,4,5);

#include <HX711_ADC.h>
#include <EEPROM.h>

//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin

//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);

static boolean newDataReady;

//define keys
#define UP A1
#define SEL A2
#define DWN A3

float weight;
int petrol;
int diesel;
int page = 0;

int eeprom_calibrationFactor = 0;
int eeprom_petrol = 1;
int eeprom_deisel = 2;

float calibrationValue;

unsigned long previousMillis = 0;
const long interval = 60000;
unsigned long currentMillis;

unsigned long prevMillis = 0;
const long inter = 5000;
unsigned long curMillis;

unsigned long previousMillis1 = 0;
const long interval1 = 1000;
unsigned long currentMillis1;

volatile boolean up = false;
volatile boolean down = false;
volatile boolean middle = false;

int downButtonState = 0;
int upButtonState = 0;  
int selectButtonState = 0;          
int lastDownButtonState = 0;
int lastSelectButtonState = 0;
int lastUpButtonState = 0;

//-----------------------------------------

  void checkIfDownButtonIsPressed()
  {
    if (downButtonState != lastDownButtonState) 
  {
    if (downButtonState == 0) 
    {
      down=true;
    }
    delay(50);
  }
   lastDownButtonState = downButtonState;
  }

//---------------------------------------------

void checkIfUpButtonIsPressed()
{
  if (upButtonState != lastUpButtonState) 
  {
    if (upButtonState == 0) {
      up=true;
    }
    delay(50);
  }
   lastUpButtonState = upButtonState;
}

//----------------------------------------------------

void checkIfSelectButtonIsPressed()
{
   if (selectButtonState != lastSelectButtonState) 
  {
    if (selectButtonState == 0) {
      middle=true;
    }
    delay(50);
  }
   lastSelectButtonState = selectButtonState;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
void HomePage()
{
  getWeight();
  lcd.print("Fuel Quantity:");
  lcd.print(weight);
  lcd.print("Ltrs");   
}  
//--------------------------------------------

  void drawMenu()
  {
    if(page == 0)
    {
      HomePage();
    }
  if (petrol == 0 && diesel == 0 && page==1) 
  {    
    lcd.print("Select Fuel");
    lcd.setCursor(0,1);
    lcd.print("Press:");
    lcd.setCursor(0,2);
    lcd.print("A: Petrol  B: Diesel");
  }
  
  else if (page==2) 
  {
    lcd.clear();
    lcd.print("Please fill");
    lcd.setCursor(0,1);
    lcd.print("500ml Fuel");
    lcd.setCursor(0,2);
    lcd.print("Press A");
    lcd.setCursor(0,3);
    lcd.print("when done");
  }

  else if (page == 3)
  {
    lcd.clear();
    lcd.print("Fuel Quality is:");
    checkFuelQuality();
    lcd.setCursor(0,2);
    lcd.print("Press A");
    lcd.setCursor(0,3);
    lcd.print("to proceed");
  }

  else if (page == 4)
  {
    lcd.clear();
    
    lcd.print("Tank being filled");
    lcd.setCursor(0,1);
    lcd.print("Please wait...");
    getWeight();
    lcd.setCursor(0,2);
    lcd.print("Qnty:");
    lcd.print(weight);
    lcd.print("Ltrs");

    lcd.setCursor(0,3);
    lcd.print("Press B to exit");
  }
}
//------------------------------------------------------------------------------------------------------------------------------------------------
void checkFuelQuality()
{
   getWeight();
   makeDecision();
 }
//-----------------------------------------------------------------------------------------------------------------------------------
void getWeight()
{
  if (LoadCell.update()) newDataReady = true;
   if(newDataReady)
   {
    weight = LoadCell.getData();
   }
}
//--------------------------------------------------------------------------------------------------------------------------------------------
void makeDecision()
{
  lcd.setCursor(0,1);
 if(petrol)
 {
  if(weight > 720 && weight < 775)
  {
    lcd.print("Good"); 
  }
  else
  {
    lcd.print("Bad");
  }
 }
  else if(diesel)
  {
    if(weight > 820 && weight < 880)
    {
      lcd.print("Good");
    }
    else
    {
      lcd.print("Bad");
    }
  }
 }


//----------------------------------------------------------------------------------------------------------------------------------------

void setup() 
{
  pinMode(UP, INPUT_PULLUP);
  pinMode(SEL, INPUT_PULLUP);
  pinMode(DWN, INPUT_PULLUP);

  LoadCell.begin();
  EEPROM.get(eeprom_calibrationFactor, calibrationValue);
  EEPROM.get(eeprom_petrol, petrol);
  EEPROM.get(eeprom_deisel, diesel);

  lcd.begin(20,4);
  
  lcd.setCursor(5,0);
  lcd.print("Welcome to");
  lcd.setCursor(2,1);
  lcd.print("Ready Automotive");
  lcd.setCursor(0,3);
  lcd.print("Technology Solutions");
  
  delay(3000);
  lcd.clear(); 
}

void loop()
{
  drawMenu();
  downButtonState = digitalRead(DWN);
  selectButtonState = digitalRead(SEL);
  upButtonState =   digitalRead(UP);
 
  checkIfDownButtonIsPressed();
  checkIfUpButtonIsPressed();
  checkIfSelectButtonIsPressed();
  
  if (up && page == 1 ) 
  {
    up = false;
    lcd.setCursor(0,4);
    lcd.print("Petrol Selected");
    page = 2;
    petrol = 1;
    diesel = 0;

    EEPROM.put(eeprom_petrol, petrol);
    EEPROM.put(eeprom_deisel, diesel);
  }
  
  else if (up && page == 2)
  {
    up = false;
    page = 3;
  }
  else if (up && page == 3)
  {
    up = false;
    page = 4;
  }
  
  if (down && page == 1)
  {
    down = false;
    lcd.setCursor(0,4);
    lcd.print("Diesel Selected");  
    page = 2;
    diesel = 1;
    petrol = 0;
    EEPROM.put(eeprom_petrol, petrol);
    EEPROM.put(eeprom_deisel, diesel);
  }

 else if (down && page == 3)
  {
    page = 4;
  }

  else if (down && page == 4)
  {
    page = 0;
  }

  if (middle && page == 0)
  {
    page = 1;
  }
   currentMillis = millis();
   if (currentMillis - previousMillis >= interval)
   {
    previousMillis = currentMillis;
    lcd.clear();
    page = 0;
    HomePage();
//    MaintainTemp();
   }
  }

will it be able to retain the value stored in EEPROM? or the eeprom gets cleared off when the new code is uploaded?

The EEPROM on a Uno does not get cleared when new code is loaded using USB

So do I need to worry about the write/read cycles on the EEPROM? I read it that eeprom read/write cycles have finite cycles.

Each EEPROM cell can be written to 100,000 times (and possibly more) so unless the user changes the option 100,000 times thus causing it to be saved to EEPROM then there should be no problem

Note that it is your responsibility to write the program in such a way as only to save to EEPROM when the user changes the option

Thank you so much!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.