I want to do an addition of saved values on sd card

Hello everyone, I need help! Please
I've stored some values on an sd card from a sensor reading and I want to do an automatic addition of that values that has been stored on the sd card.

So the sensor stored the values on the sd card in a .txt file format using an arduino uno + Sd card module + DS3231 (module to get the time and date).
I want to add all the values (only the values, not the time and date) and get a "grand total" at the end.

How can I do the automatic addition? Any idea guys

Figure/decide when You want to add that value and when You want to update the SD card.

Where does the addition need to go? In the file or just on screen somewhere ?

If it’s not in the file then

  • create a global variable or suitable type for the sum
  • in setup() read/parse the data in the file and calculate the sum
  • in loop, each time you are have added a new sample to the file, also update the sum

If it’s in the file, you need to decide where you want to have it as it will impact how you write back in the file. As it’s a text file the number of characters to represent the sum will vary and having some padding upfront will make your life easier eg don’t write in the file

Sum = 99

But

Sum =       99

So that you can overwrite one of the space when you write the next sum

Sum =      132

And not change all the file (if it’s not at the end)


Alternatively write a csv file and when you need to do some maths import that into a spreadsheet

You need to read the file, parse it, change values, write it back.

Use sscanf to parse it sprintf to format for writing back.

Can anyone help me out with the code?

post the code you already have

#include "EmonLib.h"                   // Include Emon Library
#include <DS3231.h>
#include <SPI.h>              // Include SPI library (needed for the SD card)
#include <SD.h>               // Include SD library
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

EnergyMonitor emon1;                   // Create an instance1
EnergyMonitor emon2;                   // Create an instance2

DS3231  rtc(SDA, SCL);  // Init the DS3231 using the hardware interface

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display


const int chipSelect = 4;

double sensorValue = 0;
int val[100];
int max_v = 0;
double VmaxD = 0;
double VeffD = 0;
double Veff = 0;

int period = 100;
unsigned long time_now = 0;

void setup()
{
  rtc.begin(); // Initialize the rtc object
  SD.begin(chipSelect);

  lcd.init();                      // initialize the lcd 
  lcd.backlight();    //turn backlight
  lcd.clear();

  
  emon1.current(0, 61.6);             // Current: input pin A0, calibration
  emon2.current(1, 61.6);             // Current: input pin A1, calibration

}

void loop()
{
  double Irms1 = emon1.calcIrms(1480)-0.18;  // Calculate Irms only
  double Irms2 = emon2.calcIrms(1480)-0.18;  // Calculate Irms only
  
  if(millis() >= time_now + period)
  {
    time_now += period;
  {
    for ( int i = 0; i < 100; i++ ) 
  {
    sensorValue = analogRead(A2);
    if (analogRead(A2) > 511) 
    {
      val[i] = sensorValue;
    }
    
    else 
    {
      val[i] = 0;
    }
    delay(1);
  }
  }
  max_v = 0;

  for ( int i = 0; i < 100; i++ )
  {
    if ( val[i] > max_v )
    {
      max_v = val[i];
    }
    val[i] = 0;
  }
  
  if (max_v != 0) 
  {

    VmaxD = max_v;
    VeffD = VmaxD / sqrt(2);
    Veff = (((VeffD - 420.76) / -90.24) * -210.2) + 210.2;
  }
  else 
  {
    Veff = 0;
  }

  }

  VmaxD = 0;

File dataFile = SD.open("log.txt", FILE_WRITE);
  if (dataFile)
 {
   dataFile.print("Date: ");
   dataFile.println(rtc.getDateStr());  //print date
  //---------------------------------------
   dataFile.print("time: ");
   dataFile.println(rtc.getTimeStr());  //print time
  //-----------------------------------
   dataFile.print("ACV: ");
   dataFile.print(Veff);  //print current 1 
   dataFile.println(" V"); 
   //-----------------------------------
   dataFile.print("Amp1: ");
   dataFile.print(Irms1);  //print current 1
   dataFile.println(" A"); 
   //---------------------------------
   dataFile.print("Power1: ");
   dataFile.print(Irms1 * Veff);  //print watt 1
   dataFile.println(" W"); 
  //-----------------------------------
   dataFile.print("Amp2: ");
   dataFile.print(Irms2);  //print current 2
   dataFile.println(" A"); 
   //----------------------------------
   dataFile.print("Power2: ");
   dataFile.print(Irms2 * Veff);  //print watt 2
   dataFile.println(" W");
   //---------------------------------------------------------------------
   dataFile.println("__________________________________________ ");
   dataFile.close();
 }

//***I want to do an addition of all the stored value of Irms1 * Veff and Irms2 * Veff each 10 mins and save it on the sd card***//
//Can you please help me out?

  lcd.clear();
  
  lcd.setCursor(0,0); //set cursor to columns 0, row 0
  lcd.print("PW1: ");
  lcd.print(Irms1 * Veff);
  lcd.setCursor(11,0); //set cursor to columns 11, row 0
  lcd.print("W");

  
  lcd.setCursor(0,1); //set cursor to columns 0, row 1
  lcd.print("PW2: ");
  lcd.print(Irms2 * Veff);
  lcd.setCursor(11,1); //set cursor to columns 11, row 1
  lcd.print("W");


}

This is not good millis handling, has a potential to either skip delay or to delay infinitely because of overflow

Can you clarify the use case?

What is the file for? How do you use it?
Could the file be in binary format to make it easier to process?

What happens when the arduino crashes or looses power or you remove the SD card? Should history be taken into account when you start the Arduino?

You have only one file, how big will you let it grow?

Actually the circuit, it's a current & voltage meter that measured AC voltage and current.
The file log .txt on the sd card contains the data:
Date, time, voltage, current sensor 1 and current 2.
At the end, let say each 10mins, it should add all current sensor 1 and give a grand total, do the same thing for current 2.
unfortunately, I don't know how to do that addition.

no binary format.

"What happens when the arduino crashes or looses power or you remove the SD card? Should history be taken into account when you start the Arduino?"

I will take that into consideration but right now I need someone to help me with this addition thing.

Declare a global variable sum, and every time you go write the file, add into sum whatever you want to keep