I have tried everything and need help data logging to sd PLEASE HELP!

I really dont know much about coding or arduino but I have gotten info from 3 sensors to output nicely updating every so often 10 sec i believe to the serial monitor but i jhooked up an sd it tets fine and even creates the file but its empty when i open it? I need the data on there every 10 seconds here is my code what am i doing wrong?

//
//    FILE: dht11_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL:
//
// Released to the public domain
//
#include <SPI.h>
#include <SD.h>
#include <dht.h>
#include <Wire.h>
#include "IntersemaBaro.h"
Intersema::BaroPressure_MS5607B baro(true);
dht DHT;
#define DHT11_PIN 5
const int AOUTpin=3;//the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
const int DOUTpin=8;//the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino
const int ledPin=13;//the anode of the LED connects to digital pin D13 of the arduino

int limit;
int value;
File myFile;
// change this to match your SD shield or module;
//     Arduino Ethernet shield: pin 4
//     Adafruit SD shields and modules: pin 10
//     Sparkfun SD shield: pin 8
const int chipSelect = 53;
void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(SS, OUTPUT);
    if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("fltdata.txt", FILE_WRITE);
  // if the file opened okay, write to it:
  if (myFile)
  Serial.begin(115200);
  baro.init();
  Serial.println("Spaceball X ");
 myFile.println("Spaceball X ");
  Serial.print("Branden and Zachary de Haas: ");
 myFile.println("Branden and Zachary de Haas: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPin, OUTPUT);//sets the pin as an output of the arduino
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  myFile.println("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		myFile.println("OK,\t");
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  myFile.println(DHT.humidity, 1);
  Serial.print(",\t");
  myFile.println(",\t");
  Serial.println(DHT.temperature, 1);
  myFile.println(DHT.temperature, 1);
int alt = baro.getHeightCentiMeters();
Serial.print("Centimeters: ");
myFile.println("Centimeters: ");
Serial.print((float)(alt));
myFile.println((float)(alt));
Serial.print(", Feet: ");
myFile.println(", Feet: ");
Serial.println((float)(alt) / 30.48);
myFile.println((float)(alt) / 30.48);
value= analogRead(AOUTpin);//reads the analaog value from the alcohol sensor's AOUT pin
limit= digitalRead(DOUTpin);//reads the digital value from the alcohol sensor's DOUT pin
Serial.print("Alcohol value: ");
myFile.println("Alcohol value: ");
Serial.println(value);//prints the alcohol value
myFile.println(value);
Serial.print("Limit: ");
Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
delay(100);
if (limit == HIGH){
digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPin, LOW)
;delay(10000);
}}
// END OF FILE
//

if (myFile) aren’t you missing a pair of {} after that if statement?

so you do an SD.open() but there is no closing of that file…

