Why are EEPROM positions not all resetting to 255? Some are resetting to 0

#include <EEPROM.h>

const int EEPROM_SIZE = 100; // Define size of EEPROM available
int addr = 0; // EEPROM address pointer

unsigned long programStartTime;

void setup()
{
  Serial.begin(9600);
  delay(2000);
  Serial1.begin(9600);
  delay(2000);

  programStartTime = millis(); // Record program start time

  // Initialize EEPROM data
  //  for (int i = 0; i < EEPROM_SIZE; i++)
  //    EEPROM.write(i, 0);

  displayEEPROMData();
}

void loop()
{
  if (Serial1.available() && Serial.read() != '0')
  {
    int receivedData = Serial1.read();

    if (receivedData >= 1 && receivedData <= 60)
    {
      byte receivedValue = static_cast<byte>(receivedData); // Convert to byte

      // Calculate the timestamp based on the difference from the current time
      unsigned long currentTime = millis() - programStartTime;

      // Store the received timestamp and value in EEPROM
      EEPROM.put(addr, currentTime);
      addr += sizeof(currentTime);
      EEPROM.put(addr, receivedValue);
      addr += sizeof(receivedValue);

      displayEEPROMData();
    }

  }

  if (addr >= EEPROM.length()) {                                                  // EEPROM_SIZE
    addr = 0; // Wrap around if the EEPROM is full
  }

  if (Serial.available() > 0)
  {
    Serial.read();
    displayEEPROMData();
  }

if (Serial.available() > 0)
{
  char inputChar = Serial.read();
  if (inputChar == '0') // If '0' is entered into the serial monitor
  {
    programStartTime = millis(); // Reset the program start time
    for (int i = 0; i < EEPROM_SIZE; i++)
    {
      EEPROM.update(i, 255);
    }

    // Add additional code here if needed
    // For example, you can print a message to the serial monitor
    Serial.println("EEPROM data reset");
  }
}


}

void displayEEPROMData()
{
  // Display all positions stored in EEPROM
  Serial.println("EEPROM:");

  for (int i = 0; i < EEPROM.length(); i++)
  {
    unsigned long timestamp;
    byte value;
    int offset = i * (sizeof(unsigned long) + sizeof(byte));
    EEPROM.get(offset, timestamp);
    EEPROM.get(offset + sizeof(timestamp), value);

    Serial.print(String(i + 1) + ": ");
    printTime(timestamp);
    Serial.println(", " + String(value));
  }

}

void printTime(unsigned long timestamp)
{
  // Calculate time components (hour, minute, second)
  int hours = (timestamp / 1000) % 86400 / 3600;
  int minutes = (timestamp / 1000) % 3600 / 60;
  int seconds = (timestamp / 1000) % 60;

  if (hours < 10) Serial.print("0");
  Serial.print(hours);
  Serial.print(":");
  if (minutes < 10) Serial.print("0");
  Serial.print(minutes);
  Serial.print(":");
  if (seconds < 10) Serial.print("0");
  Serial.print(seconds);
}

EEPROM positions 1-244, value = 255. Good.

225-409, value = 0. Bad.
410-429, value = 255. Good.
430-614, value = 0. Bad.
615-634, value = 255. Good.
635-819, value = 0. Bad.
820-839, value = 255. Good.
840-1024, value = 0. Bad.

How to fix this if-statement to update all values to 255:

if (Serial.available() > 0)
{
  char inputChar = Serial.read();
  if (inputChar == '0') // If '0' is entered into the serial monitor
  {
    programStartTime = millis(); // Reset the program start time
    for (int i = 0; i < EEPROM_SIZE; i++)
    {
      EEPROM.update(i, 255);
    }

    // Add additional code here if needed
    // For example, you can print a message to the serial monitor
    Serial.println("EEPROM data reset");
  }
}

Your code is only setting the first 100 bytes (the value of EEPROM_SIZE) to 255. If you want to set all the EEPROM bytes to 255, perhaps something like:

    for (int i = 0; i < EEPROM.length(); i++)
    {
      EEPROM.update(i, 255);
    }
1 Like

If the size of the EEPROM is 100 then it can only hold 100 bytes, not 100 unsigned longs

1 Like

That was it. Time is now 17:02:47 for everything though.

image

Please post your revised sketch

#include <EEPROM.h>

const int EEPROM_SIZE = 100; // Define size of EEPROM available
int addr = 0; // EEPROM address pointer

unsigned long programStartTime;

