Read and store unsigned long int values from SD card

Hi. Iam new to arduino. Iam on a project of calculating utilisation,breakdown and idle time of my machine. I could get the utilisation time from calculating millis() and manipulate the data Wat I required... The problem is after power of arduino fails the millis() start resets... So i had decided to store the data to an SD card and read values from SD card and start my manipulation part... I could store the value in SD card.... But i wanted to store only in single line like 30000,400005,500006. I wanted to read this values written in SD card and store to an unsigned integer variable... So i can recover the previous utilisation time... I wanted to store only in single line and read from file.... When next time loop runs it has to overwrite in the same line by which i can read my values...

Could any one help how to make this possible.

Could any one help how to make this possible.

What part(s) do you need help with? Are you currently writing data to a file? If so, where is the code that does that?

Are you currently opening the file for read? If so, where is the code for that? Are you currently reading anything from the file? If so, well, you know the drill by now.

Are you currently parsing the data that you read and stored? If so, ...

What problems are you having?

Hi...

Am new to arduino. Iam on a project of calculating utilisation time of my machine. Iam measuring millis() in in three conditions... When machine runs,idle state and breakdown i will start calculating millis in three different variables elapsedTime11, elapsedTime22 and elapsedTime33.

I wrote those variables in SD card in only first line every time and will read again and have to store in some variables for calculation... I got a problem in this... How to write always in first line alone and how to read and store in three other variables X,Y,Z.

Finally will display in 20X4 display..
The problem is after power failure i couldn't retrieve previous timing since millis() gets reset and starts from starting....

So i have decided to store the values of this three variables in SD card as an text file as below format. And i could write on the file.

elapsedTime11, elapsedTime22,elapsedTime33.

But it's writing continously in next line. But i have not used println().
I wanted always to write only in first line alone and I wanted to read this values and store in some other unsigned long variables. Were I can use SD card as memory for previous time before poweroff.
From the values from SD card i can calculate the time...

Help me how to do this..
overwrite in same line.
Reading and store in unsigned long int variables.

I don't know how to read and store... I need a method to do this and I needed some coding help on this...

I have given my coding here

include <RTClib.h>

include <SPI.h>

include <SD.h>

include<LiquidCrystal.h>

include<Wire.h>