(you should press ctrl-T to properly indent that code, it’s ugly to read…

else{
digitalWrite(ledPin, LOW)  // <== convention is to put the ; on the same line
;delay(10000); // not at the start of the next one... even if that works..

If you don't close the file then Windows will sometimes complain that the SD card is damaged and needs repair.

If you do close it every 10 seconds, then there's still a chance that it might be open at the moment that you yank out the card or turn off the power. However the risk is much smaller.

I just leave most files open, as I'm usually logging more often than 10 seconds. Then I just let the Windows repair do its job and there's no long-term problems.

if you don’t close you are not sure the buffers are flushed, you might actually loose data.

Thanks for all your replies but I am still lost what exactly do you recommend I remove nad or add in the code I am not great at this at all but my son wanted to build a near space balloon so I am trying my best

  if (myFile)
  Serial.begin(115200);

Why does initializing the serial port depend on successfully opening the file?

lol paul i dont know what I am doing thats why I have just been pieing things together. everything displays great in the serial monitor and I know the sd card runs fine as i ran test codes on it but when i run this then out it on the computer the the txt file there its just empty. I am doing this as a project with my son

I am doing this as a project with my son

Well, one of you needs to understand that ALL if statements should be followed by open and close curly braces, and that the stuff you want to do when the if statement evaluates to true belongs between the curly braces.

There are situations where the curly braces are optional. Use them anyway.

I am going crazy with this I have tried tutorial after tutorial

SD and MMC cards have a relatively low endurance. If you write too often to the same location you will eventually get write failures at that location. It is not as bad as it seams as the embedded controller in these cards perform "wear levelling" to distribute the load across the physical media.

When you write to a file what actually happens is you write to a 512 byte file buffer in RAM of your microprocessor. This buffer is mapped to a logical sector on the physical media. When you attempt to cross the 512 byte boundary, the target sector of the media is erased, the current 512 byte buffer is written to the media, the next sector is copied to the 512 byte buffer, the current buffer pointer points to the start of the buffer and the directory structure on the media is updated.

You can force the buffer to be flushed either by closing the file or by using the file.flush(). You do NOT want to issue a flush after every write to a file. If you were to do this after every byte is written, the same sector would be erased 512 times for each sector and the media would fail on average 512 times faster. Sometimes you do need to flush a file early - for example you have an application that logs a bunch of data very minute and you cannot afford to lose a lot of data in case of battery failure etc. In this case you might perform a flush every 15 minutes.

If you remove the media without closing or flushing the file then up to 512 bytes of data will be lost. Similarly,if you remove the media before the file system data structures have been written to the media then the file system can be corrupted (image you removed the card when the a sector of the file system directory structure has been erased while in the middle of updating the sector).

I am really appreciate for that last response but I have no idea what that means or if my code in itself is even right to accomplish the data showing on the card it is showing the file i create but its an empty .txt file

I am going crazy with this I have tried tutorial after tutorial

You should be trying to understand what people are saying and either ask for clarification or make changes to your code.

J-M-L recommended 3 things.

if (myFile)

aren't you missing a pair of {} after that if statement?

The brackets should enclose everything you want to happen if the if statement is true.

so you do an SD.open() but there is no closing of that file....

I would add myFile.close() to the end of set up to write all the setup information to the card. Then reopen the file at the beginning of the loop and close it near the end of loop after you have the last myFile.print() to write the data collected in the 10 second period.

(you should press ctrl-T to properly indent that code, it's ugly to read..

.

Have you done any of them? If so, post the code as it now stands.

I have and still does not work this is as it stands

//
//
//    FILE: dht11_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL:
//
// Released to the public domain
//
#include <SPI.h>
#include <SD.h>
#include <dht.h>
#include <Wire.h>
#include "IntersemaBaro.h"
Intersema::BaroPressure_MS5607B baro(true);
dht DHT;
#define DHT11_PIN 5
const int AOUTpin = 3; //the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
const int DOUTpin = 8; //the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino
const int ledPin = 13; //the anode of the LED connects to digital pin D13 of the arduino

int limit;
int value;
{
File myFile;
}
}
void setup()
{
  Serial.begin(115200);
  baro.init();
  Serial.println("Spaceball X ");
  Serial.print("Branden and Zachary de Haas: ");
  Serial.print("Initializing SD card...");

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("flt.txt", FILE_WRITE);
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
  pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
  pinMode(ledPin, OUTPUT);//sets the pin as an output of the arduino
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  myFile.println("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
      Serial.print("OK,\t");
      myFile.println("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.print("Checksum error,\t");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.print("Time out error,\t");
      break;
    default:
      Serial.print("Unknown error,\t");
      break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  myFile.println(DHT.humidity, 1);
  Serial.print(",\t");
  myFile.println(",\t");
  Serial.println(DHT.temperature, 1);
  myFile.println(DHT.temperature, 1);
  int alt = baro.getHeightCentiMeters();
  Serial.print("Centimeters: ");
  Serial.print((float)(alt));
  Serial.print(", Feet: ");
  Serial.println((float)(alt) / 30.48);
  myFile.println((float)(alt) / 30.48);
  value = analogRead(AOUTpin); //reads the analaog value from the alcohol sensor's AOUT pin
  limit = digitalRead(DOUTpin); //reads the digital value from the alcohol sensor's DOUT pin
  Serial.print("Alcohol value: ");
  Serial.println(value);//prints the alcohol value
  myFile.println(value);
  Serial.print("Limit: ");
  Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
  delay(100);
  if (limit == HIGH) {
    digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator
  }
  else {
    digitalWrite(ledPin, LOW);
    delay(10000);
    myFile.close();
  }
}

//
// END OF FILE
//
//
  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }

This will return from setup when the SD card fails but the loop() function will still be called, without all the pins initialised properly. Is that what you intended?

Copy the style of the example. Define all your pin numbers at the top and use those constants in the main code. Make a constant for the SD card-select pin. That way all the useful information is gathered together at the top.

{
File myFile;
}
}

The three braces are not needed.

 if (limit == HIGH) {
    digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator
  }
  else {
    digitalWrite(ledPin, LOW);
    delay(10000);
    myFile.close();
  }

I would not put myFile.close() in the else block. If the limit == HIGH, the SD card will not get written. Place it after the last myFile.print() statement in the main body of the loop,

If you are going to close the file in loop, you will need to open it at the beginning of loop. You have removed all SD data writing in setup, so just move the opening statement to the top of loop and remove if from setup.

did all that and it now runs perfect when connected to serial but when i try to run on battery which is how i have been running all this early the sketch will no longer run just placed 6 new AA's and still wont run unless connected to usb but it did before i dont get it?

the sketch will no longer run

So, you changed the code, and failed to post it. It does something, but you did not describe what it actually does. You expect it to do something, which, obviously, it does not do, but you failed to define your expectations.

And, yet, you still want us to tell you what to do to make the sketch work. Does that really seem reasonable?

Hi Paul. I understand where you are coming from. This isnt a pity post but I was recently diagnosed with cancer and I am forgetful. I am building a near space balloon with my son to get his mind off it I really had no experience with this till then. Here is my current code which no works and even saves to SD in timed increments only problem is it will only run correctly when connected via USB if run by 6 AA batterys sketch fails to run. here is the code thank you.

//
//    FILE: dht11_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL:
//
// Released to the public domain
//
#include <SPI.h>
#include <SD.h>
#include <dht.h>
#include <Wire.h>
#include "IntersemaBaro.h"
Intersema::BaroPressure_MS5607B baro(true);
dht DHT;
#define DHT11_PIN 5
const int AOUTpin = 3; //the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
const int DOUTpin = 8; //the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino
const int ledPin = 13; //the anode of the LED connects to digital pin D13 of the arduino
int limit;
int value;
File myFile; 
void setup()
{
  Serial.begin(115200);
  baro.init();
  Serial.print("Initializing SD card...");

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("flt.txt", FILE_WRITE);
  Serial.println("Spaceball X ");
  Serial.print("Branden and Zachary de Haas: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
  myFile.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
  pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
  pinMode(ledPin, OUTPUT);//sets the pin as an output of the arduino
  myFile.close();
}

void loop()
{
  // READ DATA
  myFile = SD.open("flt.txt", FILE_WRITE);
  Serial.print("DHT11, \t");
  myFile.println("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
      Serial.print("OK,\t");
      myFile.println("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.print("Checksum error,\t");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.print("Time out error,\t");
      break;
    default:
      Serial.print("Unknown error,\t");
      break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  myFile.println(DHT.humidity, 1);
  Serial.print(",\t");
  myFile.println(",\t");
  Serial.println(DHT.temperature, 1);
  myFile.println(DHT.temperature, 1);
  int alt = baro.getHeightCentiMeters();
  Serial.print("Centimeters: ");
  myFile.println("Centimeters: ");
  Serial.print((float)(alt));
  Serial.print(", Feet: ");
  myFile.println(", Feet: ");
  Serial.println((float)(alt) / 30.48);
  myFile.println((float)(alt) / 30.48);
  value = analogRead(AOUTpin); //reads the analaog value from the alcohol sensor's AOUT pin
  limit = digitalRead(DOUTpin); //reads the digital value from the alcohol sensor's DOUT pin
  Serial.print("Alcohol value: ");
  myFile.println("Alcohol value: ");
  Serial.println(value);//prints the alcohol value
  myFile.println(value);
  myFile.close();
  Serial.print("Limit: ");
  Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
  delay(100);
  if (limit == HIGH) {
    digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator
  }
  else {
    digitalWrite(ledPin, LOW);
    delay(10000);
  }
}

//
// END OF FILE
//

Here is my current code which no works and even saves to SD in timed increment

I assume you mean "now works".

if run by 6 AA batterys sketch fails to run.

What does "fails to run" mean?

No writing to SD card? No values from the sensors?

How are the batteries arranged in the battery pack and wired to the Arduino? Are you using the barrel jack or Vin?

If you have your 6*1.5v batteries in series and have 9v at the barrel jack (or Vin), you should be able to connect the usb and the batteries at the same time, and have the Arduino running from the external supply. You should be able to see Serial output, but not be running off the 5v from the USB. I think that for debug, you could also hack a USB cable to remove the power, and just use the coms.

What do you see for Serial output when the program "fails to run"?

Writing to the SD card can take alot of power, but the batteries should be able to supply as much or more than the usb.

Yes sketch runs great when connected to USB runs and saves all data to SD that I can read from the card. If i power with 6 AA using the barrell jack the power lights come on but the yellow flash that normally flashes each time it runs the code in its increments does not flash and the only thing on the Sd card in that case is and empty file but with the proper file name just nothing in it