#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
File dataFile;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.print("\nInitializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed. Things to check:");
Serial.println("* Is a card inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* Did you change the chipSelect pin to match your shield or module?");
while (1);
} else {
Serial.println("Wiring is correct and a card is present.");
}
dataFile = SD.open("data.txt", FILE_WRITE);
if (!dataFile) {
Serial.println("Error opening data file!");
}
}
void loop() {
// Write 5 different sayings to the file
for (int i = 1; i <= 5; i++) {
dataFile.print("Saying ");
dataFile.print(i);
dataFile.println(": Hello from SD card!");
}
// Flush the data to ensure it's written to the SD card
dataFile.flush();
Serial.println("Data written to SD card successfully!");
// Delay before repeating the loop
delay(5000); // Adjust the delay as needed
}