Reading EEPROM value in FIFO manner.

Hi,

I Am implementing data reading with EEPROM of arduino in FIRST IN FIRST OUT but get stucked due to wrong pattern..

I am trying to read data of temperature sensor ,and put that data in EEPROM continuously with RTC data. like "temperature At: 13:37 is 243" ,where 13 represent time ,37 represent minute and 243 correspond to temperature value.

I want to write the data on ,which is first came to EEPROM and continuously work.
same way, which is come first then it must leave first.

I assigned the address location for these three parameter and reading them to combine in the same
string.

[code]

#include <Wire.h>
#include "RTClib.h"
#include <EEPROM.h>

const int sense=A0;
 int addr = 0;
 int value,value1,value2=0;
 int addrhr = 201;
 int addrmin = 401;
 unsigned long MMvalue=0;
 // unsigned long MM=0;
RTC_DS3231 rtc;
void setup () {

Serial.begin(9600);
delay(3000); 

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");

    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 
  }
}

void loop () {
  
    DateTime now = rtc.now();
   
  int adcvalue= analogRead(sense);
  float change=adcvalue*4.88;
  float temp = change/10;
 
   MMvalue= now.minute();
  EEPROM.write(addrmin,MMvalue);
                                                                     
  if( now.minute()!=value2)
 {
  EEPROM.write(addr, temp);
  EEPROM.write(addrhr, now.hour());
  EEPROM.write(addrmin,MMvalue);

  value = EEPROM.read(addr);
  value1 = EEPROM.read(addrhr);
  value2 = EEPROM.read(addrmin);
  Serial.println("temperature At: "+String(value1)+":"+String(value2)+" is "+String(value));

  addr = addr + 1;
   if (addr == 200) {
    addr = 0;
  }
   addrhr = addrhr + 1;
   if (addrhr == 400) {
    addrhr = 0;
  }
  addrmin = addrmin + 1;
   if (addrmin == 600) {
    addrmin= 0;
  }
 delay(3000);
}
}

[/code]

What will I chang to bring that pattren..

EEPROM.write and EEPROM.read are byte based. When using floats, longs and ints, the easiest is to use EEPROM.put and EEPROM.get; that will write and read the correct number of bytes (4, 4 and 2 respectively).

You will also need to calculate the new addresses based on the size of the variables.

@OP

This post and the associated codes may work for you as a tutorial.

#include <Wire.h>     //needed because DS3231 uses I2C Bus
#include <RTClib.h>   //needed becuase we have ready-made functions of this librray
#include<EEPROM.h>

RTC_DS3231 rtc;     //the object rtc is created from the class RTC_DS3231
//#define deviceAddress 0x68
//#define eepromAddress 0x0100
int eepromAddress = 0x0100;

struct
{
  char myString1[50] = "temperature At: "; 
  byte hr;  //nowTime.hour(); 
  char x1[5] = ":"; 
  byte mn; //nowTime.minute(); //1
  char x2[5] = " "; 
  float myTemp; 
  char myString2[50] = " degC"; //5 
} myVar, myVar2;  

