Go Down

Topic: Store into multiple Variables from SD card (Read 382 times) previous topic - next topic


I want to log distances on a robot and then compare them later, there are 4 sensors, so 4 sets of data to log.

i know its possible to write/print the data to the SD card using

Code: [Select]

    Data_logger.print(Distance_1, 2);
    Data_logger.print("      ");
    Data_logger.print(Distance_2, 2);
    Data_logger.print("      ");
    Data_logger.print(Distance_3, 2);
    Data_logger.print("      ");
    Data_logger.print(Distance_4, 2);

which will give me a single line of 4 numbers, each time the code is called.

is there a way to reverse that?

the data file will have content such as

line 1: distance1 distance2 distance3 distance4
line 2: distance1 distance2 distance3 distance4
line 3: distance1 distance2 distance3 distance4

and each iteration it stores line 1: distance1 in variable 1, 2 in 2 and so on, then it does line 2 then line 3!


is there a way to reverse that?
Yes. Read each character from the file. If the character is not a carriage return, store the character in the next position in an array, and increment the index.

If the character IS the carriage return, store a NULL in the next position in the array, and then use strtok() and atoi() or atof() to convert each token to an int or a float. Then, reset the index to 0 and put a NULL in the 0th position of the array.
The art of getting good answers lies in asking good questions.


okay that sounds like a good solution!

but wont that save all values in one row into one variable? unless i have a statement that checks if the character is a " " or something, then it can convert the saves characters into an int, then continue until a carriage return. increase the index for the final saved ints and repeat for the whole file.

but how do i check each character individually? this method does take time though?

so far my knowledge is limited to "data.read(distance.txt)"

thanks a bunch


What are you using (post code) that isn't reading from the SD card one character (byte) at time?

So on the premise your program is reading one character at a time. If the character is not a space then add to a char[]. If the new character is a space then convert the char[] to the number value (atoi or atof) and store in the corresponding variable (a counter and switch statement could help with this if the variables are not an array).


my bad, i just didnt understand how the SD card read worked, i just need it to read values and store them with a counter to split them into variables.

i have 4 variables that represent the data (distance values) that change a few times a second, so i created a 2D array to store the values. it reads the char in the sd card, saves them to an array, converts them to an int when a " " is reached. then it increases the counter and resets the array counter. at a counter of 4, it resets back to 0 and starts all over again for the next set of data.

thanks for the help!

Go Up