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

85 行
1.7KB

  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 root;
  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(10)) {
  32. Serial.println("initialization failed!");
  33. return;
  34. }
  35. Serial.println("initialization done.");
  36. root = SD.open("/");
  37. printDirectory(root, 0);
  38. Serial.println("done!");
  39. }
  40. void loop()
  41. {
  42. // nothing happens after setup finishes.
  43. }
  44. void printDirectory(File dir, int numTabs) {
  45. while(true) {
  46. File entry = dir.openNextFile();
  47. if (! entry) {
  48. // no more files
  49. //Serial.println("**nomorefiles**");
  50. break;
  51. }
  52. for (uint8_t i=0; i<numTabs; i++) {
  53. Serial.print('\t');
  54. }
  55. Serial.print(entry.name());
  56. if (entry.isDirectory()) {
  57. Serial.println("/");
  58. printDirectory(entry, numTabs+1);
  59. } else {
  60. // files have sizes, directories do not
  61. Serial.print("\t\t");
  62. Serial.println(entry.size(), DEC);
  63. }
  64. entry.close();
  65. }
  66. }