The problem is that print()
and write()
behave differently when you give them numerical data, such as a byte.
When you pass your byte to print()
, it converts it to text which is convenient for humans to read. For example:
byte b = 10;
dataFile.print(b);
This will result in the following ASCII characters being written to the file:
10
That's actually two bytes: one byte with the value 49, which represents ASCII character 1
, followed by a byte with the value 48 representing character 0
.
However, write()
does not convert numbers into text. It writes them in their raw binary form. For example:
byte b = 10;
dataFile.write(b);
When you do this, it doesn't write a "1" and a "0" to the file. It literally writes a binary byte with the value 10. When you try to open this as a text file, your computer interprets that byte as a single ASCII character. It so happens that ASCII value 10 is the newline character, meaning your file basically contains a blank line.
For comparison, try this instead:
byte b = 65;
dataFile.write(b);
ASCII value 65 corresponds to the letter A
(uppercase), so that's what you should see in the file.
The write()
functions are useful for writing raw data to file in the most efficient form for another machine to read (as opposed to being human-readable). This is why they allow you to write arrays -- it acts like one big block of raw data with a fixed format.
In your case, print()
is more appropriate. That means for outputting arrays of numbers, you will need to use loops. That functionality isn't built-in to the print()
functions because different projects might need different formatting. For example, some people might want one number per line, whereas others might want numbers separated by commas, and so on.
SD.begin()
anywhere?