Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <SD.h>
  2. #include <SPI.h>
  3.  
  4. File test;
  5.  
  6. int pinCS = 10;
  7.  
  8. void setup() {
  9.  
  10.   Serial.begin(9600);
  11.   //pinMode(pinCS, OUTPUT);
  12.   SD.begin(pinCS);
  13.  
  14.   // SD Card Initialization
  15.   if (SD.begin())
  16.   {
  17.     Serial.println("SD card is ready to use.");
  18.   } else
  19.   {
  20.     Serial.println("SD card initialization failed");
  21.     return;
  22.   }
  23.  
  24.   // Create/Open file
  25.   test = SD.open("test.txt", FILE_WRITE);
  26.  
  27.   // if the file opened okay, write to it:
  28.   if (test) {
  29.     Serial.println("Writing to file...");
  30.     // Write to file
  31.     test.println("Testing text 1, 2 ,3...");
  32.     test.close(); // close the file
  33.     Serial.println("Done.");
  34.   }
  35.   // if the file didn't open, print an error:
  36.   else {
  37.     Serial.println("error opening test.txt");
  38.   }
  39.   // Reading the file
  40.   test = SD.open("test.txt");
  41.   if (test) {
  42.     Serial.println("Read:");
  43.     // Reading the whole file
  44.     while (test.available()) {
  45.       Serial.write(test.read());
  46.    }
  47.     test.close();
  48.   }
  49.   else {
  50.     Serial.println("error opening test.txt");
  51.   }
  52.  
  53.  
  54.  
  55. }
  56.  
  57. void loop() {
  58.   // put your main code here, to run repeatedly:
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement