Hello. I am relatively new to Arduino code and I am having a problem reading an array from FLASH. I wrote a simple program to test it out and I am stuck. Any suggestions would be appreciated. Here is the code:
unsigned char i = 0; // Counter
PROGMEM const unsigned int mysine[] = { 0, 100, 3, 5, 101};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
I used this same declaration in a demo program for a 128x64 OLED and it worked fine. So I thought I would use it in my own program. Obviously I have missed something.
That only prepares the library for use. You didn't use any of the functions in it that you need. The documentation does explain it, please read more of it.
Thanks for your help. I guess I really did not understand how to use FLASH storage. It is working now. Here is the new code: #include<avr/pgmspace.h> unsigned char i = 0; // Counter PROGMEM const uint16_t mysine[] = { 0, 100, 3, 5, 101};
void setup() {
// put your setup code here, to run once:* Serial.begin(9600);
@olearyds, can you please spend some time reading How to get the best out of this forum and in future apply code tags to code that you post so we don't have to look at things like mysine[] but at mysine[]. It also makes it easier to read (and copy if necessary).
@PieterP Sorry, I must have misunderstood your comment. I have updated my code as follows:
#include<avr/pgmspace.h>
unsigned char i = 0; // Counter
PROGMEM const uint16_t mysine[] = { 0, 100, 3, 5, 101};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.print(pgm_read_word(&mysine[0])); Serial.print("\n");
Serial.print(pgm_read_word(&mysine[1])); Serial.print("\n");
Serial.print(pgm_read_word(&mysine[2])); Serial.print("\n");
Serial.print(pgm_read_word(&mysine[3])); Serial.print("\n");
Serial.print(pgm_read_word(&mysine[4])); Serial.print("\n\n");
for (i = 0; i < 5; i++) {
Serial.print(i); Serial.print(": "); Serial.print(pgm_read_word_near(mysine + i)); Serial.print("\n");
}
}
void loop() {}
With the following results:
0
100
3
5
101
0: 0
1: 100
2: 3
3: 5
4: 101
Even though it is the same as before I see your point. I don't want to be lucky with code, but rather do it right so it works all the time. Thank you for your help.