# Read Last Value of SD CARD

Hi, I am using a ATMEGA 328p with SD module and RTC DS3231 a waterflow sensor and a water flow generator. The sensor gives me the amount of liters used when its powered by the generator (when water flows). Every 7 sec, the current time and the liters are updated on the sd card. The point is that i need to read the ñast values of it and depending of the time of the last value give, the sd card must be reset. For example:

28/06/2020 17:04 --> 14 Liters

if 3 minutes have not passed:

28/06/2020 17:05 --> 14 Liters

if 3 minutes have passed:

28/06/2020 17:09 --> 0 Liters

How can I do it?

When you say, "the SD card must be reset", do you actually mean, "the cumulative Liter count must be reset to zero"?

In that case, the logic is very simple:

read the last time logged from the SD
if (current time - last time logged > 7 seconds) { literCount = 0 }

And how I can't read the last value that was writen? The device would be powered every (aprox) +- 5 hours. How can I say. Read the last value. If it's posssible how I separate the number to the time and the Liters?:

Imagine:

18:06:33 , 17 Liters

This to:

18:06:33
&
17 liters

Maybe post your code and we can help?

blh64:
Maybe post your code and we can help?

``````void(* resetFunc) (void) = 0;
byte sensorInterrupt = 0;
byte sensorPin       = 2;
float calibrationFactor = 4;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;

#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
File myFile;
DS3231  rtc(SDA, SCL);
int pinCS = 53;

void setup() {

Serial.begin(9600);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount        = 0;
flowRate          = 0.0;
flowMilliLitres   = 0;
totalMilliLitres  = 0;
oldTime           = 0;
attachInterrupt(digitalPinToInterrupt(sensorPin), pulseCounter , FALLING);

pinMode(pinCS, OUTPUT);
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
rtc.begin();

}

void loop() {

Serial.print(rtc.getTimeStr());
Serial.print(",");
Serial.print(totalMilliLitres);
Serial.println(" mL");

myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
myFile.print(rtc.getTimeStr());
myFile.print(",");
myFile.println(totalMilliLitres);
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}

delay (2000);

if((millis() - oldTime) > 300)    {

detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60)*2000;
totalMilliLitres += flowMilliLitres;
unsigned int frac;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
pulseCount = 0;
}
}

void pulseCounter(){
pulseCount++;
}
``````
``````file.seek(file.size()-30);
``````

Then read forward to the end of the next-to-last line. Then read the last line.