I have bytes of natural numbers 1-60 coming in at random times:
Example of send data
// Uno send
void setup() {
Serial.begin(9600);
int integersToSend[] = {1, 22, 30, 4, 50};
for (int i = 0; i < 5; i++)
{
Serial.print(integersToSend[i]); // send as characters
delay(1000);
}
}
void loop() {}
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. I tried clearing out the memory:
#include <EEPROM.h>
void setup() {
// initialize the LED pin as an output.
pinMode(13, OUTPUT);
for (int i = 0 ; i < 22; i++) /*EEPROM.length()*/
EEPROM.write(i, 0);
// turn the LED on when we're done
digitalWrite(13, HIGH);
}
void loop() {}
Serial monitor again
EEPROM: 1: 0, 0 2: 0, 0 3: 0, 0 4: 0, 4 5: 70, 1 6: 22, 3 7: 230, 2 8: 182, 4 9: 134, 50 10: 234, 49 11: 209, 50 12: 213, 50 13: 185, 51 14: 189, 48 15: 161, 52 16: 137, 53 17: 140, 48
I'm using a Leonardo to receive. Budget is $20? I thought this was a simple thing, but struggling hard. Already sunk 22 hours into this.