Retrieve 2D array value from SD card to serial monitor

Hi. Really needs some help here.

I have a 2D array in my SD card.

I need to retrieve it and print all values (float) on Serial Monitor.

Is there any way to do this?

Thank you.

Code:

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

// change this to match your SD shield or module;
//     Arduino Ethernet shield: pin 4
//     Adafruit SD shields and modules: pin 10
//     Sparkfun SD shield: pin 8
const int chipSelect = 4;

#define maxrow 11 
#define maxcol  4 

File dataFile = SD.open("testfile.txt");

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


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
  
  // 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.
  

  // if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
      Serial.print("File existed");      
         }
  } 

     // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

void loop()
{
   for(int i=1; i<maxrow; i++)
      {
        for (int j=1; j<maxcol; j++)
        {
      int myArray[30][4]={dataFile}; 
      Serial.println(myArray[i][j]);
      }
      }
}

The following are values in my text file:

0.00 ,1384.84, 1463.85, 1341.13
1.00 ,1384.76, 1463.88, 1341.12
2.00 ,1384.99, 1463.95, 1341.15
4.00 ,1385.04, 1464.03, 1341.20
5.00 ,1384.97, 1463.83, 1341.21
6.00 ,1385.05, 1463.96, 1341.08
7.00 ,1385.00, 1463.89, 1341.28
8.00 ,1384.99, 1463.83, 1341.01
9.00 ,1385.27, 1463.97, 1341.08
10.00 ,1385.15, 1463.92, 1341.08
11.00 ,1385.15, 1463.85, 1341.04
12.00 ,1384.96, 1463.92, 1341.03
14.00 ,1384.88, 1463.99, 1341.17
15.00 ,1385.03, 1463.89, 1341.24
16.00 ,1385.03, 1463.87, 1341.17
17.00 ,1384.96, 1463.81, 1341.12
18.00 ,1384.96, 1463.88, 1341.19
19.00 ,1385.31, 1463.95, 1341.25
20.00 ,1385.05, 1463.89, 1341.31
21.00 ,1384.84, 1463.91, 1341.07
22.00 ,1384.93, 1463.91, 1341.08

Hello beegyver

Take a search engine of your choice and ask the WWW for 'sd card array reading +arduino '.

Have a nice day and enjoy coding in C++.

may be give this a go:
(compiles, NOT tested!)

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

const int chipSelect = 4;   //<-----------------------PLEASE CHECK THIS IS THE CORRECT CHIP SELECT PIN FOR YOUR SD CARD!!!!!

#define maxrow 11
#define maxcol  4

File dataFile;

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


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);

  // 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 is ready.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  dataFile = SD.open("testfile.txt"); //<-----------------------Is the file name correct?!

  // if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
      String buffer = dataFile.readStringUntil('\n');
      Serial.println(buffer); //Printing array out
    }

    dataFile.close();
  }

  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening testfile.txt");
  }
}

void loop()
{

}

hope that helps....

You forgot to open the file.

You can't initialize an array directly from a file:
int myArray[30][4]={dataFile};

You probably want a 'float' array since your data has decimal fractions.
'float myArray[maxrow][maxcol]; `

You can use nested loops to get the data from the file:

  for (int row = 0; row < maxrow; row++)
  {
    for (int col = 0; col < maxcol; col++)
    {
      myArray[row][col] = datafile.parseFloat();
      Serial.println(myArray[row][col]);
    }
  }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.