byte value;
LiquidCrystal lcd(8 ,7 ,5 ,4 ,3 ,2); // pins connected to LCD
const int chipSelect = 10;
File myFile;//
File logFile;
int greenlamp=A1; //analog input read int
int yellowlamp=A2; //analog input read int
int redlamp=A3; //analog input read int
int greenval=0; // intialize
int yellowval=0; //intialize
int redval=0; //intialize
int Utilized; //cal result int
int Idle; //cal result int
int Breakdown; //cal result int
int Runtime; //cal result int
int addr = 0;
int address = 0;
unsigned long startTime1=0;
unsigned long elapsedTime11;
unsigned long elapsedTime111;
unsigned long elapsedTime1=0;
unsigned long startTime3=0;
unsigned long elapsedTime33;
unsigned long elapsedTime333;
unsigned long elapsedTime3=0;
unsigned long runtime1=0;
unsigned long runtime2;
unsigned long runTime1;
unsigned long runTime11;
unsigned long previousMillis=0;
unsigned long startTime2=0;
unsigned long elapsedTime22;
unsigned long elapsedTime222;
unsigned long elapsedTime2=0;
int resetinterval=30000;
unsigned long int X=0;
unsigned long int Y=0;
unsigned long int Z=0;
RTC_DS1307 RTC;
void setup()
{
lcd.begin(20,4); //open the LCD
Serial.begin(9600);
Wire.begin();
RTC.begin();
if (!RTC.begin()); //RTC initialization
{
Serial.print("RTC is NOT running ");
//RTC.adjust(DateTime(DATE, TIME));
}
if (!SD.begin(10)) //sd card initialization
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization Done ");

}
void loop()
{
DateTime now=RTC.now();
now = RTC.now();
runTime1 = millis();
if
(analogRead(greenlamp) < 100) // input comparison
{
startTime1 = millis();
elapsedTime111=elapsedTime11;
}
if
(analogRead(greenlamp) >100)
{
elapsedTime1=millis()-startTime1;
elapsedTime11=elapsedTime1+elapsedTime111;
}
if
(analogRead(yellowlamp) < 100)
{
startTime2 = millis();
elapsedTime222=elapsedTime22;
}
if
(analogRead(yellowlamp) > 100)
{
elapsedTime2=millis()-startTime2;
elapsedTime22=elapsedTime2+elapsedTime222;
}
if
(analogRead(redlamp) < 100)
{
startTime3 = millis();
elapsedTime333=elapsedTime33;
}
if
(analogRead(redlamp) > 100)
{
elapsedTime3=millis()-startTime3;
elapsedTime33=elapsedTime3+elapsedTime333;
}

logFile=SD.open("LOGGFILE.txt", FILE_WRITE);
if (logFile) {
Serial.print("Writing to test.txt...");
logFile.print(elapsedTime11);
logFile.print(" , ");
logFile.print(runTime1);
logFile.print(" , ");
logFile.print(elapsedTime33);
//close the file:
myFile.close();
Serial.print("done.");
} else
{
// if the file didn't open, print an error:
Serial.println("error opening storefile.txt");
}
logFile=SD.open("LOGGFILE.txt");
if (logFile) {
Serial.print("SAMPLE.txt:");
//read from the file until there's nothing else in it:
while (logFile.available())
{
X=logFile.parseInt();
Y=logFile.parseInt();
Z=logFile.parseInt();
}
//close the file:
logFile.close();
} else {
//if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

float Utilized=( (X/28800000)*100);
float Idle=(((float)(Y-(X+Z))/28800000)*100);
float Breakdown=(((float) Z/28800000)*100);
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("RUN :");
lcd.print(Utilized);
lcd.print(" % ");
lcd.setCursor(0,2);
lcd.print("Idle :");
lcd.print(Idle);
lcd.print(" % ");
lcd.setCursor(0,3);
lcd.print("Breakdown :");
lcd.print(Breakdown);
lcd.print(" %");
lcd.setCursor(0,0);
lcd.print(now.day(),DEC);
lcd.print('/');
lcd.print(now.month(),DEC);
lcd.print('/');
lcd.print(now.year(),DEC);
lcd.print('.');
lcd.print(now.hour(),DEC);
lcd.print(':');
lcd.print(now.minute(),DEC);
lcd.print(':');
lcd.print(now.second(),DEC);
lcd.print('.');
delay(1000);
}

Also record those calculated data in another file when my condition is satisfied...
if
(now.hour() < 8) // hour comparison
{
myFile=SD.open("LOGFILE1.txt", FILE_WRITE);
{
myFile.println();
myFile.print(now.day(),DEC);
myFile.print("/");
myFile.print(now.month(),DEC);
myFile.print("/");
myFile.print(now.year(),DEC);
myFile.print("-");
myFile.print(now.hour(),DEC);
myFile.print(':');
myFile.print(now.minute(),DEC);
myFile.print(':');
myFile.print(now.second(),DEC);
myFile.print("==------==");
myFile.print("RUN :");
myFile.print(Utilized);
myFile.print(" % ");
myFile.print("Idle :");
myFile.print(Idle);
myFile.print(" %");
myFile.print("Breakdown :");
myFile.print(Breakdown);
myFile.print(" %");
myFile.println();
//close the file:
myFile.close();
}

}

}

                logFile=SD.open("LOGGFILE.txt", FILE_WRITE);
                 if (logFile) {
                       Serial.print("Writing to test.txt...");
                        logFile.print(elapsedTime11);
                        logFile.print(" , ");
                        logFile.print(runTime1);
                        logFile.print(" , ");
                        logFile.print(elapsedTime33);
                         //close the file:
                        myFile.close();

If you only want one line in the file, why are you opening the file in append mode? Look at the possible values for the second argument to the open() function, and pick the appropriate mode.

Why
is
every
if
statement
split
up
so
weirdly?

Thanks for replying..

I have opened because i need to overwrite values every time.. So it can act as non volatile even after power shuts..

If it's written in one line i thought it would be easier to read those values... Tats it.

After writing it i have to retrieve these three datas to store in X,Y,Z.... From that were i can have my calculations...

Is tre any specific mode/methods for this....
Pls suggest an idea...

I have opened because i need to overwrite values every time.

There are two ways to open a file - append mode and overwrite mode. Using append mode, the default, is NOT the way to accomplish your goal.

Is there any specific instructions/code for overwriting text file..

I could find only read and write instructions from arduino reference...

Any syntax or sample codes?

Any syntax or sample codes?

Look at SD.h. The FILE_WRITE constant is defined as O_READ | O_WRITE | O_CREAT, which means to open the file for reading and writing, and to create the file if it does not exist.

You could use FILE_WRITE | O_TRUNC to open the file for reading and writing, to create the file if it does not exist, and to truncate the file if it does exist:

                logFile=SD.open("LOGGFILE.txt", FILE_WRITE | O_TRUNC);