Go Down

Topic: Data Logging- Correct Code? (Read 581 times) previous topic - next topic

command_z

Hello, I am starting to experiment with data logging with a Seeed Studio SD Card Shield.

I want to take a light sensor reading every 5 seconds and write the data to the SD card. In order to "close" the file after writing ( file.close(); ), I have created an if statement that allows me to close the file when I physically cover the sensor with my hand and bring the sensor reading down to zero.

I know this is not very efficient. Is there a better way, besides adding a switch into the circuit?

Thanks!


Code: [Select]
#include <SD.h>

File file;
int x;


void setup() {
  Serial.begin(9600);

  if (!SD.begin()) {
    Serial.println("begin failed");
    return;
  }
 
  file = SD.open("DATA.TXT", FILE_WRITE);

 
}


void loop() {
 
 
  delay(5000);

  x = analogRead(0);
 
 
  file.println(x);
  Serial.println(x);
 



if (x<8){

file.close();
Serial.println("okay to unplug");
delay(60000);

}



command_z

Okay, I've done some more reading and I think I need to add a data.flush() every so often. Not too often though. I read that it takes a lot of power/memory. So I an incrementing counter to the code that will flush() at the counter modulo of 10.

One other thing that I am concerned about is how to "break out" of the loop when I physically cover the sensor to bring the reading down. It seems like I may be damaging the arduino/SD card by unplugging the card on the 60 second delay.

Thanks so much. Any ideas?


Code: [Select]
/*
* Sketch to compare size of Arduino SD library with SdFat V2.
* See SdFatSize.pde for SdFat sketch.
*/
#include <SD.h>

File file;
int x;
int counter;

//------------------------------------------------------------------------------

void setup() {
  Serial.begin(9600);

  if (!SD.begin()) {
    Serial.println("begin failed");
    return;
  }
 
  file = SD.open("DATA.TXT", FILE_WRITE);

 
}

//------------------------------------------------------------------------------

void loop() {
 
  counter++;
 
 
  delay(1000);

  x = analogRead(0);
 
 
  file.print(x);
  file.println(",");

  Serial.print(x);
  Serial.println(",");

if (counter % 10 == 0){
  file.flush();
  Serial.println("&");
}

if (x<8){

file.close();
Serial.println("okay to unplug");
delay(60000);

}

}



PaulS

Why is x a global variable? What the heck does x mean? Why isn't it called lightLevel, or something meaningful?

How important is it that you get every reading? Opening the file, writing, and closing the file each time eliminates the need to worry about closing the file or flushing it (closing does a flush). The tradeoff is that as the file gets bigger, the open/position/write/close cycle takes longer.

Adding a switch to control whether you are logging, or not, seems a much better idea. When not logging, it is safe to remove the card.

command_z

Thank you Paul,

I have reworked the code to Open, Write and Close each time through the loop. I am glad to hear that the flush() is not necessary since the close() does a flush.

Quote
The tradeoff is that as the file gets bigger, the open/position/write/close cycle takes longer.


I'm planning on taking a reading every minute. If I left this datalogging code to run for a week, how much more time would the open/write/closing take? Would that just add a few seconds onto each minute, or would it really start to bog down?

One last question... If I unplug the Arduino USB (or other powersource) when it is on a 60 second delay, is that harmful to the board?

Thank you.


Code: [Select]
#include <SD.h>

File file;
int led = 13;
int lightReading;
int counter;

//------------------------------------------------------------------------------

void setup() {
  Serial.begin(9600);

  if (!SD.begin()) {
    Serial.println("begin failed");
    return;
  }
 
 

 
}

//------------------------------------------------------------------------------

void loop() {
 
  counter++;
 
 
  delay(60000);

  lightReading = analogRead(0);
 
 
  // 1. Open the file
  file = SD.open("DATA.TXT", FILE_WRITE);
 
  // 2. Write to the file and serial monitor
  file.print(lightReading);
  file.println(",");

  Serial.print(lightReading);
  Serial.println(",");
 
  // 3. Close the file
  file.close();
   
   
  // 4. Poor Man's Switch
if (lightReading<5){

Serial.println("okay to unplug");
delay(60000);

}

}

PaulS

Quote
If I unplug the Arduino USB (or other powersource) when it is on a 60 second delay, is that harmful to the board?

To the Arduino? No.

Quote
Would that just add a few seconds onto each minute, or would it really start to bog down?

A few microseconds, not seconds.

command_z


command_z

How long do you think a 9V battery would be able to run this program on the arduino/sd shield?


2 days?

5 days?


Thanks!

Go Up