您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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
  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. File myFile;
  18. void setup()
  19. {
  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 Leonardo only
  24. }
  25. Serial.print("Initializing SD card...");
  26. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  27. // Note that even if it's not used as the CS pin, the hardware SS pin
  28. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  29. // or the SD library functions will not work.
  30. pinMode(10, OUTPUT);
  31. if (!SD.begin(4)) {
  32. Serial.println("initialization failed!");
  33. return;
  34. }
  35. Serial.println("initialization done.");
  36. if (SD.exists("example.txt")) {
  37. Serial.println("example.txt exists.");
  38. }
  39. else {
  40. Serial.println("example.txt doesn't exist.");
  41. }
  42. // open a new file and immediately close it:
  43. Serial.println("Creating example.txt...");
  44. myFile = SD.open("example.txt", FILE_WRITE);
  45. myFile.close();
  46. // Check to see if the file exists:
  47. if (SD.exists("example.txt")) {
  48. Serial.println("example.txt exists.");
  49. }
  50. else {
  51. Serial.println("example.txt doesn't exist.");
  52. }
  53. // delete the file:
  54. Serial.println("Removing example.txt...");
  55. SD.remove("example.txt");
  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. }
  63. void loop()
  64. {
  65. // nothing happens after setup finishes.
  66. }