The "radares.txt" file contains latitude;longitude;radar_type
The idea is read line by line, and populate ever line in an array position (the array is called lineas[]).
Here is the code i'm running:
// open the file for reading:
myFile = SD.open("radares.txt", FILE_READ);
int index = 0;
int caracterInt;
char caracter;
String linea;
String lineas[50];
while (myFile.available()) {
caracterInt = myFile.read();
caracter = caracterInt;
if (caracter != '\n')
{
linea += caracter;
}
else
{
[b]lineas[index] = linea;
Serial.print("Indice: ");
Serial.print(index);
Serial.print(" - Valor: ");
Serial.print(linea);
Serial.print(" - Array: ");
Serial.println(lineas[index]);[/b]
index++;
linea = "";
}
}
// for (int i=0; i<=50; i++)
// {
// Serial.print(i);
// Serial.println(lineas[i]);
// }
if (myFile)
{
// close the file:
myFile.close();
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening radares");
}
When i do that, i see the array is not populating correctly. There are some missing radars that are not in the array. Why?
for (int i=0; i<=50; i++)
{
Serial.print(i);
Serial.println(lineas[i]);
}
Try use char arrays instead of Strings.
What are you trying to do exactly with those strings? Why do you need to store them in a giant array? Can't you process (extract values and do something with them) as soon as possible?
guix:
What are you trying to do exactly with those strings? Why do you need to store them in a giant array? Can't you process (extract values and do something with them) as soon as possible?
The idea is compare each GPS location with my actual GPS location provided by a GPS shield.
The list of pre-defined GPS locations is in a txt file inside SD card, and i want in the setup load each GPS location in an array, to compare in the loop my GPS location with each array register.
The next step is split the comma-separated string I'm getting each line, to load into the array that would be two-dimensional.
First step: Get rid of the String class. While a String object is easy to use, it's a crutch for programmers who don't want to spend the time learning the str*() and mem*() family of C function for char array processing. Check:
for details. You will save a considerable amount of memory by learning about these functions versus those in the String class.
Second, learn how to use the strtok() function. It is an extremely powerful function for breaking out substrings in char arrays that are separated by a set of delimiters (like a CSV file from Excel).
The idea is compare each GPS location with my actual GPS location provided by a GPS shield.
You realize, I hope, that getting an exact match, to 9 decimal places is extremely unlikely.
You need to convert the string information to degrees, minutes, and seconds, for latitude and longitude, for both the current location, and the list of locations, and see if you are close enough (for whatever definition of close suits you) to any of the locations.
Storing the degrees (int), minutes (int), and seconds (float) will take far less space than storing the strings.
Sorry, its my first arduino program, and i don't know very well C.
Can you do me a favor and write me how the code should be with char instead of string data type? Thanks a lot!
Can you do me a favor and write me how the code should be with char instead of string data type?
char record[80];
byte index;
void loop()
{
while(myFile.available() > 0)
{
char c = myFile.read();
if(c != '\n' && c != '\r')
{
record[index++] = c;
record[index] = '\0';
}
else
{
// End of record found
// Use the data in record
index = 0;
record[index] = '\0';
}
}
}
char record[80];
char records[100][80];
byte index = 0;
byte index2 = 0;
while(myFile.available() > 0)
{
char c = myFile.read();
if(c != '\n')
{
record[index] = c;
record[index+1] = '\0';
index++;
}
else
{
// End of record found
// Use the data in record
index = 0;
records[index2] = record;
index2++;
}
}
Now, the line
records[index2] = record;
is giving me an error. How can i assign all characters in the record to record array?
i've tried that, but i don't know if is ok
while(myFile.available() > 0)
{
char c = myFile.read();
if(c != '\n')
{
records[index][index2] = c;
records[index][index2 + 1] = '\0';
index2++;
}
else
{
// End of record found
// Use the data in record
index2 = 0;
index++;
}
}
jerearaujo03:
Sorry, its my first arduino program, and i don't know very well C.
Let me see if I understand this: The code Paul gave you "didn't work", but you're not going to tell us why. So, the guy with over 66 thousand posts and a bazillion karma points was wrong, but you, with all the experience gained in writing one program modified his code, but you're surprised that your modification didn't work. Do I have this right?
void loop()
{
while(myFile.available() > 0)
{
char c = myFile.read();
if(c != '\n' && c != '\r')
{
record[index++] = c;
record[index] = '\0';
}
else
{
// End of record found
// Use the data in record
index = 0;
record[index] = '\0';
}
}
}
I've re-tried this, and it works!
But i cant copy record to records.
I created a index2 variable to handle the first position of the two-dimensional array.
I tried copy the actual record to records in the index2 position, but i get a looping output
Read one record from SD and compare. If match, stop. Else read next record and compare. If match, stop. Else ... till end of file.
Now this can get slow if there are a lot of records and if the matching record in the file is near the end or there is no matching record at all. There are some tricks that can be applied; but that will be a later exercise.
This makes it quite clear why anonymous printing sucks. There is no way to tell what this mess means.
But, it seems that the values MIGHT be lat and lon. As shown, the textual representation of lat and lon takes 29 bytes. As two doubles, they would take 8 bytes. You can do the math to see what the reduction in storage space requirements.