BackwardCompatibility.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. }
  30. #if USE_SD_H
  31. Serial.println(F("Using SD.h. Set USE_SD_H zero to use SdFat.h."));
  32. #else // USE_SD_H
  33. Serial.println(F("Using SdFat.h. Set USE_SD_H nonzero to use SD.h."));
  34. #endif // USE_SD_H
  35. Serial.println(F("\nType any character to begin."));
  36. while (!Serial.available()) {
  37. yield();
  38. }
  39. Serial.print("Initializing SD card...");
  40. if (!SD.begin(SD_CS_PIN)) {
  41. Serial.println("initialization failed!");
  42. return;
  43. }
  44. Serial.println("initialization done.");
  45. // open the file.
  46. myFile = SD.open("test.txt", FILE_WRITE);
  47. // if the file opened okay, write to it:
  48. if (myFile) {
  49. Serial.print("Writing to test.txt...");
  50. myFile.println("testing 1, 2, 3.");
  51. // close the file:
  52. myFile.close();
  53. Serial.println("done.");
  54. } else {
  55. // if the file didn't open, print an error:
  56. Serial.println("error opening test.txt");
  57. }
  58. // re-open the file for reading:
  59. myFile = SD.open("test.txt");
  60. if (myFile) {
  61. Serial.println("test.txt:");
  62. // read from the file until there's nothing else in it:
  63. while (myFile.available()) {
  64. Serial.write(myFile.read());
  65. }
  66. // close the file:
  67. myFile.close();
  68. } else {
  69. // if the file didn't open, print an error:
  70. Serial.println("error opening test.txt");
  71. }
  72. }
  73. void loop() {
  74. // nothing happens after setup
  75. }