You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.8KB

  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. #include <SPI.h>
  19. // On the Ethernet Shield, CS is pin 4. Note that even if it's not
  20. // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
  21. // 53 on the Mega) must be left as an output or the SD library
  22. // functions will not work.
  23. // change this to match your SD shield or module;
  24. // Arduino Ethernet shield: pin 4
  25. // Adafruit SD shields and modules: pin 10
  26. // Sparkfun SD shield: pin 8
  27. // Teensy audio board: pin 10
  28. // Wiz820+SD board: pin 4
  29. // Teensy 2.0: pin 0
  30. // Teensy++ 2.0: pin 20
  31. const int chipSelect = 4;
  32. void setup()
  33. {
  34. // Open serial communications and wait for port to open:
  35. Serial.begin(9600);
  36. while (!Serial) {
  37. ; // wait for serial port to connect. Needed for Leonardo only
  38. }
  39. Serial.print("Initializing SD card...");
  40. // see if the card is present and can be initialized:
  41. if (!SD.begin(chipSelect)) {
  42. Serial.println("Card failed, or not present");
  43. // don't do anything more:
  44. return;
  45. }
  46. Serial.println("card initialized.");
  47. // open the file. note that only one file can be open at a time,
  48. // so you have to close this one before opening another.
  49. File dataFile = SD.open("datalog.txt");
  50. // if the file is available, write to it:
  51. if (dataFile) {
  52. while (dataFile.available()) {
  53. Serial.write(dataFile.read());
  54. }
  55. dataFile.close();
  56. }
  57. // if the file isn't open, pop up an error:
  58. else {
  59. Serial.println("error opening datalog.txt");
  60. }
  61. }
  62. void loop()
  63. {
  64. }