選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

93 行
2.0KB

  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. // change this to match your SD shield or module;
  19. // Arduino Ethernet shield: pin 4
  20. // Adafruit SD shields and modules: pin 10
  21. // Sparkfun SD shield: pin 8
  22. // Teensy 2.0: pin 0
  23. // Teensy++ 2.0: pin 20
  24. const int chipSelect = 4;
  25. void setup()
  26. {
  27. // Open serial communications and wait for port to open:
  28. Serial.begin(9600);
  29. while (!Serial) {
  30. ; // wait for serial port to connect. Needed for Leonardo only
  31. }
  32. Serial.print("Initializing SD card...");
  33. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  34. // Note that even if it's not used as the CS pin, the hardware SS pin
  35. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  36. // or the SD library functions will not work.
  37. pinMode(10, OUTPUT);
  38. if (!SD.begin(chipSelect)) {
  39. Serial.println("initialization failed!");
  40. return;
  41. }
  42. Serial.println("initialization done.");
  43. if (SD.exists("example.txt")) {
  44. Serial.println("example.txt exists.");
  45. }
  46. else {
  47. Serial.println("example.txt doesn't exist.");
  48. }
  49. // open a new file and immediately close it:
  50. Serial.println("Creating example.txt...");
  51. myFile = SD.open("example.txt", FILE_WRITE);
  52. myFile.close();
  53. // Check to see if the file exists:
  54. if (SD.exists("example.txt")) {
  55. Serial.println("example.txt exists.");
  56. }
  57. else {
  58. Serial.println("example.txt doesn't exist.");
  59. }
  60. // delete the file:
  61. Serial.println("Removing example.txt...");
  62. SD.remove("example.txt");
  63. if (SD.exists("example.txt")){
  64. Serial.println("example.txt exists.");
  65. }
  66. else {
  67. Serial.println("example.txt doesn't exist.");
  68. }
  69. }
  70. void loop()
  71. {
  72. // nothing happens after setup finishes.
  73. }