/* Session 4 — Local storage without an SD card: EEPROM ring log (Arduino Uno / Tinkercad) Logs temperature (TMP36 A0) + soil moisture (A1) every LOG_EVERY_MS into the 1 KB EEPROM. Each record = 4 bytes: int16 temp*10, uint8 soil%, uint8 sequence. 250 records fit. Commands from the Serial Monitor: d = dump CSV, c = clear (Real hardware with SD + deep sleep: s04_sd_logger_esp32) */ #include const unsigned long LOG_EVERY_MS = 5000; // use 15 min (900000) in the field const int REC_SIZE = 4; const int MAX_REC = (1024 - 2) / REC_SIZE; // first 2 bytes store the write index unsigned long lastLog = 0; int readIndex() { int i; EEPROM.get(0, i); return (i < 0 || i >= MAX_REC) ? 0 : i; } void writeRecord(int16_t temp10, uint8_t soil, uint8_t seq) { int i = readIndex(); int addr = 2 + i * REC_SIZE; EEPROM.put(addr, temp10); // EEPROM.put only writes changed bytes (saves wear) EEPROM.update(addr + 2, soil); EEPROM.update(addr + 3, seq); EEPROM.put(0, (i + 1) % MAX_REC); } void dump() { Serial.println(F("record,temp_c,soil_pct,seq")); for (int i = 0; i < MAX_REC; i++) { int addr = 2 + i * REC_SIZE; int16_t t; EEPROM.get(addr, t); uint8_t s = EEPROM.read(addr + 2), q = EEPROM.read(addr + 3); if (s == 255) continue; // empty (erased EEPROM = 0xFF) Serial.print(i); Serial.print(','); Serial.print(t / 10.0, 1); Serial.print(','); Serial.print(s); Serial.print(','); Serial.println(q); } } void setup() { Serial.begin(9600); Serial.println(F("EEPROM logger. Send 'd' to dump, 'c' to clear.")); } void loop() { if (Serial.available()) { char c = Serial.read(); if (c == 'd') dump(); if (c == 'c') { for (int i = 0; i < 1024; i++) EEPROM.update(i, 255); EEPROM.put(0, 0); Serial.println(F("cleared")); } } if (millis() - lastLog >= LOG_EVERY_MS) { lastLog = millis(); float t = (analogRead(A0) * 5.0 / 1023.0 - 0.5) * 100.0; uint8_t soil = constrain(map(analogRead(A1), 0, 876, 0, 100), 0, 100); static uint8_t seq = 0; writeRecord((int16_t)(t * 10), soil, seq++); Serial.print(F("logged #")); Serial.print(readIndex()); Serial.print(F(" t=")); Serial.print(t, 1); Serial.print(F(" soil=")); Serial.println(soil); } }