My data has 34 columns and 1000 plus rows. I read Serial Basics and also SD Card Library for the sketch.
The problem I have is that the code flow is not doing what I need it to do, which is parsing the CSV data.
// include the SD library:
#include <SPI.h>
#include <SD.h>
const byte numChars = 400;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;
int Nose = 0;
int LEye = 0;
File myFile;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
myFile = SD.open("ds2.CSV");
}
void loop() {
recvWithStartEndMarkers();
while (myFile.available()) {
Serial.write(myFile.read());
if (newData == true)
{
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
}
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '>';
char endMarker = '<';
char rc;
while (Serial.available() > 0 && newData == false) { // <<== NEW - get all bytes from buffer
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,"*"); // get the first part - the string
Nose = atoi(strtokIndx);
strtokIndx = strtok(NULL, "*"); // this continues where the previous call left off
LEye = atoi(strtokIndx); // convert this part to an integer
}
void showParsedData() {
Serial.print(Nose);
Serial.print(" _ ");
Serial.print(LEye);
Serial.println("*****");
}
My data looks like:
131281402412824131531184913266129*-14120781268125351265013912614973155168153170150247156249<
Any help will be highly appreciate it.
Thanks