Strange error

I can't understand this error. It simple scketch should print some strings, but, I can't make this work.

If you try this example it print everything in the memory (reasonable).

But, if you uncomment the if line (if (character == 0) endthis = 0;), it should print each line every 0.5 seconds. But, it print a infinite number of same character.

I not understand what is happens. Can you help me?

#include <avr/pgmspace.h>

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

PROGMEM prog_char *string_table[] = 	   // change "string_table" name to suit
{   
  string_0,
  string_1,
  string_2,
  string_3,
  string_4,
  string_5 
};

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

void loop()	  
{
  
  for (uint8_t i = 0; i < 6; i++)
  {
    char character;
    
    Serial.print("String ");
    Serial.print(i);
    Serial.print(": ");    
    
    prog_char* str = string_table[i];
    
//    while((c = pgm_read_byte(str++))) Serial.write(c);

    uint8_t endthis = 1;

    do {
      character = (char)pgm_read_byte(str);
      Serial.write(character);
      if (character == 0) endthis = 0;
      str++;
    } while(endthis == 1);
    
    Serial.println();
    
    delay( 500 );
    
  }
}

Replace this...

    prog_char* str = string_table[i];

...with this...

    prog_char* str = (prog_char*) pgm_read_word( &string_table[i] );

WOW, thanks.

It's work, but I still do not know why.

I don't find documentation of pgm_read_byte...

How can I learn about that? You know?

Thanks "Coding Badly"

Here you have documentation of progmem:

http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html

The pgm_read_* functions require a memory address as parameter, that's why the "&" character is needed. Hope this help :slight_smile:

Have you read through this...

This should help with the gritty details...
http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html

This from AVR Freaks is long but very detailed...
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38003&start=all&postdays=0&postorder=asc

Thanks, "Coding Badly" and "guix". I will read this articles.