#include <SPI.h> // SPI Library for SPI interface
#include <SD.h> // Library for sd card module
File myFile; // Create a file object that will later be used to access the file
void setup() {
if(!SD.begin()) { // SD.begin() will return true if the sd card is accessible. SD.begin(cs) to use a select chip other than the default pin
Serial.println("Failed read open Micro SD card!");
return;
}
}
void loop(){
myFile = SD.open("test.txt", FILE_READ); // Open test.txt file in read mode
if (myFile) { // myFile will be true if the file is opened successfully 18 // if it fails or the file does not exist then it will be false
while(myFile.available()) { // loop as long as the file is available
// file.read will read the line later
// proceed to the next line
Serial.write(myFile.read());
}
myFile.close(); // Close a file
} else {
Serial.println("Failed to open test.txt");
}
myFile = SD.open("test.txt", FILE_WRITE); // Read a test file in write mode
if (myFile) {
myFile.print("06/09/2022,");
myFile.print(int("t"));
myFile.print(",");
myFile.println(int("h"));
myFile.close();
} else { // If the file does not exist then the file will be created
Serial.println("error opening di looop");
}
}