Reading CSV file from SD card_Edited


hi arduino community,

i have repost after realized previous post not following 'standard' here. apologies.


at current stage of project, i'm trying read csv file sd card. know topic has been posted many times, , there examples can find online, or here. believe me, have gone through tutorials/examples, , tried use programs. apparently, have managed data printed on serial monitor, readings somehow inaccurate (some corrects, not). csv file contains series of longitude , latitude data of route. here example of data:

column 1, column 2
(longitude, latitude)
4530.3979, 909.8869
4530.3981, 909.8866
4530.3982, 909.8866
4530.3982, 909.8868

so complete code i'm using.


code: [select]


#include <spi.h>
#include <sd.h>

file myfile;

void setup()
{
  // open serial communications , wait port open:
  serial.begin(9600);
  while (!serial) {
    ; // wait serial port connect. needed leonardo only
  }


  serial.print("initializing sd card...");

  if (!sd.begin(4)) {
    serial.println("initialization failed!");
    return;
  }
  serial.println("initialization done.");

  // re-open file reading:
  myfile = sd.open("datalog3.csv");
  if (myfile) {
    serial.println("test.txt:");

    // read file until there's nothing else in it:
    while (myfile.available()) {
      string a="";
       
       float wval;
      for(int i=0;i<99;++i)
      {
     
       char temp=myfile.read();
       // serial.println(temp);
       // serial.print("ciclo  ");
       // serial.println(i);
        if(temp!=','&&temp!='\n')
      { //a=temp;
        a+=temp;}
      else if(temp==','||temp=='\n'){
         serial.println(a);
        char buf[a.length()];
        a.tochararray(buf,a.length());
         wval=atof(buf);
 
      serial.println(wval,4);
     
      break;}
     
        }
     
    }
    // close file:
    myfile.close();
  } else {
    // if file didn't open, print error:
    serial.println("error opening test.txt");
  }
}

void loop()
{
  // nothing happens after setup
}


and these readings got;

4530.3979
4530.3969
909.8869
909.8869
4530.3981
4530.3979
909.8866
909.8866
4530.3982
4530.3979
909.8866
909.8866
4530.3982
4530.3979
909.8868
909.8868

i'm not sure why gives different reading file. need program read data correctly columns , following sequence, can proceed next stage. appreciate if can me out this. thank you.

you know maximum length of record. ditch stupid string class. use char array hold data.

i not understand why puzzled differences. go read documentation on float arduino. see reasonable.


Arduino Forum > Using Arduino > Programming Questions > Reading CSV file from SD card_Edited


arduino

Comments