Flex Pointer to different constant Array's

Hi,

I'm struggling with this...

  1. under my Variables.h tab I have declared 2 different Array's: this works fine

const PROGMEM byte Move_Get_Up[]={....} const PROGMEM byte Move_Hello_World[]={....}

  1. I have created subroutines that use these Array's with a pointer: this works fine

void Move_Init(const byte *data_Move){....}

  1. BUT In my main loop I want to call these subroutines WITH a variable (depending on the Array I want to use in my subroutine)

Move_Init(Move_Hello_World); => I want to have a variable that I can change.

I thought this would work, but does not: (meaning I cant change "SelectedArray" content in main loop) (expected primary expression....)

byte SelectedArray[] = "Move_Hello_World"; Move_Init(SelectedArray); SelectedArray[] = "Move_Get_Up"; Move_Init(SelectedArray);

Any suggestions?

Thank you

YO_BE: Any suggestions?

The first suggestion is that you post a complete code (in Code Tags) that demonstrates the problem.

The second suggestion is that variable definitions don't belong in a .h file. If that file were to be #included in more than one .cpp (or even .ino) file, it will result in multiple definitions of the same variables. See https://www.geeksforgeeks.org/difference-between-definition-and-declaration/

Your second example is using strings and has nothing to do with your const PROGMEM arrays. Is this what you were trying to do?

const PROGMEM byte Move_Get_Up[]={....}
const PROGMEM byte Move_Hello_World[]={....}

byte *ArrayPointer = Move_Hello_World;
Move_Init(ArrayPointer);
ArrayPointer = Move_Get_Up;
Move_Init(ArrayPointer);
...

Hi,

I can post the full code, but its already quite a long program, 95% not related to the question Perhaps I can isolate the issue and have a separate program to post.

Thanks for the suggestion on the .h file declaration. Will look into this. (thought it would make my program a bit more structured to have all variable declarations separate)

blh64, I will try your suggestion. Seems its what I'm looking for. I need to better understand pointers....

Hi,

Still is not working as I have a conflict between a constant and a variable. "Invalid conversion between const byte and byte"

The Array's must be constant as I cant afford loosing RAM space.

This is the simplified code:

bool Movement_Start;
byte time_Movement_Next, Movement_Step;

const PROGMEM byte Move_Hello_World[]={
  1,0, 100,100,100,100,100,100,155,100,1,1,80,
  3,0, 50,100,100,100,100,100,155,100,0,0,10,
  5,0, 100,100,100,100,100,100,155,100,0,0,50,
  6,0, 130,100,100,100,100,100,155,100,0,0,80,
  6,50, 100,100,100,100,100,100,155,100,0,0,10,
  0,0
};

const PROGMEM byte Move_Get_Up[]={
  1,0, 100,100,100,100,100,100,155,100,1,1,30,
  2,0, 100,10,10,100,10,10,155,100,0,0,30,
  3,0, 100,10,10,100,10,10,130,100,0,0,10,
  6,0, 100,10,10,100,10,10,130,100,0,0,10,
  6,50, 100,10,10,100,10,10,130,100,0,0,10,
};


void setup() {
  
  Movement_Start=HIGH;

}

void Move_Init(const byte *data_Move){
    time_Movement_Next=data_Move[Movement_Step];
    Movement_Start=LOW;
    Movement_Step=0;  // reset the step counter
    Serial.println("Jump to Init send any value to start");
    
   }

void loop() {
    byte *ArrayPointer = Move_Hello_World;
    Move_Init(ArrayPointer);
    ArrayPointer = Move_Get_Up;
    Move_Init(ArrayPointer);
 }

Why don't you use the arrays themselves? Why do you need to pass a pointer copy instead of the pointer itself?

Also, what is the program supposed to do? (looks useless rn - but I'm sure it's still in development)

This will fix the compiler error:

const byte *ArrayPointer = Move_Hello_World;

But, it still won’t work. You need to use the special functions to read from PROGMEM. See: avr-libc: <avr/pgmspace.h>: Program Space Utilities

Also, see: PROGMEM - Arduino Reference

Power_Broker:
Also, what is the program supposed to do? (looks useless rn - but I’m sure it’s still in development)

This is just a minor part of the code to show the issue.

Two minor changes needed. In your loop() you have to declare the pointer as 'const byte *' so you can store a 'const byte *' into it. In your Move_Init() you have to fetch each byte from PROGMEM. The compiler doesn't keep track of which addresses point to RAM and which point to PROGMEM so it is up to you to remember and fetch from the PROGMEM address space if the data is in PROGMEM.

void Move_Init(const byte *data_Move) {
  time_Movement_Next = pgm_read_byte(&data_Move[Movement_Step]); ///////////////
  Movement_Start = LOW;
  Movement_Step = 0; // reset the step counter
  Serial.println("Jump to Init send any value to start");
}


void loop() {
  const byte *ArrayPointer = Move_Hello_World;  ///////////////
  Move_Init(ArrayPointer);
  ArrayPointer = Move_Get_Up;
  Move_Init(ArrayPointer);
}

Hi team,

thanks for all the input. Based on first tests, this setup, see below, seems to work fine. Will test more. For PROGMEM related issues, certainly read this link: http://www.gammon.com.au/progmem

bool Movement_Start;
byte time_Movement_Next, Movement_Step;

const byte *ArrayPointer; 

const PROGMEM byte Move_Hello_World[]={
  1,0, 100,100,100,100,100,100,155,100,1,1,80,
  3,0, 50,100,100,100,100,100,155,100,0,0,10,
  5,0, 100,100,100,100,100,100,155,100,0,0,50,
  6,0, 130,100,100,100,100,100,155,100,0,0,80,
  6,50, 100,100,100,100,100,100,155,100,0,0,10,
  0,0
};

const PROGMEM byte Move_Get_Up[]={
  7,0, 11,100,100,100,100,100,155,100,1,1,30,
  2,0, 100,10,10,100,10,10,155,100,0,0,30,
  3,0, 100,10,10,100,10,10,130,100,0,0,10,
  6,0, 100,10,10,100,10,10,130,100,0,0,10,
  6,50, 100,10,10,100,10,10,130,100,0,0,10,
};


void setup() {
  
  Movement_Start=HIGH;

}

void Move_Init(const byte *data_Move){
    time_Movement_Next=data_Move[0];
    Serial.println(time_Movement_Next);
    time_Movement_Next=data_Move[2];
    Serial.println(time_Movement_Next);
    
   }

void loop() {
    delay(1000);
    ArrayPointer= Move_Hello_World;
    Move_Init(ArrayPointer);
    ArrayPointer = Move_Get_Up;
    Move_Init(ArrayPointer);
 }

Warning: Sometimes a sketch that uses PROGMEM arrays will give the expected results in a small sample because the compiler optimizes away all of the fetching from PROGMEM. When you expand the sketch to use more of the data, the compiler will stop substituting the constants and will begin fetching from RAM (it doesn't know any better) and you will have to add the functions to fetch from PROGMEM to get the expected results.

Thank you John for the heads up!!! I will secure your feedback!