#include <SPI.h>
#include <SD.h>
File myFile;
volatile float time = 0;
int num = 1;
#define pot A0
#define timestamp 1000.0 // make it as you like
#define timeinseconds timestamp/1000.0
#define TimeBeforeReading (10.0-1.0)*timeinseconds
uint16_t p , n;
volatile bool printed = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
p = millis();
}
void WriteinFile(String filename, int data)
{
myFile = SD.open(filename, FILE_WRITE);
if (myFile) {
printed = 0;
Serial.println("Writing to test.txt...");
myFile.print(num);
myFile.print(". ");
myFile.println(data);
num++;
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
myFile.close();
}
void loop() {
// put your main code here, to run repeatedly:
if (millis() - p > timestamp && time <= TimeBeforeReading)
{
WriteinFile("test.txt", analogRead(pot));
p = millis();
time+=timeinseconds;
}
if( time > TimeBeforeReading && printed == 0)
{
printed = 1;
//reading the data...
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
time = 0 ;
}
}