BackwardCompatibility.ino 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // A simple read/write example for SD.h.
  2. // Mostly from the SD.h ReadWrite example.
  3. //
  4. // Your SD must be formatted FAT16/FAT32.
  5. //
  6. // SD.h does not support some default SdFat features.
  7. // To compare flash size, set USE_FAT_FILE_FLAG_CONTIGUOUS,
  8. // ENABLE_DEDICATED_SPI, and USE_LONG_FILE_NAMES to zero also
  9. // set SDFAT_FILE_TYPE to one in SdFat/src/SdFatCongfig.h
  10. //
  11. // Set USE_SD_H nonzero to use SD.h.
  12. // Set USE_SD_H zero to use SdFat.h.
  13. //
  14. #define USE_SD_H 0
  15. //
  16. #if USE_SD_H
  17. #include <SD.h>
  18. #else // USE_SD_H
  19. #include "SdFat.h"
  20. SdFat SD;
  21. #endif // USE_SD_H
  22. // Modify SD_CS_PIN for your board.
  23. // For Teensy 3.6 and SdFat.h use BUILTIN_SDCARD.
  24. #define SD_CS_PIN SS
  25. File myFile;
  26. void setup() {
  27. Serial.begin(9600);
  28. while (!Serial) {}
  29. #if USE_SD_H
  30. Serial.println(F("Using SD.h. Set USE_SD_H zero to use SdFat.h."));
  31. #else // USE_SD_H
  32. Serial.println(F("Using SdFat.h. Set USE_SD_H nonzero to use SD.h."));
  33. #endif // USE_SD_H
  34. Serial.println(F("\nType any character to begin."));
  35. while (!Serial.available()) {
  36. yield();
  37. }
  38. Serial.print("Initializing SD card...");
  39. if (!SD.begin(SD_CS_PIN)) {
  40. Serial.println("initialization failed!");
  41. return;
  42. }
  43. Serial.println("initialization done.");
  44. // open the file.
  45. myFile = SD.open("test.txt", FILE_WRITE);
  46. // if the file opened okay, write to it:
  47. if (myFile) {
  48. Serial.print("Writing to test.txt...");
  49. myFile.println("testing 1, 2, 3.");
  50. // close the file:
  51. myFile.close();
  52. Serial.println("done.");
  53. } else {
  54. // if the file didn't open, print an error:
  55. Serial.println("error opening test.txt");
  56. }
  57. // re-open the file for reading:
  58. myFile = SD.open("test.txt");
  59. if (myFile) {
  60. Serial.println("test.txt:");
  61. // read from the file until there's nothing else in it:
  62. while (myFile.available()) {
  63. Serial.write(myFile.read());
  64. }
  65. // close the file:
  66. myFile.close();
  67. } else {
  68. // if the file didn't open, print an error:
  69. Serial.println("error opening test.txt");
  70. }
  71. }
  72. void loop() {
  73. // nothing happens after setup
  74. }