Isue reading file from SD

Hi guys. I’m new to Arduino and I’m trying to do the folllowing:

Read a file from SD with this format (example)

0,0,0,0
1,1,1,1
2,2,2,2

create an array from those values

Read another file from SD with this format (example)

0,0,0
1,1,1
2,2,2

create an array from those values.

I’m using this code that I saw here in the forum, combined with the “filedump” example

/*
SD card file dump

This example shows how to read a file from the SD card using the
SD library and send it over the serial port.

The circuit:

  • SD card attached to SPI bus as follows:
    ** MOSI - pin 11
    ** MISO - pin 12
    ** CLK - pin 13
    ** CS - pin 4

created 22 December 2010
by Limor Fried
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;
int Main_Array[1][3];
int Main_Array2[1][3];
int Main_Array3[1][3];

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print(“Initializing SD card…”);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
return;
}
Serial.println(“card initialized.”);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File myFile2 = SD.open(“modem.txt”);
while (myFile2.available()){
for (int e=0; e<=3; e++){
for(int r=0; r<=3; r++){
Main_Array2[e][r] = myFile2.parseInt();
Serial.println(Main_Array2[e][r]);}
}
}
myFile2.close();

Serial.print(“A test Value is:”);
Serial.println(Main_Array2[1][1]);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File myFile = SD.open(“patchm.txt”);
while (myFile.available()){
for (int q=0; q<=5; q++){
for(int w=0; w<=2; w++){
Main_Array[q][w] = myFile.parseInt();
Serial.println(Main_Array[q][w]);}
}
myFile.close();}

Serial.print(“A test Value is:”);
Serial.println(Main_Array[1][1]);

}
void loop()
{
}

The issue is, it only works for one of the files. If I comment the code concerning one of the the files,the other one works OK.

Can you help, please?

Best regards

What do you mean by 'it works for one of the files'?

You might have a possible memory leak by the looks of it :wink:
e.g. Main_Array2 consists of one element that can hold 3 integers. You however try to dump 9 integers in there (two for loops); it probably depends on the data in the file. I have never used parseInt() (and will never use it) so I might see it wrong. Can you post example data so we can look at it?

Please edit your post and use code tags for the code. Place
** **[code]** **
before the code in your post and
** **[/code]** **
after the code in your post. So it looks like

your code here

// edit
PS
You can apply the principles of Serial Input Basics - updated to read your file. There is basically no difference between reading from serial port and reading from serial port file.

There is basically no difference between reading from serial port and reading from serial port.

Of course not. 8)

There is basically no difference between reading from serial port and reading from a file, either.

PaulS:
Of course not. 8)

There is basically no difference between reading from serial port and reading from a file, either.

Thanks for confirming :smiley: Will neatly correct