Files.ino 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. SD card basic file example
  3. This example shows how to create and destroy an SD card file
  4. The circuit:
  5. SD card attached to SPI bus as follows:
  6. ** MOSI - pin 11
  7. ** MISO - pin 12
  8. ** CLK - pin 13
  9. ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
  10. created Nov 2010
  11. by David A. Mellis
  12. modified 9 Apr 2012
  13. by Tom Igoe
  14. This example code is in the public domain.
  15. */
  16. #include <SPI.h>
  17. #include <SD.h>
  18. File myFile;
  19. void setup() {
  20. // Open serial communications and wait for port to open:
  21. Serial.begin(9600);
  22. while (!Serial) {
  23. ; // wait for serial port to connect. Needed for native USB port only
  24. }
  25. Serial.print("Initializing SD card...");
  26. if (!SD.begin(4)) {
  27. Serial.println("initialization failed!");
  28. while (1);
  29. }
  30. Serial.println("initialization done.");
  31. if (SD.exists("example.txt")) {
  32. Serial.println("example.txt exists.");
  33. } else {
  34. Serial.println("example.txt doesn't exist.");
  35. }
  36. // open a new file and immediately close it:
  37. Serial.println("Creating example.txt...");
  38. myFile = SD.open("example.txt", FILE_WRITE);
  39. myFile.close();
  40. // Check to see if the file exists:
  41. if (SD.exists("example.txt")) {
  42. Serial.println("example.txt exists.");
  43. } else {
  44. Serial.println("example.txt doesn't exist.");
  45. }
  46. // delete the file:
  47. Serial.println("Removing example.txt...");
  48. SD.remove("example.txt");
  49. if (SD.exists("example.txt")) {
  50. Serial.println("example.txt exists.");
  51. } else {
  52. Serial.println("example.txt doesn't exist.");
  53. }
  54. }
  55. void loop() {
  56. // nothing happens after setup finishes.
  57. }