empty data file

Hİ guys firstly it is my code

#define Grove_Water_Sensor 8 // Attach Water sensor to
Arduino Digital Pin 8
#define LED 9 // Attach an LED to Digital Pin 9 (or use
onboard LED)
#include <SPI.h>
#include <SD.h>
const int GSR=A1;
int sensorValue=0;
int gsr_average=0;
const int chipSelect = 10;
File dataFile;

void setup() {
 /*
Serial.begin(9600);
  while (!Serial) {
    ; 
    }*/
  
pinMode(Grove_Water_Sensor, INPUT); // The Water Sensor is an Input
pinMode(LED,OUTPUT); // The LED is an Output
Serial.begin(9600);// open serial port, set the baud rate to 9600 bps
  while (!Serial) {
    ; 
 }
Serial.print("Initializing SD card...");
  // make sure that
the default chip select pin is set to
  // output, even if
you don't use it:
  pinMode(10, OUTPUT);

  // see if the card
is present and can be initialized:
  if
(!SD.begin(chipSelect)) {  Serial.println("Card failed, or not present");
    // don't do
anything more:
    while (1) ;
  }

Serial.println("card initialized.");


  // Open up the file
we're going to log to!
  dataFile =SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
  
Serial.println("error opening datalog.txt");
    // Wait forever
since we cant write data
    while (1) ;
  }
  }
void loop() {

  String dataString ="";
  long sum=0;
  for(int i=0;i<10;i++) //Average the 10 measurements to remove the glitch
    {
     sensorValue=analogRead(GSR);
     sum += sensorValue;
      delay(20);
    }
gsr_average =
sum/10;
Serial.print("GSR ");  
Serial.print(gsr_average);
dataString +="GSR " + String(gsr_average);


int sensorValue;
sensorValue =
analogRead(0);  
Serial.print(","); 
Serial.print("STEAM "); 
Serial.print(sensorValue); //print the value to serial
dataString +=", STEAM " + String(sensorValue);
delay(20);
   if(digitalRead(Grove_Water_Sensor) == LOW) {digitalWrite(LED,LOW);
      Serial.print(",");     
      Serial.print("WATER ");    
      Serial.println(LOW);
      dataString +=", WATER " + String("LOW");
    }else {
      digitalWrite(LED,HIGH);
      Serial.print(",");
      Serial.print("WATER ");
      Serial.println(HIGH);
      dataString +=", WATER " + String("HIGH");
  }
   /*
StringgroveWaterSensorValue = (digitalRead(Grove_Water_Sensor) ==LOW?"LOW":"HIGH");
digitalWrite(LED,groveWaterSensorValue);
Serial.print(",");
Serial.print("WATER ");
Serial.println(groveWaterSensorValue);dataString +=", WATER " +String(groveWaterSensorValue);
*/
dataString +="\n";
dataFile.println(dataString);
dataFile.flush();
delay(20);
}

I can initliaze my sd card and i can open a file for writing data but file is empty but sometimes it is working can you help me pleas

Hello
did you CLOSE the file ?

paulpaulson:
Hello
did you CLOSE the file ?

Yes of course :frowning:

i can create a file but i think sd card does not show the contain of the file. Please help me

Sorry, your code is just too difficult to read. Please reformat it and post it again?

umimako:
Yes of course :frowning:

Are you shure, aren´t you?

paulpaulson:
Are you shure, aren´t you?

I have paste my code and contain close func. Yea i am sure

umimako:
I have paste my code and contain close func. Yea i am sure

Paul was being very polite - there is no reference to datafile.close in your code which means the data could be still in a buffer waiting to be written to disk before you turn it off. It will work sometimes as the buffer has been written, but fail other times and could often be truncated.
You could call the close function and reopen the file every few minutes/second/whatever is most suitable.

countrypaul:
Paul was being very polite - there is no reference to datafile.close in your code which means the data could be still in a buffer waiting to be written to disk before you turn it off. It will work sometimes as the buffer has been written, but fail other times and could often be truncated.
You could call the close function and reopen the file every few minutes/second/whatever is most suitable.

Hello countrypaul
I like to leave each user his own learning success :slight_smile:

I thought flush was doing this close thing. I will try to use close func

Guys i cannot use close funct with this code. Please help me because i thought flush was closing

I tried datalogger example and this hapenned again. Empty filed is created

Why can't you use close with your code?

countrypaul:
Why can't you use close with your code?

i have added dataFile.Close() at the and of code it is not working

What makes you sure it is not working? Have you an adjacent call to println so you know the code path calls it?

countrypaul:
What makes you sure it is not working? Have you an adjacent call to println so you know the code path calls it?

because it is creating empty file. I have tried basic example and i faced with same problem.

Hello
Did you swich on the write protection for the SD-Card?

paulpaulson:
Hello
Did you swich on the write protection for the SD-Card?

ıt is on position

Can you create and write to a file on the SD card using a PC or other device?
Can you also try a different SD card?

umimako:
ıt is on position

write protection == ON ?