Error in simple code using PROGEMEM

Hello everyone,

I'm still new to arduino, and today I tried to use Flash Memory to save integer values in an array but

somehow there is an error I could not find and solve

Here is the code :

#include <avr/pgmspace.h>

const int string0[1] PROGMEM ={1};

const int string1[1]PROGMEM={2};

const int string2[1]PROGMEM={3};

const int string3[1]PROGMEM={4};

const int string4[1]PROGMEM={5};

const int string5[1]PROGMEM={6};

const int* const string_table[] PROGMEM={string0,string1,string2,string3,string4,string5};

int buffer[30];

void setup(){

  Serial.begin(9600);
  
}

void loop(){
  for(int i=0;i<6;i++){
    
  
  strcpy_P(buffer,(int*)pgm_read_word(&(string_table[i])));
  Serial.println(buffer);
  delay(500);
  }
}

I appreciate you help.

Thanks.

AbdullahWalidy:
I appreciate you help.

What type of data constants do you want to store in PROGMEM?
Is it 'just some int values or do you want literal strings in PROGMEM?

Here's the error message that I get with IDE 1.8.2:

cannot convert 'int*' to 'char*' for argument '1' to 'char* strcpy_P(char*, const char*)'

Looking at how strcpy() is used in the program:

strcpy_P(buffer,(int*)pgm_read_word(&(string_table[i])));

The first argument is buffer. It's declared like this:

const int* const string_table[] PROGMEM={string0,string1,string2,string3,string4,string5};

as int*. The error message says that it can't convert a pointer to int into a pointer to char in the first argument to strcpy(). That suggests that buffer should be declared as char* in order to use it in strcpy.

Changing the decaration of buffer won't fix everything, but it's a start. You might be able to take it from there.

You'll also want to look at the way you declare the stringX[] array constants. Keep in mind that a C string is terminated by a zero byte. Where would that byte be stored in these array constants?

/Users/john/Documents/Arduino/sketch_may09a/sketch_may09a.ino: In function 'void loop()':
sketch_may09a:25: error: cannot convert 'int*' to 'char*' for argument '1' to 'char* strcpy_P(char*, const char*)'
     strcpy_P(buffer, (int*)pgm_read_word(&(string_table[i])));

This error means you are trying to use the address of an int array as if it were a character pointer. You seem to be very confused.

sketch_may09a:26: error: call of overloaded 'println(int [30])' is ambiguous
     Serial.println(buffer);
                          ^

This error means that you can't pass an array of 30 integers to .print() or .println(*). What did you want to do with those integers? Print the values of all 30 integers on separate lines???

If you want to put integers in PROGMEM you would do it more like:

const int integer_table[] PROGMEM = {1, 2, 3, 4, 5, 6};

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < sizeof integer_table / sizeof integer_table[0]; i++) {
    Serial.println(pgm_read_word(&integer_table[i]));
    delay(500);
  }
}

tmd3:
Here's the error message that I get with IDE 1.8.2:

cannot convert 'int*' to 'char*' for argument '1' to 'char* strcpy_P(char*, const char*)'

Looking at how strcpy() is used in the program:

strcpy_P(buffer,(int*)pgm_read_word(&(string_table[i])));

The first argument is buffer. It's declared like this:
const int* const string_table[] PROGMEM={string0,string1,string2,string3,string4,string5};

as int*. The error message says that it can't convert a pointer to int into a pointer to char in the first argument to strcpy(). That suggests that buffer should be declared as char* in order to use it in strcpy.
Changing the decaration of buffer won't fix everything, but it's a start. You might be able to take it from there.
You'll also want to look at the way you declare the stringX[] array constants. Keep in mind that a C string is terminated by a zero byte. Where would that byte be stored in these array constants?

It seems that I'm still confused with the pointer :frowning:
I tried to do as in this example but instead of printing char I wanted to print integers but didnt work.

const char string_0[] PROGMEM = "String 0";   // "String 0" etc are strings to store - change to suit.
const char string_1[] PROGMEM = "String 1";
const char string_2[] PROGMEM = "String 2";
const char string_3[] PROGMEM = "String 3";
const char string_4[] PROGMEM = "String 4";
const char string_5[] PROGMEM = "String 5";


// Then set up a table to refer to your strings.

const char* const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5};

char buffer[30];    // make sure this is large enough for the largest string it must hold

void setup()
{
  Serial.begin(9600);
  while(!Serial);
  Serial.println("OK");
}


void loop()
{
  /* Using the string table in program memory requires the use of special functions to retrieve the data.
     The strcpy_P function copies a string from program space to a string in RAM ("buffer").
     Make sure your receiving string in RAM  is large enough to hold whatever
     you are retrieving from program space. */


  for (int i = 0; i < 6; i++)
  {
    strcpy_P(buffer, (char*)pgm_read_word(&(string_table[i]))); // Necessary casts and dereferencing, just copy.
    Serial.println(buffer);
    delay( 500 );
  }
}
[code]

[/code]

I declared buffer as char but still have no clue what to do next ^^'

This error means that you can't pass an array of 30 integers to .print() or .println(*). What did you want to do with those integers? Print the values of all 30 integers on separate lines???

Yes, That is what I wanted to do but using the method in this example

nst char string_0[] PROGMEM = "String 0"; // "String 0" etc are strings to store - change to suit.
const char string_1[] PROGMEM = "String 1";
const char string_2[] PROGMEM = "String 2";
const char string_3[] PROGMEM = "String 3";
const char string_4[] PROGMEM = "String 4";
const char string_5[] PROGMEM = "String 5";

// Then set up a table to refer to your strings.

const char* const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5};

char buffer[30]; // make sure this is large enough for the largest string it must hold

void setup()
{
Serial.begin(9600);
while(!Serial);
Serial.println("OK");
}

void loop()
{
/* Using the string table in program memory requires the use of special functions to retrieve the data.
The strcpy_P function copies a string from program space to a string in RAM ("buffer").
Make sure your receiving string in RAM is large enough to hold whatever
you are retrieving from program space. */

for (int i = 0; i < 6; i++)
{
strcpy_P(buffer, (char*)pgm_read_word(&(string_table*))); // Necessary casts and dereferencing, just copy.*

  • Serial.println(buffer);*
  • delay( 500 );*
  • }*
    }[/quote]