Passing ProgMem array to function

I've got a sketch the works with an LED sign.

It has a large char array with text that looks like this

prog_uchar signMessage[] PROGMEM = { " some text is here"};

I'm trying to make several of these arrays and pass them to a

playMessage function that processes the texts and spits it out to the sign.

When I try this:

void loop() {
  playMessage(signMessage);
}


void playMessage(prog_uchar* message){
  
maxStringSize =  sizeof(message);  // find message size

I get this

error: variable or field 'playMessage' declared void In function 'void loop()': In function 'void playMessage(prog_uchar*)':

I know there are some other ways to do this - any nudge in the right direction would be appreciated.

Paul

I'm not sure what the correct way of doing it is, but I'm pretty sure that sizeof won't work. Even if it did compile, won't it just return the size of the pointer?

You might need to write some string length function yourself - i.e. loop through the string until you hit null. There might be some build in functions for this. I'm just new to the arduino so I don't know everything that's available.

I'm not so interested in the sizeof function - I did that outside the function and passed it in. I'm more interested in getting the base address of the array in - it seems like it should be so easy!

I got it all working in a slightly inelegant way by just using global variables and assigning them outside the function.

Thanks for your reply taiyac

PB

void playMessage(prog_uchar* message){
  
maxStringSize =  sizeof(message);  // find message size
}

void loop() {
  playMessage(signMessage);
}

There fixed. :slight_smile:
The function has to come before you call it. Alternatively you can use a header file.

I'll try this and report results - but I distinctly do not have to list other functions before calling them - and commonly have "loop" at the top and a half dozen functions below that all work fine.

I suspect it has something to do with progmem because if I put the phrase in ram instead of flash, it works fine.

Paul

It might be some strange interaction with the automatic-generation of function prototypes by the Arduino environment. Maybe it doesn't generate them correctly in the prog_mem case? I'm not sure why that would be though.

Brian Riley hammered on it for a while and came to the conclusion it was somewhere in the Arduino environment.

We didn't discuss it in any detail though. I'm going to see if rearranging things helps. I kind of remember that happening (at random) in other C environments and always seemed like voodoo to me.

P

If you have the function defined above the place where you call it, you don't need a prototype. I'm not sure why the automatic prototype generation is failing, though.