void setup()
{
  Serial.begin(9600);
  delay(2000);
  Serial1.begin(9600);
  delay(2000);

  programStartTime = millis(); // Record program start time

  // Initialize EEPROM data
  //  for (int i = 0; i < EEPROM_SIZE; i++)
  //    EEPROM.write(i, 0);

  displayEEPROMData();
}

void loop()
{
  if (Serial1.available() && Serial.read() != '0')
  {
    int receivedData = Serial1.read();

    if (receivedData >= 1 && receivedData <= 60)
    {
      byte receivedValue = static_cast<byte>(receivedData); // Convert to byte

      // Calculate the timestamp based on the difference from the current time
      unsigned long currentTime = millis() - programStartTime;

      // Store the received timestamp and value in EEPROM
      EEPROM.put(addr, currentTime);
      addr += sizeof(currentTime);
      EEPROM.put(addr, receivedValue);
      addr += sizeof(receivedValue);

      displayEEPROMData();
    }

  }

  if (addr >= EEPROM.length()) {                                                  // EEPROM_SIZE
    addr = 0; // Wrap around if the EEPROM is full
  }

  if (Serial.available() > 0)
  {
    char inputChar = Serial.read();
    if (inputChar == '0') // If '0' is entered into the serial monitor
    {
      programStartTime = millis(); // Reset the program start time

      for (int i = 0; i < EEPROM.length(); i++)
      {
        EEPROM.update(i, 255);
      }

      Serial.println("EEPROM data reset");
    }
    else
    {
      displayEEPROMData();
    }
  }



}

void displayEEPROMData()
{
  // Display all positions stored in EEPROM
  Serial.println("EEPROM:");

  for (int i = 0; i < EEPROM.length(); i++)
  {
    unsigned long timestamp;
    byte value;
    int offset = i * (sizeof(unsigned long) + sizeof(byte));
    EEPROM.get(offset, timestamp);
    EEPROM.get(offset + sizeof(timestamp), value);

    Serial.print(String(i + 1) + ": ");
    printTime(timestamp);
    Serial.println(", " + String(value));
  }

}

void printTime(unsigned long timestamp)
{
  // Calculate time components (hour, minute, second)
  int hours = (timestamp / 1000) % 86400 / 3600;
  int minutes = (timestamp / 1000) % 3600 / 60;
  int seconds = (timestamp / 1000) % 60;

  if (hours < 10) Serial.print("0");
  Serial.print(hours);
  Serial.print(":");
  if (minutes < 10) Serial.print("0");
  Serial.print(minutes);
  Serial.print(":");
  if (seconds < 10) Serial.print("0");
  Serial.print(seconds);
}

Which Arduino board are you using and what is sending data to Serial1?

Leonardo. I haven't sent any new data. Just trying to clear out the time, a reset.

If that's the case, why have you commented these lines and not changed EEPROM_SIZE to EEPROM.length() as suggested.

// Initialize EEPROM data
 //  for (int i = 0; i < EEPROM_SIZE; i++)
 //    EEPROM.write(i, 0);
  if (Serial.available() > 0)
  {
    char inputChar = Serial.read();
    if (inputChar == '0') // If '0' is entered into the serial monitor
    {
      programStartTime = millis(); // Reset the program start time

      for (int i = 0; i < EEPROM.length(); i++)
      {
        EEPROM.update(i, 255);
      }
[...]

EEPROM.length() was put in instead of EEPROM_SIZE.

The other part of the program is commented out because I want it to reset to zero IFF Serial.read() = '0' and not every time at upload/reset. Forget the other part in setup() is there.

In the initialized eeprom after the input of '0', all the cells contain 0xFF. (Decimal 255) and timestamp = 0xFFFFFFFF

17:02:47 is the result of printTime(0xFFFFFFFF)

void setup() {
  Serial.begin(9600);
  unsigned long timestamp = 0xFFFFFFFF;
  printTime(timestamp);
}

void loop() { }

void printTime(unsigned long timestamp)
{
  // Calculate time components (hour, minute, second)
  int hours = (timestamp / 1000) % 86400 / 3600;
  int minutes = (timestamp / 1000) % 3600 / 60;
  int seconds = (timestamp / 1000) % 60;

  if (hours < 10) Serial.print("0");
  Serial.print(hours);
  Serial.print(":");
  if (minutes < 10) Serial.print("0");
  Serial.print(minutes);
  Serial.print(":");
  if (seconds < 10) Serial.print("0");
  Serial.print(seconds);
}
1 Like

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