Pages: [1]   Go Down
Author Topic: How to read array of bytes  (Read 690 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello

I want to read and write an array of bytes to a file on sd card and I have some questions about it. The SD library supports the function write with parameters buf and len possible. That seems to be good for my plan.
But how does reading works? I don`t understand it exactly.

Is this the right way?:
Code:
CFile = SD.open("led01.txt");
  if (CFile)
  {
    for (int i=0; i<96; i++)
    {
      led01[i] = CFile.read();
    }
    CFile.close();
  } 

I want to fill the array led01 with the bytes of the file (96 bytes)
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Today I get my Arduino MEGA and I have tested my code. It works great!

I think it works in this way: The first call of read() returns the first byte in the file. The next call of read() returns the next Byte and so on until there is nothing to read

greetings
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 100
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can read SD card files line-by-line if this helps:

Code:
String l_line = "";
while (l_SDFile.available() != 0) {
    //A consistent line length will not lead to heap fragmentation
    l_line = l_SDFile.readStringUntil('\n');
    if (l_line == "") //no blank lines are anticipated
      break;
    //
 
   //Load into array here

}
   

Cheers

Catweazle NZ
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7175
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, read and write work the same way. The library keeps an internal pointer to where to read/write the next byte. All you need to do is repeatedly call read or write. Is your led01 array defined as a byte (or char) array?
Logged


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

Today I get my Arduino MEGA and I have tested my code. It works great!

I think it works in this way: The first call of read() returns the first byte in the file. The next call of read() returns the next Byte and so on until there is nothing to read

greetings

Arduino mega? How did you conect the signals of the sd card to the pins of the arduino?
Logged

Pages: [1]   Go Up
Jump to: