Go Down

Topic: SD card data acquisition (Read 288 times) previous topic - next topic

R1c4rd

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

PaulS

Code: [Select]
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

Setting the mode of the digital pins that share space with the analog pins is useless, when you are using the pins as analog pins.

Code: [Select]
    for (;millis() < startTime + 5000UL;)
Why are you abusing a for statement this way? Use a while statement!

Use subtraction involving time, NOT addition.

Code: [Select]
     sprintf(tuf1,"high ");
It's silly to us a sledgehammer to push in thumbtacks. Use strcpy().

It's especially silly to do that, when the print() function can take a string literal, which is all that you are copying to the array.

Code: [Select]
     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

Are you sure about that? Useless comments should be deleted.

Reading from two different analog pins like that, in succession, often results in incorrect readings from the second pin. Better to read from a pin, discard the value, and read again, when switching pins.

The SD card reader/writer is an SPI device. Do you know what pins SPI devices use, on your Arduino? Do you know that you can't use those pins for other purposes at the same time?
The art of getting good answers lies in asking good questions.

Go Up