Can someone please give me a simple example of using numeric data (float or int) with FileLogger::append to write data to the SD card? There are plenty of text-data examples around.
I've tried many solutions posted here and haven't gotten anything to work - I'm pulling out my hair here!
The ability to write integer or float data to a file starts with how you open the file. If you open it as a text file, writing binary data to it will not work. If you open it as a binary file, it will.
So, how are you opening the file? As text or as binary?
What have you tried, and what problems have you encountered?
byte data[1];
int temp = 123;
data[0] = temp;
unsigned long length = sizeof(data[1]);
FileLogger::append("data.txt", data, length);
When I look at the SD card (in Notepad say) I get lots of garbage.
I'm really just trying to log some numeric data (float or int) to the SD card. I've tried several examples I found here (including the sprintf ones) and this is the closest I've come to getting anything to work.
I would recommend the SparkFun OpenLog. I bought one and it is very easy to use. It requires 2 pins from the Arduino to talk to it, but it's very simple. Price seems a bit steep at first, but once you've used it, it's worth the price.
This method is, of course, not writing numeric data to a file. It is writing an ascii representation of numeric data to a file. A fine distinction, but real none the less.
Writing float data is a bit more difficult as the %f format is not supported by sprintf on the Arduino.
You could split the float into two parts - the integer part and the fractional part:
float someValue = 14.375;
int intPart = someValue;
int fracPart = (someValue - intPart) * 100; // Use larger value for more decimal places
// fracPart will be 37
Then, use sprintf to generate a string using both parts:
Yeah, I just get the sense that people copy code when it's supplied, and I wanted to point out that the first sprintf statement wasn't a robust answer in all cases, so that people wouldn't use it verbatim. Your followup above is better.
[edit]You probably meant to use sprintf(buffer, "%i.%[glow]0[/glow]2i", intPart, fracPart);[/edit]
People will have to understand how to adjust it to get the number of decimal places they want, and that's not necessarily a bad thing.
You have a good example of a loop in another situation elsewhere on the forum, converting incoming individual bytes via serial to an int. The loop multiplies an intermediate result by 10 and adds the next digit received. An extension of that loop could be devised for converting floats to a string. The loop would also have to be adjusted, depending on the number of decimal places desired, much like the note in your last line above.