#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);
}
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);
}
#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);
}
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.