This still isn’t working correctly.
I am currently running this code. It reads in a text file in CSV format containing values like 0.01, ?0.12, 23.98, etc. I am trying to put each value into an array. Right now, it seems like I am putting each char into the array. Current code:
#include <SdFat.h>
#include <SdFatUtil.h>
#include <string.h>
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
char responseArray[] = {};
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
void setup(void) {
Serial.begin(9600);
// Serial.println();
// Serial.println("Type any character to start");
// while (!Serial.available());
Serial.println();
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!card.init(SPI_HALF_SPEED)) error("card.init failed");
// initialize a FAT volume
if (!volume.init(&card)) error("volume.init failed");
// open the root directory
if (!root.openRoot(&volume)) error("openRoot failed");
// open a file
if (file.open(&root, "CR.TXT", O_READ)) {
Serial.println("Opened CR.TXT");
}
else{
error("file.open failed");
}
Serial.println();
String receivedData;
// copy file to serial port
int16_t n;
uint8_t buf[7];// nothing special about 7, just a lucky number.
while ((n = file.read(buf, sizeof(buf))) > 0) {
for (int i = 0; i < n; i++)
{
responseArray[i] = buf[i];
Serial.print("responseArray[");
Serial.print(i);
Serial.print("] = ");
Serial.println(responseArray[i]);
}
}
Serial.println("\nDone");
Serial.println(responseArray[0]);
Serial.println(responseArray[1]);
Serial.println(responseArray[2]);
Serial.println(responseArray[3]);
Serial.println(responseArray[4]);
Serial.println(responseArray[5]);
Serial.println(responseArray[6]);
}
void loop(void) {
}
My output looks like this:
responseArray[0] = 0
responseArray[1] = .
responseArray[2] = 0
responseArray[3] = 0
responseArray[4] = ,
responseArray[5] =
responseArray[6] = 0
responseArray[0] = .
responseArray[1] = 0
responseArray[2] = 8
responseArray[3] = ,
responseArray[4] =
responseArray[5] = -
responseArray[6] = 0
It’s going in there, but it is going in one value at a time.
The last Serial.printlns at the end are telling me that it isn’t working. It seems like I am still missing something in the code that tells it when to stop reading the file, consider those values a single number, and put THAT into the array. I need a CSV parser of some sort. There needs to be something that tells it to stop at a comma, no?
I have found strtok and sscanf ... but no example of how to use them when reading from an SD card. I have done string parsing before, but never when it is coming in nonstop from the SD card. That is where I am lost.
Many thanks.
Ryan
I