Go Down

Topic: Calculate statistic in EEPROM (Read 1 time) previous topic - next topic


Jul 01, 2019, 04:01 am Last Edit: Jul 01, 2019, 04:04 am by bemp

I have checked the statistic library in Arduino.
Can it be used with data in EEPROM?. I've search on how to do statistic calculation in EEPROM, but non related as in the attachment.

The data that I stored is body temperature.
Currently I stored the data using float.

Below is my code for data store in eeprom.

Code: [Select]

 addr = count;               //Universal dayCurrent (BBT)
    addr1 = addr - 10;           //EEPROM dayBefore (BBT)
    addr2 = addr - 20;           //EEPROM dayPass (BBT)
    EEPROM.put(addr, t);


The 'count' in
Code: [Select]
addr = count
represent the number of data that have been inserted into the eeprom and also represent the address value.

I need suggestion links or maybe any idea on how to do statistics in EEPROM.

Best Regards


You're not likely to find much information with a search for statistic calculation in EEPROM. You need to understand this is about two separate things:
  • How to read data from EEPROM
  • How to do the statistical calculation on data

You'll find plenty enough information on each of those topics. Start by learning to do each task individually. Once you fully understand that and have it working perfectly, then you only need to figure out how to combine the two tasks in a single program.


Sir Pert,

Here I attach my code as I looked at your comment.

Code: [Select]
#define LM35 A1
#define READSAMPLES 100

float bt;
int buttonPin1 = D5;     // the number of the pushbutton pin 1 MEASURE
int buttonPin2 = D6;     // the number of the pushbutton pin 2 RESET CYCLE
int buttonState = 0;         // variable for reading the pushbutton status
int ButtonState = 0;         // variable for reading the pushbutton status
int address = 0;      //EEPROM address counter
int count, counT;
int sample[READSAMPLES];

void setup() {
    int contCount = EEPROM.get(5, count) / 10;


void loop() {
  buttonState = digitalRead(buttonPin1);
  ButtonState = digitalRead(buttonPin2);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {
        Serial.println("Measuring your Body Temperature:");
        count = counting();
        counT = count / 10;
        Serial.print("Data count entering: ");

  if (ButtonState == LOW) {
        Serial.println("Update New Cycle of body temp...");
        Particle.publish("DONE new Cycle");

void printTemp()
  for (int i = 0 ; i < EEPROM.length() ; i++) {
    float cel = EEPROM.read(i);                //read EEPROM data at address i
    //How to find MEAN of all data?

void clearEEPROM()
  for (int i = 0 ; i < EEPROM.length() ; i++) {
    if(EEPROM.read(i) != 0)                     //skip already "empty" addresses
      EEPROM.write(i, 0);                       //write 0 to address i
  Serial.println("EEPROM erased");
  address = 0;                                  //reset address counter
void writeTemp()
  //getting the voltage reading from the temperature sensor
  int j;
  float average;
  for (j = 0; j < READSAMPLES; j++)
    sample[j] = analogRead(LM35);        //read sensor value

  average = 0;
  for (j = 0; j < READSAMPLES; j++)
    average += sample[j];
  average /= READSAMPLES;

  //float mv = ( average / 4096.0) * 3300;
  //float cel = ( mv / 10 ) - 55.01;
  float mv = ( average / 1024.0) * 5000;
  float cel = ( mv / 10 );
  EEPROM.write(address, cel);         //write value to current address counter address
  Serial.print("Sensor value stored at address ");
  address++;                      //increment address counter
  if(address == EEPROM.length())  //check if address counter has reached the end of EEPROM
    address = 0;              //if yes: reset address counter

float counting()
  count = count + 10;
    Particle.publish("Start Count =", String(count));
  return count;

However, I'm stuck at this part,

Code: [Select]
void printTemp()
  for (int i = 0 ; i < EEPROM.length() ; i++) {
    float cel = EEPROM.read(i);                //read EEPROM data at address i
    //How to find MEAN of all data?

beacuse, I know how to calculate average in array using for loop statement.

Do you have any improvement on this idea?


EEPROM.write() and EEPROM.read() can only write/read a single byte from EEPROM. A byte can hold an integer value from 0-255. Yet you're trying to use these functions to store floating point data. This will cause a conversion from the float data type to the byte data type, which may very well not be what you want.

It seems like you might have skipped over the first step, which is to learn read (and write) data from EEPROM. Just write a simple sketch that does that only first. Make sure to use some Serial.println() statements to check whether the results are as you expect them to be. If you do need to store float data in EEPROM, you should use EEPROM.put() and EEPROM.get(). Remember that each float will take 4 bytes of EEPROM, so you need to increment the EEPROM address you write/read to by 4 each time. If you only increment it by 1 then you will overwrite three bytes of the previous data point and end up with garbage data.

You can find lots of information about the EEPROM library here:


Hi, everyone..

I have some news about my problem.
Now, I'm almost sure that I can do statistics using data in EEPROM.
Here I attach the code.

Code: [Select]

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  ButtonState = digitalRead(ButtonPin);
  EEPROM_END = count;
    if (buttonState == HIGH) {
      t = writeTemp();
        EEPROM.write(address, t);         //write value to current address counter address
        Serial.print("Temp Value:");
        Serial.print("Sensor value stored at address:");
        count = address;
    else if (ButtonState == HIGH) {
        float mean;
        for (int i = EEPROM_START; i < EEPROM_END; i++)
            sum = sum + EEPROM.read(i);
        mean = sum / count;
            Serial.print("Average value:");
            Serial.println(mean );

However, I'm getting 0 value when I'm doing sum operation.
Need your idea on how can I get the sum operation better.

Hope, we can together improve the code.


Please post your full sketch.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.


Jul 23, 2019, 08:17 am Last Edit: Jul 23, 2019, 08:18 am by GolamMostafa

Please, start with this simple job first:

Store these 5 numbers 11.23, 15.76, 13.75, 13.54, 12.45 into the internal EEPROM of the MCU of UNO Board. Read them back, do the average and show the result 13.346 on the Serial Monitor. Kindly. post your codes.

Go Up