awinograd

Lab 5 - SD Card

May 3rd, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. /*
  2.  
  3.   SD card file dump
  4.  
  5.  
  6.  
  7.  This example shows how to read a file from the SD card using the
  8.  
  9.  SD library and send it over the serial port.
  10.  
  11.  
  12.  
  13.  created  22 December 2010
  14.  
  15.  
  16.  
  17.  This example code is in the public domain.
  18.  
  19.  
  20.  
  21.  */
  22.  
  23.  
  24.  
  25. #include <SD.h>
  26.  
  27.  
  28.  
  29. // On the Ethernet Shield, CS is pin 4. Note that even if it's not
  30.  
  31. // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
  32.  
  33. // 53 on the Mega) must be left as an output or the SD library
  34.  
  35. // functions will not work.
  36.  
  37. const int chipSelect = 12;
  38.  
  39.  
  40.  
  41. void setup()
  42.  
  43. {
  44.  
  45.   Serial.begin(9600);
  46.  
  47.   Serial.print("Initializing SD card...");
  48.  
  49.  
  50.  
  51.   // see if the card is present and can be initialized:
  52.  
  53.   if (!SD.begin(chipSelect)) {
  54.  
  55.     Serial.println("Card failed, or not present");
  56.  
  57.     // don't do anything more:
  58.  
  59.     return;
  60.  
  61.   }
  62.  
  63.   Serial.println("card initialized.");
  64.  
  65.  
  66.  
  67.   // open the file. note that only one file can be open at a time,
  68.  
  69.   // so you have to close this one before opening another.
  70.  
  71.   File dataFile = SD.open("datalog.txt", FILE_WRITE);
  72.   dataFile.seek(0);
  73.   dataFile.println("Tom Riddle");
  74.   dataFile.close();
  75.   dataFile = SD.open("datalog.txt");
  76.   // if the file is available, write to the serial port:
  77.  
  78.   if (dataFile) {
  79.  
  80.     while (dataFile.available()) {
  81.  
  82.       Serial.write(dataFile.read());
  83.  
  84.     }
  85.  
  86.     dataFile.close();
  87.  
  88.   }  
  89.  
  90.   // if the file isn't open, pop up an error:
  91.  
  92.   else {
  93.  
  94.     Serial.println("error opening datalog.txt");
  95.  
  96.   }
  97.  
  98. }
  99.  
  100.  
  101.  
  102. void loop()
  103.  
  104. {
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment