Go Down

Topic: Reading a SD card to parse floats to an float array (Read 152 times) previous topic - next topic

mdbirley


Hi
I am working on an autonomous robot and want to record waypoints on an SD card and then to retrieve them into variable to drive the mission. I have most of the code sorted however I am struggling to read my SD card to be able to parse the lat and long floats that are stored on the card.

I have enclosed the waypoints that I have recorded onto the sd card. way4.txt and the code that I have been struggling with. This only represents a small amount of the overall code. When the problem is solved the aim is to integrate this code into the main program.

I am hoping to read the data on the SD card into 2 variables. An array of lats and an array of longs both to 7 decimal places.

The code opens the sd card and reads the data into a variable called aChar
aChar is then added to fileContents
I have tried to then split fileContents into the 2 arrays.

If you can give some guidance it would be appreciated as I have been on this for days

regards
Max



Code: [Select]

#include <SPI.h>
#include <SD.h>
char fileContents[128];
String data;
String latitude[3];
String longitude[3];
int index;
int counter = 0;

void setup()
{
 
 Serial.begin(9600); 
 // Serial.print("Initializing card."); 
  pinMode(53, OUTPUT);
 
  if (!SD.begin(53))
  {
   // Serial.println("Card failed to initialize or no card available"); 
    return;
  }
  //Serial.println("Card initialized!");

 File myfile = SD.open("way4.txt");

 /*
  if (myfile) {
   
    while (myfile.available()) {
    Serial.write(myfile.read());
    }
    // close the file:
    myfile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening way3.txt");
  }

*/
  // Open the Way Point file for reading:
 myfile = SD.open("way4.txt");

  if (myfile)  //*********************************************************
    {
   
     // Probably can be smaller
    byte index = 0;
   
   while (myfile.available()) //++++++++++++++++++++++++++++++++++++++++++
      {
      char aChar = myfile.read();
     
      if(aChar != '\n' && aChar != '\r')
        {
         fileContents[index++] = aChar;
         fileContents[index] = '\0'; // NULL terminate the array
             
        }
      else                          // the character is CR or LF
         {
          //Serial.print(fileContents);
          //Parsing part of code
           data = fileContents;
          latitude[1] = (data.substring(0,10));
          longitude[1] = (data.substring(11,22));
          //Serial.print(latitude[1]);
         // Serial.print(" +");
          Serial.print(longitude[1]);

               
         fileContents[0] = '\0';
         index = 0;
          }
    } //End while (myfile available) +++++++++++++++++++++++++++++++++++++++++
   
    // close the file:
      myfile.close();
   
    } //End if (myfile) *******************************************************
  else
      {
    // if the file didn't open, print an error:
    Serial.println("error opening way3.txt");
      } //End else


           

     
} //End Set up

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



sterretje

Read Robin's updated Serial Input Basics thread; the principles also apply to reading files. It even contains a parse example and a conversion to integer (change atoi to atof for floats).

I suggest that you make use of an array of structs.

And get rid of String (capital S).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up