Here's an updated example where, instead of saving each line of the SD card as a string datapoint during playback mode ("500: 24"), each line of the SD card (ex "500,24,110") is parsed into 3 different data variables : time of sample, emg, eog.
There's also a place to run the data, recorded or in real time, through the program:
if (recording || playData) {
//---------regular program goes here--------
// example of handing data
handleData( timeData, emg, eog);
}
Serial monitor
14:47:45.674 -> Mode: RECORDING
14:47:45.674 ->
14:47:45.674 -> Initializing SD card...initialization done.
14:47:45.674 -> deleting file.
14:47:45.739 -> DATA.TXT doesn't exist. Now creating....
14:47:45.739 ->
14:47:45.739 -> RECORDING...
14:47:45.782 -> timeData: 100 emg: 396 eog: 357
14:47:45.855 -> timeData: 200 emg: 366 eog: 357
14:47:45.959 -> timeData: 300 emg: 354 eog: 351
14:47:46.066 -> timeData: 400 emg: 346 eog: 345
14:47:46.141 -> timeData: 500 emg: 339 eog: 338
14:47:46.245 -> timeData: 600 emg: 333 eog: 332
14:47:46.352 -> timeData: 700 emg: 328 eog: 326
14:47:46.453 -> timeData: 800 emg: 322 eog: 320
14:47:46.555 -> timeData: 900 emg: 318 eog: 316
14:47:46.658 -> timeData: 1000 emg: 312 eog: 311
14:47:46.764 -> timeData: 1100 emg: 308 eog: 307
14:47:46.868 -> timeData: 1200 emg: 305 eog: 303
14:47:46.936 -> timeData: 1300 emg: 301 eog: 299
14:47:47.046 -> timeData: 1400 emg: 298 eog: 296
14:47:47.155 -> timeData: 1500 emg: 296 eog: 294
14:47:47.155 -> Done recording.
14:48:00.703 -> Mode: PLAYBACK
14:48:00.703 ->
14:48:00.703 -> Initializing SD card...initialization done.
14:48:00.703 ->
14:48:00.703 -> PLAYING BACK...
14:48:00.808 -> timeData: 100 emg: 396 eog: 357
14:48:00.881 -> timeData: 200 emg: 366 eog: 357
14:48:00.992 -> timeData: 300 emg: 354 eog: 351
14:48:01.101 -> timeData: 400 emg: 346 eog: 345
14:48:01.205 -> timeData: 500 emg: 339 eog: 338
14:48:01.277 -> timeData: 600 emg: 333 eog: 332
14:48:01.387 -> timeData: 700 emg: 328 eog: 326
14:48:01.491 -> timeData: 800 emg: 322 eog: 320
14:48:01.602 -> timeData: 900 emg: 318 eog: 316
14:48:01.674 -> timeData: 1000 emg: 312 eog: 311
14:48:01.778 -> timeData: 1100 emg: 308 eog: 307
14:48:01.878 -> timeData: 1200 emg: 305 eog: 303
14:48:01.991 -> timeData: 1300 emg: 301 eog: 299
14:48:02.093 -> timeData: 1400 emg: 298 eog: 296
14:48:02.199 -> timeData: 1500 emg: 296 eog: 294
Code
#include <SPI.h>
#include <SD.h>
File myFile;
bool recording = // set whether we should be recording data or playing back data
false;
// true;
bool readData = true; // false when we saved a data point from the SDcard to the input string
// and haven't handled it yet during a sampling rate moment,
// so we to pause reading data, ie pause before reading the next data point from the SD card,
// until we handle the last data point. Not used in recording mode.
bool playData = true; // false when we get to the end of the SDcard file. Not used in recording mode.
int samplingDelta = 100;
unsigned long startTime = millis();
unsigned long actualTime;
unsigned long loops = 0;
int stringIndex = 0;
const size_t BUF_DIM = 50;
char inputString[BUF_DIM];
char inputChar;
unsigned long timeData;
int eog;
int emg;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println();
Serial.print("Mode: ");
Serial.println(recording ? "RECORDING" : "PLAYBACK");
Serial.println();
Serial.print("Initializing SD card...");
if (!SD.begin(5)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
if (recording) {
deleteAndRecreateFile();
myFile = SD.open("DATA.TXT", FILE_WRITE);
}
if (!recording) {
myFile = SD.open("DATA.TXT");
}
Serial.println();
Serial.println(recording ? "RECORDING..." : "PLAYING BACK...");
}
void loop() {
actualTime = millis();
// -------------if PLAYING BACK, get next data point from SD card---------
if (!recording && readData && inputChar != EOF ) {
inputChar = myFile.read();
while (inputChar != '\n' && inputChar != EOF) {
inputString[stringIndex] = inputChar;
stringIndex++;
inputChar = myFile.read();
}
if (inputChar == '\n' || inputChar == EOF) {
readData = false;
}
stringIndex = 0;
}
bool timeToSample = actualTime - startTime >= samplingDelta;
// -----------------------SAMPLE-----------------------
if ( timeToSample) {
// -----------------if RECORDING get biodata from sensors-----------------
if (recording) {
int sensorValue0 = analogRead(A0);
int sensorValue1 = analogRead(A1);
emg = sensorValue0;
eog = sensorValue1;
timeData = actualTime;
writeToSD( emg, eog, timeData, myFile); //not working
} else {
if (playData) {
if (inputChar != EOF) {
// updates values recordingTime, emg, eog
parseData(inputString, timeData, emg, eog);
readData = true;
}
if (inputChar == EOF) {
playData = false;
}
}
}
startTime = startTime + samplingDelta;
if (recording || playData) {
//---------regular program goes here--------
// example of handing data
handleData( timeData, emg, eog);
}
}
if (recording && millis() > samplingDelta * 15) stopCollectingData(); // To limit the recording for testing purposes.
}
void parseData(String data, unsigned long& timeData, int& emgData, int& eogData) {
int ary[2];
int aryI = 0;
String singleData = "";
//string data to array
for (int i = 0; i < data.length(); i = i + 1)
{
if ( data[i] == ',') {
ary[aryI] = singleData.toInt();
singleData = "";
aryI++;
i++;
}
singleData = singleData + data[i];
if ((i + 1) == data.length()) {
ary[aryI] = singleData.toInt();
timeData = ary[0];
emgData = ary[1];
eogData = ary[2];
}
}
}
void handleData( int timeData, int emgData, int eogData) {
// real code would go here
Serial.print("timeData: ");
Serial.print(timeData);
Serial.print(" emg: ");
Serial.print(emgData);
Serial.print(" eog: ");
Serial.println(eogData);
}
void writeToSD(int emg, int eog, unsigned long timeOfSample, File file) {
if (file) {
// Serial.print(F(" t: "));
// Serial.print(timeOfSample);
// Serial.print(F(" emg: "));
// Serial.print(emg);
// Serial.print(F(" eog: "));
// Serial.println(eog);
file.print(timeOfSample);
file.print(",");
file.print(emg);
file.print(",");
file.println(eog);
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void deleteAndRecreateFile() {
Serial.println("deleting file.");
SD.remove("DATA.TXT");
if (SD.exists("DATA.TXT")) {
Serial.println("---ERROR DELETING----DATA.TXT exists.");
}
else {
Serial.println("DATA.TXT doesn't exist. Now creating....");
myFile = SD.open("DATA.TXT", FILE_WRITE);
myFile.close();
}
}
void stopCollectingData() {
myFile.close();
Serial.println("Done recording.");
delay(9000000000); //not a good real world solution for ending the code, this just pauses the script for sample purposes purposes
}