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();
}
}
}