SD write after I put SD in

Hey guys, this is my first time using the SD card. I have the example code in Arduino IDE itself from the SD library. I tried that so I can see what the options are. But it's not going the way I want it to.
I want to be able to put the sd card in whenever I want. At the moment I have to put the SD card in before I turn the Arduino on.
this is what I have to try out the SD card module. (the module I have is this)
code:

/*
  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 (for MKRZero SD: SDCARD_SS_PIN)

  created   Nov 2010
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

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

File myFile;
int butt = 5;
int buttstate;
int timers;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(butt, INPUT);
  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!");
    while (1);
  }
  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("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    Serial.print("Writing to test.txt...");
}

void loop() {
 buttstate = digitalRead(butt);
 timers= millis(); 
  // nothing happens after setup
    if (myFile) {

    myFile.println(timers);
    // close the file:
    
  }
  if (buttstate == HIGH)
  {
    
    myFile.close();
  }
  
}


SD/SD.cpp at master · arduino-libraries/SD · GitHub

//call this when a card is removed. It will allow you to insert and initialise a new card.
void SDClass::end() {
root.close();
}

Looks like with a bit of code your want is possible.

Does this use another library or is this the same the Arduino already has?
Where can I find the content of "void SDClass"?

Did you do an internet search using the words "arduino sd library", which should allow You to look at the library's code?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.