Read text from structure data stored in PROGMEM


I have defined the following data structure

typedef struct Star// this structure is defined before  the array named StarsList
  char *StarName;// name of the star "Arcturus"
  int32_t RA;// Right ascension 513397 
  int32_t DECL;// Declination 690570

and in program i have the table

// the table i want to store in progmem contains the star name, and star coordinates
const PROGMEM Star StarsList[] = {
{"Sirius", 243089, -601780},
{"Canopus", 230371, -1897050},
{"Arcturus", 513397, 690570},//used in the example above
{"RigilKentaurus", 527759, -2190070},
{"Vega", 670163, 1396210}

to read the data from the table i use

function text of the lcd needs the string and the coordinates on the display
//for example will display CrtStar at x=10, y=70
 Star CrtStar = StarsList[3];// define variable CrtStar that will have the star name and coordinates from table StarList
  text("CrtStar:", 10, 70);
  text(CrtStar.StarName, 60, 70);//doesn't display anything
  //code below works
  text("RA:", 135, 70);
  text((long)CrtStar.RA, 155, 70);
  text("DEC:", 205, 70);
  text((long)CrtStar.DECL, 230, 70);

and the function for displaying strings on lcd is below (from lcd library

text(char *textString, uint16_t xLoc, uint8_t yLoc);

How can i get the string from the table and display it on the lcd? (i don't know how to copy/transfer "Arcturus" to CrtStar.StarName


please post your whole code as this part only makes little sense to me.

Star CrtStar = StarsList[3];

StarsList is stored in the program memory (as specified by the PROGMEM keyword). Because this chip uses a Harvard architecture, you can't just read it directly like a with a normal array (stored in the RAM), you have to use special functions to read it, but I am not sure how you do that with objects such as structs.

hi robtillaart, i've put some comments to the code. I can't upload the code because it is huge (65k)

The idea is to get the string from the structure table stored in progmem and pass it to the lcd function text I've also look at strcpy_P here (bottom of the page)

Hope my comments will help to understand my problem

I can't upload the code because it is huge (65k)

can you write a stripped down version of your sketch that shows the problem.

While the structure array StarsList[] is in PROGMEM, it has a member, StarName, that will be pointing to a non-progmem string.


I know one way to make the string actually go into PROGMEM, but it's likely a space-waster:

struct Star// this structure is defined before  the array named StarsList
  char StarName[32];   // room for the longest star name
  int32_t RA;              // Right ascension 513397 
  int32_t DECL;           // Declination 690570

This way the string is right there in the struct and is in progmem.

The pgmspace manual gives a clearer picture what's going on than the Arduino docs.

As for accessing the struct, you kinda have two choices, copy the whole array element to a conventional RAM struct, and access it normally:

struct Star CrtStar;
memcpy_P(&CrtStar, &StarsList[3], sizeof(struct Star));

Or access each of the members via pgm_read_dword() and strcpy_P() and such.

Thanks for reply I eliminate the structure from my code and i only use arrays now. I took the way described here