Hello all,
I am trying to write data from 4 pressure transducers to a microSD card using a SD shield. My code worked perfectly fine switching between high and log on pin 12 until I added commands to write to the SD card. Now the program switches between both high and low loops for pin 12 without having meet the condition in my if statements.
#include <SD.h>
File file;
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(13, INPUT);
pinMode(12, OUTPUT);
// Opens the serail connection
Serial.begin(250000);
SD.begin(10);
}
void loop()
{
unsigned long startTime;
unsigned long intreval = 2000UL; // 120 seconds
unsigned long previousInterval = 0;
unsigned long intreval1 = 5000UL; // 60 seconds
unsigned long previousInterval1 = 0;
while (digitalRead(13) == HIGH);
file = SD.open("WR_TEST4.TXT", O_CREAT | O_WRITE);
{
startTime = millis();
for (;millis() < startTime + 2000UL;)
{
char tuf[10];
digitalWrite(12, LOW);
sprintf(tuf,"low ");
file.print(tuf);
dataLog();
if (digitalRead(13) == LOW)
{
file.flush();
break;
}
}
for (;millis() < startTime + 5000UL;)
{
char tuf1[10];
digitalWrite(12, HIGH);
sprintf(tuf1,"high ");
file.print(tuf1);
dataLog();
file.flush();
if (digitalRead(13) == LOW)
{
file.flush();
break;
}
}
}
}
void dataLog()
{
char buf[100];
char buf1[50];
//Pressure transducers
int PT1A;
int PTT1;
int PT1B;
int PT1C;
unsigned long runMillis= millis();
unsigned long allSeconds=millis()/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);
file.print(buf);
file.println(buf1);
}