SOLVED PROGMEM data retrieval returns garbage

I am trying to learn how to use program space and having problems with this simple test code.
It returns wrong value for variable puint and a different, but still wrong, value when Serial.print is commented out.

Am I using the pgm_read_byte_near macro incorrectly ?

Help would be appreciated.

Cheers Vaclav

#include <avr/pgmspace.h>

  uint8_t displaypuint = 0 ;
  prog_uint8_t puint PROGMEM  = 2; 
  displaypuint = (uint8_t)(pgm_read_byte_near(&puint));
  
  lcd.clear();
  lcd.print(displaypuint,BIN);
  lcd.setCursor(0,1);
  lcd.print("STOP");

  Serial.print(displaypuint);

Can you set up a test piece of code that compiles and not just a snippet?

http://snippets-r-us.com/

If it helps, yes,

#include <avr/pgmspace.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);


  uint8_t displaypuint = 0 ;
  prog_uint8_t puint PROGMEM  = 2; 
  displaypuint = (uint8_t)(pgm_read_byte_near(&puint));
  
//  lcd.clear();
//  lcd.print(displaypuint,BIN);
//  lcd.setCursor(0,1);
//  lcd.print("STOP");

  Serial.print(displaypuint);    // on my  run it prints  35 
  
}

void loop() {
  // put your main code here, to run repeatedly: 
  
}
void setup() {
...

  prog_uint8_t puint PROGMEM  = 2;

PROGMEM variables cannot be local variables. That was what your earlier posting did not show.

Move that PROGMEM line to global scope and it works OK.

While I was waiting for you I made a test:

byte displaypuint = 0 ;
const byte puint PROGMEM  = 42; 

void setup ()
{
  Serial.begin (115200);
  Serial.println ();

  displaypuint = pgm_read_byte(&puint);
  
  Serial.println(displaypuint);

}  // end of setup

void loop () { }

That correctly displays 42.

However I do think that puint is a bad name for a byte variable. The name implies it is an int.

Had you turned on “verbose compiling” you would have seen this give-away line:

sketch_oct05b.ino: In function ‘void setup()’:
sketch_oct05b.ino:10: warning: ‘__progmem__’ attribute ignored

Thanks, missed the warning! ( I am setting the COM baud rate to 75 !)

However, how was I suppose to know PROGMEM needs to be global? Been thru AVR doc and did not see that. Lesson learn. Thanks again.

As far as bad names goes - I started with p(rogram)u(nsigned )int and than thought I may have wrong type, so I kept changing it. I generally do not write code to be scrutinized by people who are just aching to pick it apart and not really answer the question, as it just happen on my another post. But sometime getting mad at folks like that wakes up the brain cells - so problem solved, indirectly thanks to the group "help". Cheers Vaclav Thanks again.

OK, in case you know the answer. Putting the program space variables as global apparently makes them "read only".

I am going to work on this tomorrow - my guess is somewhere in the macros is "const" modifier. Good nite Vaclav

Audio_Loopback_28.ino: In function 'void PROGMEMTest()': Audio_Loopback_28:2194: error: assignment of read-only variable 'var'

Vaclav: I generally do not write code to be scrutinized by people who are just aching to pick it apart ...

Just trying to encourage good habits, old chap. ;)

Vaclav: Putting the program space variables as global apparently makes them "read only".

Naturally they are read-only. They are in flash memory which only changes when you upload a new copy of the code.

I have stuff about PROGMEM here:

http://www.gammon.com.au/forum/?id=12615

I generally do not write code to be scrutinized by people who are just aching to pick it apart and not really answer the question

Does code get picked apart, yes, but I have not seen any one here who does it for thrill (well maybe one). Get a thicker skin, don't take it to heart, don't be paranoid, and you don't have to read a post if you don't want to. People here offer their skills freely and unselfishly, however they may not have the courteous gene. This is a great group. I learn some great here every day.

PROGMEM variables cannot be local variables

They can, if you qualify them with "static".

I would have written “cannot be auto variables” but I thought that might have confused newbies. However I agree with AWOL that you could conceivably make them static (and local).

However from: http://en.wikipedia.org/wiki/Automatic_variable

The term local variable is usually synonymous with automatic variable, since these are the same thing in many programming languages …

That’s why I said “local”. It’s usually synonymous, and it therefore is easier to explain.