Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

86 lines
2.0KB

  1. /*
  2. SD card read/write
  3. This example shows how to read and write data to and from 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. // open the file. note that only one file can be open at a time,
  37. // so you have to close this one before opening another.
  38. myFile = SD.open("test.txt", FILE_WRITE);
  39. // if the file opened okay, write to it:
  40. if (myFile) {
  41. Serial.print("Writing to test.txt...");
  42. myFile.println("testing 1, 2, 3.");
  43. // close the file:
  44. myFile.close();
  45. Serial.println("done.");
  46. } else {
  47. // if the file didn't open, print an error:
  48. Serial.println("error opening test.txt");
  49. }
  50. // re-open the file for reading:
  51. myFile = SD.open("test.txt");
  52. if (myFile) {
  53. Serial.println("test.txt:");
  54. // read from the file until there's nothing else in it:
  55. while (myFile.available()) {
  56. Serial.write(myFile.read());
  57. }
  58. // close the file:
  59. myFile.close();
  60. } else {
  61. // if the file didn't open, print an error:
  62. Serial.println("error opening test.txt");
  63. }
  64. }
  65. void loop()
  66. {
  67. // nothing happens after setup
  68. }