#include "FS.h"
#include "SD.h"
#include "SPI.h"
int entryNumber= 0;
void setup() {
Serial.begin(115200);
if (!SD.begin(5)) {
Serial.println("Card Mount Failed");
return;
}
Serial.println("Card detected.....");
// Write data to a text file
writeFile("/data.csv","Time Stamp : Analog Values : Weight in lbs\n");
}
void loop() {
// Sample analog data and write to the file
for (int i = 0; i < 3; i++) {
int analogValue = analogRead(13); // Read analog value from pin A0
entryNumber = entryNumber + 1; // Entry number
// Format data as a string
String data = String(millis()/1000) + " : " + String(analogValue) + " : " + String(entryNumber) + "\n";
// Write data to the file
appendFile("/data.csv", data.c_str());
delay(100); // Adjust delay as needed
}
readFile("/data.csv");
// Nothing to do here
delay(5000);
}
void appendFile(const char *path, const char *message) {
File file = SD.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (file.print(message)) {
Serial.println("Data appended successfully");
} else {
Serial.println("Append failed");
}
file.close();
}
void writeFile(const char *path, const char *message) {
File file = SD.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written successfully");
} else {
Serial.println("Write failed");
}
file.close();
}
void readFile(const char *path) {
File file = SD.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.println("File contents:");
// Read from the file until there's nothing left
while (file.available()) {
// Read a line from the file and print it
String line = file.readStringUntil('\n');
Serial.println(line);
}
file.close();
}