Hello. I have a question, i need to use malloc in a arduino project to allocate memory for a specific struct, i understand the basic of the malloc command but i dont see how to integrate it in my project, here is the code and the file that i need to open.
The code bassicaly reads a set of locations from a file called local and saves the coordinates and the name of the places in a struct.
#include <SD.h>
#include <SPI.h>
File printFile;
String Linha, Lat, Long, Name, Aux = "";
boolean SDfound;
int x, count=0, nloc=0, aux=0, meme=0;
typedef struct local_s{
char nome[30];
float lat;
float lon;
} local;
local preenche(){
local l;
l.lat=Lat.toFloat();
l.lon=Long.toFloat();
Name.toCharArray(l.nome, 30);
return l;
}
void setup() {
Serial.begin(9600);
if (!(SD.begin (4))) {
if (!SD.begin(53)) {
Serial.print("The SD card cannot be found");
while(1);
}
}
printFile = SD.open("local.txt");
if (!printFile) {
Serial.print("The text file cannot be opened");
while(1);
}
while (printFile.available()) {
Linha = printFile.readStringUntil('\n');
nloc++;
}
printFile.close();
local tab[nloc];
printFile = SD.open("local.txt");
if (!printFile) {
Serial.print("The text file cannot be opened");
while(1);
}
while (printFile.available()) {
Linha = printFile.readStringUntil('\n');
for (x=0; x<Linha.length();x++){
if (Linha[x]!=' ' || count==2){
Aux += Linha[x];
}
if (Linha[x]==' '){
if (count==1){
Lat=Aux;
count=2;
Aux = "";
}
if (count==0){
Long=Aux;
count=1;
Aux = "";
}
}
if (x==(Linha.length()-1)){
Name=Aux;
count=0;
Aux = "";
}
}
tab[aux]=preenche();
aux++;
}
printFile.close();
Serial.println();
Serial.print("Tem um total de: ");
Serial.print(nloc);
Serial.print(" locais.");
Serial.println();
}
void loop() {
while (meme==0){
local tab[nloc];
for (x=0; x<nloc; x++){
Serial.println(tab[x].nome);
Serial.println(tab[x].lat);
Serial.println(tab[x].lon);
}
meme=1;
}
}
local.txt (171 Bytes)