void setup()
{
  Serial.begin(9600);
  analogReference(DEFAULT);

  //Serial.println(myVar.myString1);
  if (! rtc.begin())   //DS3231 is intialized
  {
    while (1);        //initialization failed
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
  // rtc.adjust(DateTime(2018, 12, 23, 21, 46, 17));//set date-time manualy:yr,mo,dy,hr,mn,se
}

void  loop()
{
  //--read Date and Tme from DS3231 using the method now()---------------------;
  //--store the Date and Time into another user define object named nowTime
  DateTime nowTime = rtc.now();

  //-----------------------------------------------------
  Serial.print(myVar.myString1);//("temperature At: ");
  myVar.hr = nowTime.hour();
  if (myVar.hr <= 9)
  {
    Serial.print("0");
  }
  Serial.print(myVar.hr);//nowTime.hour());

  Serial.print(myVar.x1);//(":");

  myVar.mn = nowTime.minute();
  if (myVar.mn <= 9)
  {
    Serial.print("0");
  }
  Serial.print(myVar.mn);//(nowTime.minute());

  Serial.print(myVar.x2);//("  ");
  float tempC = (float)100 * (5 / 1024.0) * analogRead(A0);
  myVar.myTemp = tempC;
  Serial.print(myVar.myTemp, 2);//(tempC, 2);

  Serial.print(myVar.myString2);//(" degC");
  Serial.println();
  //---------------------------------------------------------
  EEPROM.put(eepromAddress, myVar);  //storing in EEPROM
  EEPROM.get(eepromAddress, myVar2);  //reading bcak the contents of EEPROM and put into myVar2
  //--------------------------------------------------------
  Serial.println("==Reading back from EEPROM====");
  Serial.print(myVar2.myString1);

  if (myVar2.hr <= 9)
  {
    Serial.print("0");
  }
  Serial.print(myVar2.hr);//nowTime.hour());

  Serial.print(myVar2.x1);//(":");

  if (myVar2.mn <= 9)
  {
    Serial.print("0");
  }
  Serial.print(myVar2.mn);//(nowTime.minute());

  Serial.print(myVar2.x2);
  Serial.print(myVar2.myTemp, 2);//testing the correct operation on EEPROM
  Serial.println(myVar2.myString2);
  Serial.println();

  Serial.println("=====Real Time Display======");
  eepromAddress = eepromAddress + 0x74;
  // eepromAddress = eepromAddress + sizeof(myVar.myString1)
  //  + sizeof(myVar.hr)+sizeof(myVar.x1) +  sizeof(myVar.mn)
  //  + sizeof(myVar.x2)+sizeof(myVar.myTemp)
  // + sizeof(myVar.myString2);

  Serial.print(eepromAddress, HEX);
  Serial.println("(New EEPROM Location)");
  delay(59999);
}

sm28.png

sm28.png

Thanks for reply,

I forgot to tell about my main target is that the data should overwrite on the location after 60 reading as per my plan ,

Actually there is one button, when I press ,want the previous 60 reading which I
continuously storing in EEPROM.

Whenever I press button its must show data of previous 60 reading from current timing and same work would to be carry forword with EEPROM to store data by overwriting on 1st reading of the EEPROM storage .then again for next iteration the data should efface for the 1st reading and write new one for current timing.

just like the following simple program that I write and read the data of 10 location and when I press button it shows the data for 5th location as per my code...

similarly when I press button then it will show the last 60 reading which is storing in EEPROM.

#include <EEPROM.h>
int addr = 0;
byte value;
int lastaddr = 40;
byte num [10]={1,2,3,4,5,6,7,8,9,10};

void setup()
{
 Serial.begin(9600);
 pinMode(9, INPUT_PULLUP);
}

void loop() {

  //int adcvalue= analogRead(A0);
  //float change=adcvalue*4.88;
  //float temp = change/10;
 
  //EEPROM.write(addr, array[i]);
  for ( int i = 0; i < 10; ++i ){
    EEPROM.write ( i, num[ i ] );
  

 // value = EEPROM.read(addr);
  //for ( int i = 0; i < 10; ++i )
    EEPROM.read ( i);

  Serial.print(i);
  Serial.print("\t");
  Serial.print(num [ i ], DEC);
  Serial.println("\n");
  
  }

   if (digitalRead(9)==LOW){

    Serial.println("After buttonpress :");

  byte value= EEPROM.read (num [4]);
   
   Serial.println(value);


   }
   
  //addr = addr + 1;
  //if (addr == lastaddr) {
  //  addr = 0;
 // }
 delay(1000);
}

Thats it....

could you help me to change in your following [posted] code for button operation please ?

#include <Wire.h>     //needed because DS3231 uses I2C Bus
#include <RTClib.h>   //needed becuase we have ready-made functions of this librray
#include<EEPROM.h>

RTC_DS3231 rtc;     //the object rtc is created from the class RTC_DS3231
//#define deviceAddress 0x68
//#define eepromAddress 0x0100
int eepromAddress = 0x0100;

struct
{
  char myString1[50] = "temperature At: "; //16
  byte hr;  //nowTime.hour(); //1
  char x1[5] = ":"; //1
  byte mn; //nowTime.minute(); //1
  char x2[5] = " "; //1
  float myTemp; //4
  char myString2[50] = " degC"; //5 
} myVar, myVar2;  //29

void setup()
{
  Serial.begin(9600);
  analogReference(DEFAULT);

  //Serial.println(myVar.myString1);
  if (! rtc.begin())   //DS3231 is intialized
  {
    while (1);        //initialization failed
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
  // rtc.adjust(DateTime(2018, 12, 23, 21, 46, 17));//set date-time manualy:yr,mo,dy,hr,mn,se
}

void  loop()
{
  //--read Date and Tme from DS3231 using the method now()---------------------;
  //--store the Date and Time into another user define object named nowTime
  DateTime nowTime = rtc.now();

  //-----------------------------------------------------
  //Serial.print(myVar.myString1);//("temperature At: ");
  myVar.hr = nowTime.hour();
 // if (myVar.hr <= 9)
 // {
   // Serial.print("0");
  //}
  //Serial.print(myVar.hr);//nowTime.hour());

  //Serial.print(myVar.x1);//(":");

  myVar.mn = nowTime.minute();
  //if (myVar.mn <= 9)
  //{
   // Serial.print("0");
 // }
  //Serial.print(myVar.mn);//(nowTime.minute());

  Serial.print(myVar.x2);//("  ");
  float tempC = (float)100 * (5 / 1024.0) * analogRead(A0);
  myVar.myTemp = tempC;
 // Serial.print(myVar.myTemp, 2);//(tempC, 2);

 // Serial.print(myVar.myString2);//(" degC");
 // Serial.println();
  //---------------------------------------------------------
  EEPROM.put(eepromAddress, myVar);  //storing in EEPROM
  EEPROM.get(eepromAddress, myVar2);  //reading bcak the contents of EEPROM and put into myVar2
  //--------------------------------------------------------
 // Serial.println("==Reading back from EEPROM====");
  Serial.print(myVar2.myString1);

  if (myVar2.hr <= 9)
  {
    Serial.print("0");
  }
  Serial.print(myVar2.hr);//nowTime.hour());

  Serial.print(myVar2.x1);//(":");

  if (myVar2.mn <= 9)
  {
    Serial.print("0");
  }
  Serial.print(myVar2.mn);//(nowTime.minute());

  Serial.print(myVar2.x2);
  Serial.print(myVar2.myTemp, 2);//testing the correct operation on EEPROM
  Serial.println(myVar2.myString2);
  //Serial.println();

  //Serial.println("=====Real Time Display======");
  eepromAddress = eepromAddress + 32;
  // eepromAddress = eepromAddress + sizeof(myVar.myString1)
  //  + sizeof(myVar.hr)+sizeof(myVar.x1) +  sizeof(myVar.mn)
  //  + sizeof(myVar.x2)+sizeof(myVar.myTemp)
  // + sizeof(myVar.myString2);

  //Serial.print(eepromAddress, HEX);
//  Serial.println("(New EEPROM Location)");
  delay(59999);
}

If you still couldn't figure it out, lemme tell you that you need two variables (address to read and address to write) and probably just a single struct variable (because you can reuse that space) unless the read and write processes weren't atomical (i.e. if the data retrieval and usage may become interrupted by the opposite operation).

Speaking about the struct, you don't need to store that much information; with the basic data is enough. Storing the actual string is redundant when you can reconstruct it on demand and with fewer data (so only the timestamp and temperature is required for this).
This way you save some effort to the poor EEPROM.