Program will create a file on SD but will not write to it

As the title says, my code will create a file, when i push a button it will execute an if statement to close the file and print done. However when i open the file on the laptop it is empty. Setup is as follows;

genuine arduino uno R3
genuine arduino ethernet shield with SD
sandisk 16Gb class 10 microSD
ptm button;
arduino - breadboard
5V to button
button to ground
10K resistor connected between between button and ground

so connected like in the link below;

below is my code;

#include <SPI.h>
#include <SD.h>

File myFile;

const int buttonPin = 3;     // the number of the pushbutton pin

int buttonState = 0;
void setup() {
  // put your setup code here, to run once:

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  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.

  pinMode(buttonPin, INPUT);

  myFile ="BUTTON.txt", FILE_WRITE);


void loop() {

  buttonState = digitalRead(buttonPin);
  if (myFile) {
  // put your main code here, to run repeatedly:
 if ( buttonState == HIGH) {
        // close the file:

i have had this card working before but now it wont work. I have tried a a separate module, a kingston 32Gb class 4 SD, powering the module with 3.3V and 5V.

When i run it, it gives me the time in milliseconds, but when i press the button it stops the code, writes “done”, but will not have saved it, though it obviously has gone through the if statement to write “done”. I know it will be something simple but i just cant see it.


I don't see where you actually write anything to the file.


Should that be?


well that would be it...thankyou. Been up all night trying to sort everything out for an experiment today. More sleep is needed!

Sometimes a fresher pair of eyes...