Hello,
I don't understand why I can't display the arrays with a loop, but only line by line:
#include <avr/pgmspace.h>
const PROGMEM char t1[]="One";
const PROGMEM char t2[]="Two";
const PROGMEM char t3[]="Three";
const PROGMEM char *const tab1[]={t1,t2,t3};
const PROGMEM char t4[]="Four";
const PROGMEM char t5[]="Five";
const PROGMEM char t6[]="Six";
const PROGMEM char *const tab2[]={t4,t5,t6};
const PROGMEM char **const tab[]={tab1,tab2};
char str_buffer[50];
void setup(){
Serial.begin(9600);
}
If I use this loop, it's doesn't work, it only prints strange characters in the monitor:
void loop(){
for(byte j=0;j<2;j++)
for(byte i=0;i<3;i++) Serial.println(strcpy_P(str_buffer,(char *)pgm_read_word(&(tab[j][i]))));
}
But if I replace the "for" by it's two values 0 then 1, it works, it prints "one, two, three, four, five, six"
void loop(){
for(byte i=0;i<3;i++) Serial.println(strcpy_P(str_buffer,(char *)pgm_read_word(&(tab[0][i]))));
for(byte i=0;i<3;i++) Serial.println(strcpy_P(str_buffer,(char *)pgm_read_word(&(tab[1][i]))));
}
Why so complicated?
There is no need for two indirections and no need to copy the data to RAM to print it.
(Note the different baudrate )
const PROGMEM char t1[] = "One";
const PROGMEM char t2[] = "Two";
const PROGMEM char t3[] = "Three";
const PROGMEM char t4[] = "Four";
const PROGMEM char t5[] = "Five";
const PROGMEM char t6[] = "Six";
const PROGMEM char *const tab[2][3] = {{t1, t2, t3}, {t4, t5, t6}};
void setup() {
Serial.begin(250000);
for (byte j = 0; j < 2; j++) {
for (byte i = 0; i < 3; i++) {
Serial.println((__FlashStringHelper *)pgm_read_word(&tab[j][i]));
}
}
}
void loop() {}
One
Two
Three
Four
Five
Six
If you prefer the original double indirection (thereby wasting no space), here you go:
const PROGMEM char t1[] = "One";
const PROGMEM char t2[] = "Two";
const PROGMEM char t3[] = "Three";
const PROGMEM char t4[] = "Four";
const PROGMEM char t5[] = "Five";
const PROGMEM char t6[] = "Six";
const PROGMEM char *const tab1[] = {t1, t2, t3};
const PROGMEM char *const tab2[] = {t4, t5, t6};
const PROGMEM char * const * const tab[] = {tab1, tab2};
void setup() {
Serial.begin(250000);
for (byte j = 0; j < 2; j++) {
for (byte i = 0; i < 3; i++) {
char** table = (char **)pgm_read_word(&tab[j]);
Serial.println((__FlashStringHelper *)pgm_read_word(&table[i]));
}
}
}
void loop() {}
One
Two
Three
Four
Five
Six