I require the data to be logged with a starting time of 00:00:00 when power is applied to pin 3.
I have added some variables and code to record the first time when pin 3 goes HIGH. Then, the timeZeroMillis value is later subtracted from runMillis.
#include <SD.h>
File file;
boolean recordTimeZero = true;
unsigned long timeZeroMillis;
void setup()
{
// Sets analog pins to input
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
// Sets analog pins to input
pinMode(3, INPUT);
pinMode(2, OUTPUT);
// Opens the serial connection
Serial.begin(500000);
SD.begin(10);
file = SD.open("WR_TEST5.TXT", O_CREAT | O_WRITE);
}
void loop()
{
unsigned long startTime = 0;
unsigned long intreval = 2000UL; // 120 seconds
unsigned long previousInterval = 0;
unsigned long intreval1 = 5000UL; // 60 seconds
unsigned long previousInterval1 = 0;
delayMicroseconds(10);
while (digitalRead(3) == HIGH)
{
delayMicroseconds(100);
startTime = millis();
if (recordTimeZero)
{
recordTimeZero = false;
timeZeroMillis = startTime;
}
for (; millis() < startTime + 2000UL;)
{
char tuf[10];
digitalWrite(2, LOW);
sprintf(tuf, "low ");
//Serial.print(tuf);
file.print(tuf);
dataLog();
if (digitalRead(3) == LOW)
{
break;
}
}
for (; millis() < startTime + 7000UL;)
{
char tuf1[10];
digitalWrite(2, HIGH);
sprintf(tuf1, "high ");
//Serial.print(tuf1);
file.print(tuf1);
dataLog();
if (digitalRead(3) == LOW)
{
break;
}
} file.flush();
}
}
void dataLog()
{
char buf[100];
char buf1[50];
//Pressure transducers
int PT1A;
int PTT1;
int PT1B;
int PT1C;
unsigned long runMillis = millis() - timeZeroMillis;
//unsigned long allSeconds = (millis() / 1000;
unsigned long allSeconds = runMillis / 1000;
int runHours = allSeconds / 3600;
int secsRemaining = allSeconds % 3600;
int runMinutes = secsRemaining / 60;
int runSeconds = secsRemaining % 60;
// Sets values of incoming analog pin data to transducer nnumbers
PT1A = analogRead(A0); // Reads data from analog pin A0
PTT1 = analogRead(A1); // Reads data from analog pin A1
PT1B = analogRead(A2); // Reads data from analog pin A2
PT1C = analogRead(A3); // Reads data from analog pin A3
sprintf(buf, "Runtime%02d:%02d:%02d ", runHours, runMinutes,
runSeconds);
sprintf(buf1, "PT1A: %d; PTT1: %d; PT1B: %d; PT1C: %d", PT1A, PTT1, PT1B, PT1C);
/*Serial.print(buf);
Serial.println(buf1);*/
file.print(buf);
file.println(buf1);
}
EDIT: I'm not clear on what is controlling pin 3 and if there are indeed breaks and resumptions of the data recording. If you want to reset the time after a break, then set recordTimeZero back to true when pin 3 goes low and the break occurs.