Pages: [1]   Go Down
Author Topic: Read/Write short to SD card  (Read 531 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey all,

This seems like a very simple question, however I am having some trouble with it.

I wish to write a 2 byte value (unsigned short) to an SD card, and then read that same 2 byte value later on.

Saving and reading to the SD card all works fine, however I can only find how to read and write 1 byte at a time, using the basic functions such as file.read(), file.write().

Any help would be appreciated!
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So, read one byte twice, and combine them?
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I should have been more specific, I do understand how to read 2 bytes and join them together, however I can only seem to write into the file as ascii.

For example, if I wanted to write in the file the number 425, file.print() writes it in as the seperate digits '4', '2', and '5'.

Is there any way to write into the file as a complete number? Or prehaps combine the seperate digits '4', '2' and '5' into a complete number again?

Cheers!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45972
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
For example, if I wanted to write in the file the number 425, file.print() writes it in as the seperate digits '4', '2', and '5'.
Sure, that's what print() does.

Quote
Is there any way to write into the file as a complete number?
Sure, that's what write() does.

Quote
Or prehaps combine the seperate digits '4', '2' and '5' into a complete number again?
Sure, that's what atoi() does, once you've stored the characters in a NULL terminated array of chars.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could either store the value as ASCII on the SD card and convert it back using atoi(), or you can use the .write() function to write individual bytes.

You can separate bytes out, and recombine them, by using simple boolean algebra - AND (&), OR (|), left shift (<<) and right shift (>>).

For example, to turn a 16-bit integer into two 8-bit bytes:

Code:
int in = 3769;
byte b1,b2;

b1 = in >> 8;
b2 = in & 8;

And to re-combine them again:

Code:
in = (b1 << 8) | b2;
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks both for your replies,

I was having trouble as atoi() would write as an integer, and I wanted a short, and file.write() would only let me write 1 byte of a 2 byte number.

But I will use the method of splitting the number into seperate bytes before writing it, thanks!
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino uses 16-bit integers.  That's the same as a short.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well damn. I have no idea how I didn't already know that, too used to C I guess!

Thanks a bunch, thats some important info!
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino is programmed in C.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Pages: [1]   Go Up
Jump to: