Is it the way I write or the way I read ? ...

Hi all. I hope I’m doing this the right way … it’s my very first Post here.

I’ve posted this on another thread and it got no attention, so I’m starting a new subject, just in case. I think it has better chance to get replies from a helping hand. Please, allow me to explain my problem.

I want to write and read ‘short’ and ‘float’ variables on a microSD card. So far, I had no problem doing the writing part of it and I can read it back with a text editor such as TextEdit and I can see the exact same things I wrote. When I want to read it back, though, I get some data back, but not at all what I expect. It seems I get ASCII values of the digits I previously stored and this, one character at a time.

The might be irrelevant, but I get the data from a ‘struct’ string sent via an nRF24L01+ bluetooth module. I’m sure you all know about these wonderful little modules. I’ve made the sketch to its simplest not to get confusing. I get a ‘short’ (the year) although there are much more data involved in the original sketch. I just don’t read it right, that’s all.

I try to save it the following way … (My struct is called ‘Station’. So here goes …

void Save_Data () // Write variables on SD Card
{
Log_File = SD.open (“TEST_LOG.TXT”, FILE_WRITE); // Opens ‘TEST_LOG.TXT’

if (Log_File)
{
Log_File.print (Station.Year); // Store the ‘Year’ variable contained in the ‘struct

Log_File.close (); // Closest file.
}
}

I tried writing the data so many ways before I came here, taking tips from many Boards and Forums, but nothing seems to solve my problem. I tried converting to Strings, but I have ‘floats’ that won’t convert and all sorts of problem … it’s very frustrating. So here id the code I use to read the variable(s) back from the file.

void Read_Data () // Read Data from File
{
Log_File = SD.open (“TEST_LOG.TXT”, FILE_READ);

if (Log_File)
{
Year = Serial.write (Log_File.read ()); // ‘Year’ was previously declared as of type ‘short

… and I trie reading it this was as well : Year = Log_File.read();

Serial.println (Year);
Log_File.close ();
}
}

I’m sure there is a simple way of doing this but, being very green on Arduino, I fail to grasp some concept and this leads me nowhere. Is it the way I write the data or the way I read it ? … or both, for that matter! :slight_smile: :confused:

Is there someone here that could lead me into doing it the right way so I can go on in my Wireless Weather Station project (Yes, another one! Hahaha!). Thank you in advance for any help.

Best regards and have a great evening.

Yes, the print methods print things out in ascii. That's why you can open it and read it and see the things you wrote, because it's ascii. If it wasn't ascii then when you opened it you'd just see a bunch of random junk as your text editor tried to make ascii out of it.

You can write it out in raw bytes using write instead of print. Or you can do it like you have and read it back in expecting it to be in ascii. Read the digits into a buffer and use atoi or strtoul or something like that to convert it back to a number.

1 Like