store variable on SD then use it at a later date

I want a variable that will increase due to an event to be stored onto my SD card.

In the event I lose power then it returns I want to access the variable stored on the SD card.

That way the events that occur when power is on will continue to be counted from the last number of occurrences.

This is what I have done so far and it counts the variable up and stores it correctly, but obviously resets to zero in the event the board is reset.

Any guidance would be greatly appreciated.

void MenuC()
{  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("# of Operations");
    
    while(ReadKeypad()!= 'L')
    {
         if (ReadKeypad() == 'U')
      {       
      var = var + 1;
      lcd.setCursor(0,1);
      lcd.print(var);
      delay(500);
      }
     varlog(var);
    }
}


void varlog(int ops)
 {
  int var = ops;
  // lcd log event
  SD.remove(filename4);
  File dataFile = SD.open(filename4, FILE_WRITE);
  if (dataFile) 
    {
  dataFile.print(var);
  dataFile.close();
  }
   else
  {
    Serial.println("Couldn't open log file");
    return;
  }
}

Thank you in advance for your help.

For this kind of application EEPROM is much better.

How would I accomplish with EEPROM?

To confirm the variable would still have the same value if the board lost power?

void MenuC()
{  
    while(ReadKeypad()!= 'L')
    {
    dataFile = SD.open(filename4);
  if (dataFile) 
    {
    int input = dataFile.parseInt();
    // read from the file until there's nothing else in it:
    myIntVal = input;  
    }
    dataFile.close();
    // close the file:
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("# of Operations:");
    lcd.setCursor(0,1);
    lcd.print(myIntVal);    
         if (ReadKeypad() == 'U')
      {       
      myIntVal = myIntVal + 1;
      lcd.setCursor(0,1);
      lcd.print(myIntVal);
      SD.remove(filename4);
      File dataFile = SD.open(filename4, FILE_WRITE);
      if (dataFile) 
        {
          dataFile.print(myIntVal);
          dataFile.close();
        }
      else
          {
           Serial.println("Couldn't open log file");
            return;
          }  
      }
   }
}

I managed to achieve it with the above code

This is what I have done so far and it counts the variable up and stores it correctly, but obviously resets to zero in the event the board is reset.

You must have a reference for the recorded event. The reference could be a non-zero serial-number (or a time-stamp from an off-Arduino always-on source, say RTC.)

On power-up, a counter would be zero and the value read from SD would be non-zero, just swap and continue.

Ray

I have achieved storing a variable to the SD then starting where I left off in the event of a power off. I wanted to find out if storing it in EEPROM would be the better solution as someone has previously mentioned.

My memory is quite limited as the program I have written is so far taking up 55% of program storage space and 75% of dynamic memory. This is why I thought keeping things on the SD as much as possible would be useful.

I am however not very experienced in programming so I could be completely mistaken.

I am however not very experienced in programming so I could be completely mistaken.

If you must use the SD for some reason other than the power-on variable storage, the why complicate with eeprom?

If, however you are only using the SD for storing 1 variable, that is too high of a price in resources... eliminate SD and recover all of the code space and SRAM, and just use the eeprom.

Ray