0

On an Arduino, I'm recording some data to external EEPROM (24LC256 by I2C) in a CSV format.

My sketch to Write seems to work , as indicated by troubleshooting code I added that after every write, does a read and prints to serial monitor.

But my sketch to Read to Serial Monitor fails b/c repeatedly reads off the same data. (I'm not sure it is actually row/record.)

I got the same result with 2 different I2C/EEPROM modules, 2 different Arduinos and with similar code written by two people.

--- Here is Write sketch (with lines to read that I added for troubleshooting) : result to Ser Mon is correct: rows of CSV with incrementing SampleCounter and values as expected from random().

/* Library: SparkFun_External_EEPROM_Arduino_Library Stores serial number and 2 values Also reads to get confirmation on Ser mon.*/

#include <Wire.h>
#include "SparkFun_External_EEPROM.h" // 
ExternalEEPROM myMem;
String sample2Write = "";     // build up data from one sample
String sampleRead = "";
int sampleCounter = 0;
int sampleSize = 32;  // bytes

void setup() //////////////////////////////////////////
{
  Serial.begin(9600);
  Wire.begin();
  if (myMem.begin() == false) {
    Serial.println("No memory detected. Freezing.");
    while (1);
  }
} // setup

void loop() {
  delay(1000);
  sampleCounter++;

  // WRITE  ////////////////////////////////////////////////////
  sample2Write = "";
  sample2Write = sample2Write + String(" - ") + "," ; // for future
  sample2Write = sample2Write + String(sampleCounter) + "," ;
  sample2Write = sample2Write + String(random(10, 20)) + "," ;
  sample2Write = sample2Write + String(random(21, 30));
  myMem.put(sampleCounter * sampleSize, sample2Write); //(location, data)

  // following read is just to observe data real-time for troubleshooting
  // actual read is different sketch
  sampleRead = "";
  myMem.get(sampleCounter * sampleSize, sampleRead); // location, receiver var
  Serial.println(sampleRead);
} // loop

--- Here is Read.ino sketch: Problem is Ser Mon shows rows of CSV but always the same data (probably not even actual saved values)

/* Library:   SparkFun_External_EEPROM_Arduino_Library*/
#include <Wire.h>
#include "SparkFun_External_EEPROM.h" // 
ExternalEEPROM myMem;
String sampleRead = "";
int sampleSize = 32;  // bytes
int sampleCounter = 0;

void setup() //////////////////////////////////////////
{
  Serial.begin(9600);
  Wire.begin();
  if (myMem.begin() == false){
    Serial.println("No memory detected. Freezing.");
    while (1); }
} // setup

void loop(){
  delay(300);
//    READ DATA then display to serial monitor    //////////
  sampleCounter++;
  sampleRead="",
  myMem.get(sampleCounter*sampleSize, sampleRead); // location, receiver var
  Serial.println(sampleRead);
} // loop
Playing with GAS
  • 565
  • 3
  • 10
  • 18
  • Your `Read.ino` appears to be two sketches smashed together - I would expect multiple "duplicate definition" errors if you actually tried to compile it like that. One of them has a `sampleSize` of 32, matching the sketch that wrote the data; the other one has 200, which cannot possibly work. What exactly are you running? (You also have a problem that the write sketch doesn't stop when it reaches the end of the EEPROM.) – jasonharper Mar 26 '23 at 19:55
  • Jason: I accidently pasted twice. Very embarrassing. Now edited. – Playing with GAS Mar 26 '23 at 21:33
  • Jason: I accidently pasted the read.ino code twice. Very embarrassing. Now edited. I will look at prevention of over-running end of EEPROM. So far in testing we have added one sample every few seconds for a minute so at this point only 1% of capacity. For the read problem: by chance is there like some kind of pointer left behind in the EEPROM and the reads actually start not at zero but at the pointer? Just an idea but can't find any reference to that. Much thanks for your time and attention. – Playing with GAS Mar 26 '23 at 21:39
  • Avoid using a String for your put/get as Library uses Sizeof() to get data length (2 for string instead of string length). use char array [32] and set memorySize. Start with library examples to ensure all is OK, hard and soft, speed, mem size etc... – tuyau2poil Mar 27 '23 at 09:00
  • Thanks for tips. I'm getting closer by using above. – Playing with GAS Mar 29 '23 at 16:58

0 Answers0