Okay, I am really trying here. This goes WAY beyond my understanding of programming. I have zero C++ background and it seems like strtok() is a native C function. This means the examples I can find are near impossible to understand. I have managed to get it to do what seems right (after a ton of trial and error) but it isn’t right.
Here is what I understand my problems to be:
-
char buf[6]; is a problem. I don’t necessarily know what the length of the calibration value is. Some are like 0.00, some are like ?0.10, and most are like 12.34. I tried char buf[5] but that starting returning gibberish. Basically, I don’t want this at all. I want to retrieve just the chunks in between the commas. I can’t get that to work.
-
My debug output looks great... but it isn’t inserting the right values into the array. It seems like it is putting the last value into every array element.
-
Right now, I am swapping variable types just to get the thing to compile. I want to build an array of floats. To get this to work, I can only get chars in there. I don’t seem to be able to parse the serial data for a comma without using chars.
#include <SdFat.h>
#include <SdFatUtil.h>
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
char* responseArray[] = {};
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
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, "CSV.TXT", O_READ)) {
Serial.println("Opened CSV.TXT");
}
else{
error("file.open failed");
}
int16_t n;
char* parameter;
int parserIteration = 0;
char buf[6]; // THIS IS BASICALLY HARD-CODING. I CAN'T GUARANTEE I KNOW THE LENGTH OF THE VALUES IN THE TXT FILE.
while ((n = file.read(buf, sizeof(buf))) > 0)
{
Serial.print("Entering ");
parameter = strtok(buf, ",");
Serial.print(parameter);
Serial.print(" into responseArray[");
Serial.print(parserIteration);
Serial.println("]");
responseArray[parserIteration] = parameter; // THESE VALUES ARE GOING INTO THE ARRAY AS CHAR* ... I NEED THEM IN THERE AS FLOATS.
parserIteration++;
while (parameter != NULL)
{
parameter = strtok(NULL, ","); // NOT SURE WHAT THIS DOES, BUT IT BREAKS IF I DON'T HAVE IT
}
}
Serial.println("\nDone\n");
Serial.println("Now, test the array...\n");
// AND THIS DOESN'T REALLY WORK AT ALL, DESPITE LOOKING CORRECT IN MY DEBUG OUTPUT
Serial.print("responseArray[0] now = ");
Serial.println(responseArray[0]);
Serial.print("responseArray[1] now = ");
Serial.println(responseArray[1]);
Serial.print("responseArray[18] now = ");
Serial.println(responseArray[18]);
}
void loop(void) {}
and my seemingly awesome serial output that does nothing...
Type any character to start
Opened CSV.TXT
Entering 00.00 into responseArray[0]
Entering 00.08 into responseArray[1]
Entering -0.10 into responseArray[2]
Entering 00.56 into responseArray[3]
Entering 01.36 into responseArray[4]
Entering 01.89 into responseArray[5]
Entering 03.15 into responseArray[6]
Entering 03.15 into responseArray[7]
Entering 05.23 into responseArray[8]
Entering 06.20 into responseArray[9]
Entering 07.34 into responseArray[10]
Entering 09.71 into responseArray[11]
Entering 09.29 into responseArray[12]
Entering 10.76 into responseArray[13]
Entering 13.43 into responseArray[14]
Entering 17.42 into responseArray[15]
Entering 16.76 into responseArray[16]
Entering 21.10 into responseArray[17]
Entering 23.51 into responseArray[18]
Entering 26.18 into responseArray[19]
Entering 27.96 into responseArray[20]
Entering 25.20 into responseArray[21]
Entering 25.83 into responseArray[22]
Entering 25.56 into responseArray[23]
Entering 26.84 into responseArray[24]
Entering 28.69 into responseArray[25]
Entering 27.88 into responseArray[26]
Entering 23.08 into responseArray[27]
Entering 25.11 into responseArray[28]
Entering 25.29 into responseArray[29]
Entering 24.52
*@ into responseArray[30]
Done
Now, test the array...
responseArray[0] now = 24.52
*@
responseArray[1] now =
responseArray[18] now = 24.52
*@
Getting closer!