Text based database

Hi

I'm trying to find a way of having a "database" like structure to read data from. At the moment I'm hard coding the data in the program and later I'll have it on an SD card.

The data is formatted as follows:

  1. Name of point Eg: Witfontein Road
  2. Latitude Eg: -33.951918
  3. Longitude Eg: 22.426744
  4. Direction of travel Eg 50

I've tried an array, but battling to get mixed data into the program. I've used the following formats:

 String point[1] = {"Witfontein Road", -33.951918, 22.426744, 50};

or as individual items:

  String point[1][1] = {"Camera 1"};
  Double point[1][2] = {-33.951918};
  Double point[1][3] = {22.426744};
  Int point[1][4] = {50};

All the above gives errors...

Any idea what is the proper way of having data in an Arduino program? I'll eventually have multiple points which I need to cycle through and see what is the closest one.

How about:

struct place {
  char nameOfPoint[25];
  float latitude;
  float longitude;
  int directionOfTRavel;
};

place places[50];

//To write :
places[3].nameOfPoint = "Camera 1";
places[3].latitude = -33.951918;
places[3].longitude = 22.426744;
places[3].directionOfTravel = 50;

Try to avoid Strings with Arduino... They just call for trouble.

Jacques

Thank you. That helps quite a bit.

I'm getting the following error:

incompatible types in assignment of 'const char [9]' to 'char [25]'

String names are not all going to be the same size. Do I need to make them all the same length? When I do make them the same length I get the following error for the same line:

invalid array assignment

Seems like string arrays are a problem.

Then have a char * instead of an array and dynamically alllocate the memory needed by a particular c-string if they are entered dynamically. always check that malloc() returned a pointer that is not NULL though

If the database is all static, then just build the pointers at structure declaration and initialisation time (with const char * as a type)

CSV formatted file?

J-M-L, thank you. I've tried char* but still get an error. Without a working example, I don't quite understand your answer and are not any wiser...

Johnny010, yes a CSV file would be perfect. I'm used to working with CSV files, but haven't used it in Arduino coding yet. I've tried to find working examples via Google, but...

Is your database static or dynamically changing?

Static pre-allocation of memory and assignment to a pointer is done like thisconst char * str = “hello”that assigns just the right number of bytes

If you have an array of struct, that works the same way the pointer to the statically allocated c-string will be assigned to an entry

struct data_t {
  const  char * message ;
  int x;
};

data_t data[] = {
   {“Hello”, 10},
   {“World”, 20}
};

For dynamic memory allocation search for malloc()