I have bytes of natural numbers 1-60 coming in at random times.
I want to log the time (any time format) and what value was received into EEPROM:
#include <EEPROM.h>
const int timeRefreshDisplay = 30000;
const int EEPROM_SIZE = 100; // Define size of EEPROM available
int addr = 0; // EEPROM address pointer
unsigned long programStartTime;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600); // Initialize Serial1 for data input
programStartTime = millis(); // Record program start time
}
void loop()
{
if (Serial1.available())
{
int receivedData = Serial1.read();
if (receivedData != 0 && receivedData <= 60)
{
byte receivedValue = receivedData; // Convert?
// Calculate the timestamp based on program start time
unsigned long currentTime = programStartTime + millis();
// Store the received timestamp and value in EEPROM
EEPROM.put(addr, currentTime);
addr += sizeof(unsigned long);
EEPROM.put(addr, receivedValue);
addr += sizeof(int);
}
}
if (millis() - programStartTime >= timeRefreshDisplay)
{
programStartTime = millis();
displayEEPROMData();
}
}
void displayEEPROMData() {
// Display all positions stored in EEPROM
Serial.println("EEPROM:");
for (int i = 0; i < EEPROM_SIZE; i += sizeof(unsigned long) + sizeof(int)) {
if (i >= EEPROM_SIZE) {
i = 0; // Start reading from the beginning when reaching the end of EEPROM
}
unsigned long timestamp;
int value;
EEPROM.get(i, timestamp);
EEPROM.get(i + sizeof(unsigned long), value);
// Display position data with timestamp in HH:MM:SS format
Serial.print(String(i / (sizeof(unsigned long) + sizeof(int)) + 1) + ": ");
Serial.println(String(timestamp) + ", " + String(value));
}
}
Serial monitor
EEPROM:
1: 17330, 22272
2: 101, 27943
3: 4143972352, 116
4: 0, 0
5: 0, 0
6: 0, 0
7: 0, 0
8: 0, 0
9: 0, 0
10: 0, 0
11: 0, 0
12: 0, 0
13: 0, 0
14: 0, 0
15: 0, 0
16: 0, 0
17: 0, 0
I'm not quite sure how it is reading data into EEPROM. That has to be where the issue is. I just don't know how to fix it.