Storing an int value to SD card and adding it to a variable

Hello! I'm making a device to display how long my car operates while I'm on trips.
I'm having a rough time figuring out how to store the mins/hours on my SD card module and then reading from the SD card the integer value so I can add more mins into it. Basically the purpose to the SD card is to retain the amount of time my car has been working.

Here's the code I have so far

#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>

const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

File hrcount;
void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  SD.begin(10);
  hrcount = SD.open("add_min.txt", FILE_WRITE);
  hrcount.close();
  hrcount = SD.open("add_hours.txt", FILE_WRITE);
  hrcount.close();
}

int tm = 0;
int con = 0;
int totl = 2000;
unsigned long lastM = 0L;

void loop() {
  //Read mins
  hrcount = SD.open("add_min.txt", FILE_READ);
  //Reset mins and add hour
  if (tm >= 60){
    con += 1;
    totl += 1;
    tm = 0;
    //clear unwated symbols from display
    lcd.setCursor(12, 1);
    lcd.print('1 ');
  }
  hrcount.close();

  //Clears the display after 3mins and displays total hours done overall
  if (millis() - lastM >= 3*60*1000L){
    lastM += 3*60*1000L;
    lcd.clear();
    lcd.setCursor(2, 0);
    lcd.print("TIME DISPLAY");
    lcd.setCursor(1, 1);
    lcd.print("Total:");
    lcd.setCursor(7, 1);
    lcd.print(totl);
  }
  //add 1 min
  hrcount = (SD.open("add_min.txt", FILE_WRITE);
  hrcount.println(tm += 1);
  hrcount.close();
  //Wipe display after 5s
  delay(5000);
  lcd.clear();
  //Basic display information
  lcd.setCursor(2, 0);
  lcd.print("TIME DISPLAY");
  lcd.setCursor(1, 1);
  lcd.print("TRIP:");
  //Trip hours displayed
  lcd.setCursor(12, 1);
  lcd.print(tm);
  lcd.setCursor(6, 1);
  lcd.print(con);
  //delay loop
  delay(55000);
}

I only have the way I tried to do it on the mins variable.
The rest of the code works as expected I just have trouble saving the mins/hour on the SD and then loading them back in the variables to keep counting from where I left from.
I will also store the mins, hours, and total hours on different files to avoid complexion unless it makes it easier.

It would be much easier to store the time on EEPROM than keep track of three separate files on an SD card.

Consider using a unix timestamp. A single 32 bit variable contains the time and date information, with precision of one second. The Arduino TimeLib library handles all the conversions for you.

It would be much easier to store the time on EEPROM than keep track of three separate files on an SD card.

The arduino will have power when my car ignition will be turned on, so I have a couple questions.

Will the EEPROM retain the time after I turn off my car and will it be able to store the total hours I've done while doing trips for example 450hours?

This presents some rather serious electrical problems: automotive electrical circuits are notorious for noise and high voltage spikes, so you need very good filtering for the Arduino power supply. See this application note: https://www.st.com/resource/en/application_note/cd00181783-protection-of-automotive-electronics-from-electrical-hazards-guidelines-for-design-and-component-selection-stmicroelectronics.pdf

EEPROM stores data for decades with the power off. A linux timestamp can record intervals of over a hundred years with one second precision.

Be mindful of the approximate 100,000 write cycle limit. If you update it every minute that is about 1666 hours and then the EEPROM location is wore out...unless you cycle through the EEPROM which would give you 1666.67 hours * 128 or 213,333 hours before having to replace the Arduino (assuming you have 512 bytes of EEPROM).

Similar problems with an SD card, especially given the OP's posted code, which opens a file, writes one variable, and closes it immediately. This leads to vastly increased SD card error rate and current consumption.

  //add 1 min
  hrcount = (SD.open("add_min.txt", FILE_WRITE);
  hrcount.println(tm += 1);
  hrcount.close();

Indeed.

FYI, 213,333 hours is about 24 years. Cycling through EEPROM sounds feasible.

Similar problems with an SD card, especially given the OP's posted code, which opens a file, writes one variable, and closes it immediately. This leads to vastly increased SD card error rate and current consumption.

I'm trying to figure out if it's possible to store the values and adding them to variables that's my concern mostly.

Hence why it is a programming question rather than if the components will endure the torture

Then see post #6. You will reach a write limit eventually, however, no matter whether you use SD or EEPROM.

As we've mentioned, there are several problems with your current ideas that will lead to early failure: electrical system noise, failure of the SD card, overly complicated scheme of storing time and date, etc.

Best to do some more research, thinking and experimenting.

Then see post #6. You will reach a write limit eventually, however, no matter whether you use SD or EEPROM.

I would be very grateful if you could direct me to that post.

The post # is in the upper right hand corner. This post is #12.

The post # is in the upper right hand corner. This post is #12

Thank you! But My programming question is still not answered.

You have not actually explained the "programming question". All we have is this.

The rest of the code works as expected I just have trouble saving the mins/hour on the SD and then loading them back in the variables to keep counting from where I left from.

Please explain what "have trouble" means. Describe what you expect to happen, and what happens instead.

But the basic approach is fundamentally flawed and probably not worth pursuing.

I try to save the values from my variables to my SD card and then load them back in the same variables for when I power the arduino back on.

Instead of calling hrcount.println() you could use hrcount.write() to write a buffer. See SD - write()

In setup() you can read the SD data into a buffer using hrcount.write(). See SD - read()

Could you give me an example please, I used file.write() before but I assume I didn't use it right.

I just quickly coded this up:

#include <SPI.h>
#include <SD.h>

long testData = 147322;

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }

  myFile = SD.open("test.dat", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("testData = ");
    Serial.println(testData);
    Serial.print("Writing testData to test.dat...");
    myFile.write((char *)&testData, sizeof(testData));
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.dat");
  }

  Serial.println("Setting testData to 0");
  testData = 0;

  // re-open the file for reading:
  myFile = SD.open("test.dat");
  if (myFile) {
    Serial.print("reading value from test.dat into testData...");
    myFile.read((char *)&testData, sizeof(testData));
    Serial.println("done.");
    Serial.print("testData = ");
    Serial.println(testData);
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.dat");
  }
}

void loop() {
}

Also, instead of persistently opening and closing the file you can do a seek(0) to rewind before you write the new value.

Has the OP considered asking the car what time it is through the CAN bus?