Thanks for the suggestions I'm going to try to implement them.
Here it is what I have so far for the logging and reading
#include <SPI.h>
#include <SD.h>
File SensorDataout;
File SensorDatain;
// change this to match your SD shield or module;
const int chipselect = 10;
void setup()
{
Serial.begin (9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(SS, OUTPUT);
/* Let's check is the file exists, if it doesn't
we will exit
*/
if (!SD.begin(chipselect)){
Serial.println("Initialization failed!");
return;
}
Serial.println("Initialization done!");
if (SD.exists("EMGdata3.txt")){
Serial.println("EMGdata3.txt exists.");
}
else{
Serial.println("EMGdata3.txt doesn't exists.");
makeFile();
}
// cals the write function
writeData();
//////////******************************************///////////////
////////// This part begins the sd reader part ////////////////
///////// for the setup part /////////////////
//////////******************************************///////////////
SensorDatain = SD.open("EMGdata3.txt");
if (SensorDatain) {
Serial.println("EMGdata3.txt:");
// read from the file until there's nothing else in it:
while (SensorDatain.available()) {
char byteIn = SensorDatain.read();
int data1;
int data2;
int data3;
while (byteIn != '\n') {
data1 = SensorDatain.parseInt();
data2 = SensorDatain.parseInt();
data3 = SensorDatain.parseInt();
//ln = Serial.write(datatest.read());
//Serial.print("this is data1 = ");
Serial.println(data1);
}
}
// close the file:
SensorDatain.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening EMGdata3.txt");
}
}
void loop()
{
//////////******************************************///////////////
////////// This part begins the sd write part ////////////////
///////// /////////////////
//////////******************************************///////////////
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
SensorDataout.println(dataString);
//Serial.println(dataString);
SensorDataout.flush();
}
void makeFile()
{
/* Let's make a file in case we don't have one already */
Serial.println("Let's make that file");
SensorDataout = SD.open("EMGdata3.txt", FILE_WRITE); //FILE_WRITE is the flag that allow us to creat a new fil
SensorDataout.close();
// let's make sure the file was created
// Check to see if the file exists:
if (SD.exists("EMGdata3.txt")) {
Serial.println("EMGdata3.txt exists.");
}
else {
Serial.println("EMGdata3.txt doesn't exist.");
}
}
void writeData()
{
//////////******************************************///////////////
////////// This part begins the sd write part ////////////////
///////// for the setup part /////////////////
//////////******************************************///////////////
SensorDataout = SD.open("EMGdata3.txt", FILE_WRITE);
if (! SensorDataout) {
Serial.println("error opening EMGdata3.txt");
// Wait forever since we cant write data
while (1) ;
}
}