I am trying to get the INT value stored in the program memory through pgm_read_word. But mySerial.printf() shows 0 instead 85.
What is wrong with my code?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(1, 0);
PROGMEM const int num = 85;
const int *pointer;
void setup() {
mySerial.begin(9600);
pointer = #
// put your setup code here, to run once:
int *x = (int*)pgm_read_word(&pointer);
mySerial.println(*x);
}
You're reading FROM a pointer, not TO a pointer. (unless you want the word from address 85 of RAM.)
and "pointer" is already a pointer, so you don't need the "&" in front of it.
So that should just be:
int x = pgm_read_word(pointer);
// Or:
int x = pgm_read_word(&num);
To your question: I need this INT pointer to point to a variable that constantly changes. This code that I have published is a reduced version of a larger one. In the larger one, I have a menu with menu items. Every menu item has a "name" and a "quantity".
Then I combine this values to show texts like "5 Notifications", where name="Notifications" and quantity="5".
Quantity is the pointer who points to notificationsCount.
This is the code with your modifications:
typedef struct MenuItem {
PROGMEM const char *name;
PROGMEM const int *quantity;
} MenuItem;
// Menu Item values
PROGMEM const char name[] = "NAME";
PROGMEM const int num = 69;
// Initialize menuitems
PROGMEM const MenuItem mi = {name,&num};
const MenuItem *root;
void setup() {
mySerial.begin(9600);
root = &mi;
// Get the name
char buf[50];
strcpy_P(buf, (char *)pgm_read_word(&root->name));
// Get the quantity
int q = pgm_read_word(&root->quantity);
mySerial.println(buf); // Print: ö. It's incorrect!
mySerial.println(q); // Print 256. It's incorrect!
}