     myFile.close();
   } else {
   	// if the file didn't open, print an error:
-    Serial.println("error opening test.txt");
+    Serial.println("error opening mtest.txt");
+  }
+}
+
+int counter = 0;
+
+// Append int value to file
+void logToFile(char* filename, int value)
+{
+  // 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(filename, FILE_WRITE);
+  
+  // if the file opened okay, write to it:
+  if (myFile) {
+    // Serial info
+    Serial.print("Writing ");
+    Serial.print(value);
+    Serial.print(" to ");
+    Serial.print(filename);
+    Serial.print("... ");
+    
+    // Write value to file and close
+    myFile.println(value);
+    myFile.close();
+    
+    // Update counter
+    counter++;
+    
+    Serial.println("done.");
+  } else {
+    // if the file didn't open, print an error:
+    Serial.println("error opening for writing.");
   }
 }
 

/*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */
 
#include <SD.h>

File myFile;
void readFileToSerial()
{
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {

    Serial.println("Woo");
    if (startSD())
    {
      logToFile("mtest.txt", counter);
      readFileToSerial();
    }
    buttonPressed = false;
