byte PROGMEM list[100] .. strange results [solved]

Hello !
I’m new to Arduino and i’ve tried the Code attached.
The result was:

freeMemory()=1635
97

I was expecting the result of my array[0] which is 1 but not 97 ?

At my first attempt i was using double to store in Progmem. I’ve read about “Floating point numbers in program memory do not appear to be supported.” in the Arduino Reference. So i made this simple test with byte. But obviously there is a lack of understanding. When i exchange the line "byte PROGMEM list[100] " with “prog_uchar PROGMEM list[100]” it doesn’t change the result.
I’d be happy to get any hint on this issue.

Arduino IDE 1.0.5-r2 / ATmega 368

#include <MemoryFree.h>
#include <avr/pgmspace.h>

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

void loop ()
{
  delay(2000);
  
  Serial.print(F("freeMemory()="));
  Serial.println(freeMemory());
  
  byte PROGMEM list[100] = {
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0
                      };
  Serial.println(pgm_read_byte (&list[0]));
}

Hi, welcome to the forum.

I would like to check it, but which "MemoryFree.h" are you using ?

You can try these: prog_uchar list[100] PROGMEM = ... prog_uchar PROGMEM list[100] = ... const byte list[100] PROGMEM = ...

That has been changed a little for the new Arduino 1.5.7 BETA. If you like to try that, the 'prog_uchar' has been discarded, and 'const byte' should be used.

I don't think you can use PROGMEM as an automatic variable - one defined inside a function. Move it to the global data area outside of setup().

#include <MemoryFree.h>
#include <avr/pgmspace.h>

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

void loop ()
{
  delay(2000);
  
  Serial.print(F("freeMemory()="));
  Serial.println(freeMemory());
  
  static byte PROGMEM list[100] = {
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0
                      };
  Serial.println(pgm_read_byte (&list[0]));
}

Or do that. 8^)

Thank you all ! Moving the array definition outside the function made it work :) Also the "static byte PROGMEM" worked as well.

Test with Arduino 1.5.7 BETA, this version shows the sketch (code in flash) size and the global variables use (in ram). This is my test sketch:

const byte PROGMEM list[100] = {
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0,
                        1,2,3,4,5,6,7,8,9,0
                      };
                      
void setup(void) {
    Serial.begin(9600);
}

void loop ()
{
  Serial.println( pgm_read_byte (&(list[0])));
}

Sketch uses 2222 bytes, Global variables use 188 bytes. Without the 'const' and without 'PROGMEM' : Sketch uses 2222 bytes, Global variables use 288 bytes. Instead of 'byte', now 'uint32_t' : Sketch uses 2522 bytes, Global variables use 188 bytes. That is perfect, 100 times 4 bytes is 400 bytes, and it was 100 bytes before. 300 byte increase. Now for the real test with : const float PROGMEM list[100] = { Sketch uses 2522 bytes, Global variables use 188 bytes. So even for floating point there is no problem.

It all makes perfect sense.