#include <SD.h>
#define CS_PIN 10
#define MAX_LEN 32
char line[MAX_LEN];
size_t pos = 0;
uint32_t t0;
size_t openFile(const char* filename) {
File file = SD.open(filename);
if (file) {
while (file.available()) {
Serial.write(file.read());
}
size_t size = file.size();
file.close();
return size;
} else {
Serial.println("error opening wokwi.txt!");
}
return 0;
}
void appendValue(const char* filename) {
File file = SD.open(filename, FILE_WRITE);
if (file) {
file.seek(pos);
snprintf(line, MAX_LEN, "% 4d; % 4d; % 4d; % 4d;\n",
analogRead(A0),
analogRead(A1),
analogRead(A2),
analogRead(A3)
);
pos += file.write(line);
file.close();
}
}
void setup() {
Serial.begin(115200);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("initialization done.\n");
pos = openFile("wokwi.csv");
}
void loop() {
t0 = millis();
appendValue("wokwi.csv");
Serial.println(millis() - t0);
}