Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

96 lines
2.1KB

  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, pin 7 on Teensy with audio board
  7. ** MISO - pin 12
  8. ** CLK - pin 13, pin 14 on Teensy with audio board
  9. ** CS - pin 4, pin 10 on Teensy with audio board
  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 <SD.h>
  17. #include <SPI.h>
  18. File myFile;
  19. // change this to match your SD shield or module;
  20. // Arduino Ethernet shield: pin 4
  21. // Adafruit SD shields and modules: pin 10
  22. // Sparkfun SD shield: pin 8
  23. // Teensy audio board: pin 10
  24. // Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
  25. // Wiz820+SD board: pin 4
  26. // Teensy 2.0: pin 0
  27. // Teensy++ 2.0: pin 20
  28. const int chipSelect = 4;
  29. void setup()
  30. {
  31. //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  32. //SPI.setMOSI(7); // Audio shield has MOSI on pin 7
  33. //SPI.setSCK(14); // Audio shield has SCK on pin 14
  34. // Open serial communications and wait for port to open:
  35. Serial.begin(9600);
  36. while (!Serial) {
  37. ; // wait for serial port to connect.
  38. }
  39. Serial.print("Initializing SD card...");
  40. if (!SD.begin(chipSelect)) {
  41. Serial.println("initialization failed!");
  42. return;
  43. }
  44. Serial.println("initialization done.");
  45. if (SD.exists("example.txt")) {
  46. Serial.println("example.txt exists.");
  47. }
  48. else {
  49. Serial.println("example.txt doesn't exist.");
  50. }
  51. // open a new file and immediately close it:
  52. Serial.println("Creating example.txt...");
  53. myFile = SD.open("example.txt", FILE_WRITE);
  54. myFile.close();
  55. // Check to see if the file exists:
  56. if (SD.exists("example.txt")) {
  57. Serial.println("example.txt exists.");
  58. }
  59. else {
  60. Serial.println("example.txt doesn't exist.");
  61. }
  62. // delete the file:
  63. Serial.println("Removing example.txt...");
  64. SD.remove("example.txt");
  65. if (SD.exists("example.txt")){
  66. Serial.println("example.txt exists.");
  67. }
  68. else {
  69. Serial.println("example.txt doesn't exist.");
  70. }
  71. }
  72. void loop()
  73. {
  74. // nothing happens after setup finishes.
  75. }