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.

74 lines
1.7KB

  1. /*
  2. SD card file dump
  3. This example shows how to read a file from the SD card using the
  4. SD library and send it over the serial port.
  5. The circuit:
  6. * SD card attached to SPI bus as follows:
  7. ** MOSI - pin 11
  8. ** MISO - pin 12
  9. ** CLK - pin 13
  10. ** CS - pin 4
  11. created 22 December 2010
  12. by Limor Fried
  13. modified 9 Apr 2012
  14. by Tom Igoe
  15. This example code is in the public domain.
  16. */
  17. #include <SD.h>
  18. // On the Ethernet Shield, CS is pin 4. Note that even if it's not
  19. // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
  20. // 53 on the Mega) must be left as an output or the SD library
  21. // functions will not work.
  22. const int chipSelect = 4;
  23. void setup()
  24. {
  25. // Open serial communications and wait for port to open:
  26. Serial.begin(9600);
  27. while (!Serial) {
  28. ; // wait for serial port to connect. Needed for Leonardo only
  29. }
  30. Serial.print("Initializing SD card...");
  31. // make sure that the default chip select pin is set to
  32. // output, even if you don't use it:
  33. pinMode(10, OUTPUT);
  34. // see if the card is present and can be initialized:
  35. if (!SD.begin(chipSelect)) {
  36. Serial.println("Card failed, or not present");
  37. // don't do anything more:
  38. return;
  39. }
  40. Serial.println("card initialized.");
  41. // open the file. note that only one file can be open at a time,
  42. // so you have to close this one before opening another.
  43. File dataFile = SD.open("datalog.txt");
  44. // if the file is available, write to it:
  45. if (dataFile) {
  46. while (dataFile.available()) {
  47. Serial.write(dataFile.read());
  48. }
  49. dataFile.close();
  50. }
  51. // if the file isn't open, pop up an error:
  52. else {
  53. Serial.println("error opening datalog.txt");
  54. }
  55. }
  56. void loop()
  57. {
  58. }