Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EEPROM.h>
- const int EEPROM_SIZE = 2048;
- const int PAGE_SIZE = 32;
- const int CHUNK_SIZE = sizeof(double);
- int writeIndex = 0;
- void writeData(double data) {
- int currentPos = findCurrentPosition();
- byte dataBytes[CHUNK_SIZE];
- memcpy(dataBytes, &data, sizeof(double));
- EEPROM.put(currentPos, dataBytes);
- // Erase the next page if we're at the last chunk of the current page
- if ((currentPos + CHUNK_SIZE) % PAGE_SIZE == 0) {
- int nextPageStart = (currentPos + CHUNK_SIZE) % EEPROM_SIZE;
- byte emptyPage[PAGE_SIZE] = {0};
- EEPROM.put(nextPageStart, emptyPage);
- }
- EEPROM.commit();
- }
- int findCurrentPosition() {
- for (int i = 0; i < EEPROM_SIZE; i += CHUNK_SIZE) {
- bool isEmpty = true;
- for (int j = 0; j < CHUNK_SIZE; j++) {
- if (EEPROM.read(i + j) != 0x00) {
- isEmpty = false;
- break;
- }
- }
- if (isEmpty) {
- return i;
- }
- }
- return 0;
- }
- void writeDataEverySecond() {
- double data = random(0, 100) / 1.0;
- writeData(data);
- writeIndex++;
- delay(1000);
- }
- void setup() {
- Serial.begin(9600);
- EEPROM.begin(EEPROM_SIZE);
- }
- void loop() {
- writeDataEverySecond();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement