Go Down

Topic: Reading csv to Array (Read 1 time) previous topic - next topic

Loren

I'm trying to read csv data from a micro SD card to an array.

Here is the code that I've come up with:

Code: [Select]
#include "SD.h"


int CS_pin = 4;
File myFile;
File aFile;
int index = 0;
  int row = 0;
  int col = 0;

void setup(){
Serial.begin(9600);
pinMode(CS_pin, OUTPUT);


// check if the card is ready

  if(!SD.begin(CS_pin)){
    Serial.println("Card Failed");
    return;
  }
  Serial.println("Card Ready");
 

  char LineData[255];
  char d;
  char o; //char counter
  aFile = SD.open("two.csv");
 
  while (aFile.available()){
   
    d = aFile.read();
    // if this is not a newline char, store and move on
            if (d != '\n') { //not a new line
               LineData[o] = d;
                o++;
                if (LineData[o] != ','){
                 }else{
                    col++;
                  }
            }else {
             
                row++;
                col = 0;

            }//close checking for new line char
           
  }//Close While
  aFile.close();
  col++;
     Serial.println("One Column Added");
     Serial.print("Columns:  ");
     Serial.println(col);
     Serial.print("Rows:  ");
     Serial.println(row);
 
  String dArray [row][col];
   myFile = SD.open("two.csv");
   
int n = 0; // the character counter
int prev = 0;
String inString;
int aRow=0;
int aCol=0;
char LineFromFile[255]; 
char c; // character read in from file


while (myFile.available()) {
    c = myFile.read();
       // if this is not a newline char, store and move on
            if (c != '\n') {
             LineFromFile[n] = c;
                n++;
               
                   // this works fine for now
                    int xyz;
                      for (xyz = 0; xyz< n; xyz++){
                      if (LineFromFile[xyz] != ','){
                          dArray[aRow][aCol] += LineFromFile[xyz];
                         Serial.print(LineFromFile[xyz]);
                      }else{
                          Serial.print(" ");
                          aCol++;
                          Serial.print("ci ");
                        }
                       
                      }
                     
            // we reached end of the line, process what we found
            } else {
                   
                 Serial.println(" ");     

               
               aRow++;
                    aCol=0;
                   Serial.print("c0 ");
            }
    n = 0;
  }
   
   
   
int y;
int z;
Serial.println(" ");
Serial.println("Info:  ");
Serial.println(" ");
Serial.print("Row:  ");
Serial.println(row);
Serial.print("Column:  ");
Serial.println(col);
Serial.println(" ");

int col2 = col + 1;
for (y=0; y < row; y++){
for (z=0; z < col2; z++){
  Serial.print(dArray[y][z]);
  Serial.print(" ");
 
}
Serial.println(" ");
}
  }

void loop(){

}


Here is the file that I'm reading:
Code: [Select]
id,twoCommands,description
1,@02353,Play
2,@02354,Stop
3,@02355,Record
4,@023MT,ManTrack


Here is what the serial monitor reads out:

Code: [Select]
Card Ready
One Column Added
Columns:  2
Rows:  4
id ci twoCommands ci description
c0 1 ci @02353 ci Play
c0 2 ci @02354 ci Stop
c0 3 ci @02355 ci Record
c0 4 ci @023MT ci ManTrack
Info: 

Row:  4
Column:  2

id twoCommands description1 
description1 @02353 Play2 
Play2 @02354 Stop3 
Stop3 @02355 Record4


When the serial monitor reads the data being parsed to the array it appears as though the process is happening correctly.  I used the c0 and ci to ensure that the column ints were be changed at the appropriate time.  I think that here is something going on in the way that the array is being read back at the end, but I can't figure it out. 

Any help would be awesome!

Loren

PaulS

Code: [Select]
                o++;
Why is o a char type? Chars are for letters. Use int or byte or uint8_t for o's type.

Code: [Select]
  String dArray [row][col];
A 2D array of Strings will gobble memory and leak it all over the place. Don't do that.

Quote
When the serial monitor reads the data being parsed to the array

The Serial Monitor doesn't read the data being parsed. It displays data you send it.


Go Up