0

Hello everyone i am new to the world of Arduino and i am having problems storing temporary values in Arduino RAM so i thought i would store them in flash storage using PROGMEM. i am trying to store 2 byte arrays but i cant get them to display after they are stored. After some testing i cant get Serial Print to work properly either but if in the mean time somebody can better explain PROGMEM that would be helpful. my code so far is:

#include <avr/pgmspace.h>
struct GRID{
  boolean isOn;
  uint16_t color;
  uint8_t X;
  uint8_t Y;
};

GRID landed[10][22];
GRID falling[22][10];

//PROGMEM prog_uint8_t xAxis[] = {1,2,3,4,5,6,7,8,9,10};
//PROGMEM prog_uint8_t yAxis[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22};

uint8_t xAxis[] = {1,2,3,4,5,6,7,8,9,10};
uint8_t yAxis[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22};

void iniLanded(){
  for(int i = 0; i < 10; i++)
    for(int j = 0; j < 22; j++){
      landed[i][j].color=0x0000;
      landed[i][j].X = xAxis[i];
      landed[i][j].Y = yAxis[j];
    }
}

void iniFalling(){
  for(int i = 0; i < 22; i++)
    for(int j = 0; j < 10; j++){
      falling[i][j].color=0x0000;
      falling[i][j].X = xAxis[j];
      falling[i][j].Y = yAxis[i];
    }
}

void printLanded(){
  for(int i = 0; i < 10; i++)
    for(int j = 0; j < 22; j++){
      Serial.print(landed[i][j].X); Serial.print(" "); Serial.println(landed[i][j].Y);
    }
}

void printFalling(){
  for(int i = 0; i < 22; i++)
    for(int j = 0; j < 10; j++){
      Serial.print(landed[i][j].X); Serial.print(" "); Serial.println(landed[i][j].Y);
    }
}

void setup(){
  Serial.begin(9600);
  iniLanded();
  iniFalling();

}

void loop(){
  Serial.println(F("Hello World!"));
  delay(100);
  /*printLanded();
  delay(100);
  printFalling();
  delay(100);*/
}
1
  • to those who are wondering why nothing is printing in serial it's because the Arduino runs out of RAM during the second initialize function. Commented Nov 30, 2013 at 16:23

1 Answer 1

1

The trick is in the way you call from flash. See my example this is where I define much like your commented code. and here is where you call back the data. Note the use of pgm_read_word_near and the deference &. Where there are other similar functions found here for byte, word, etc...

Sign up to request clarification or add additional context in comments.

3 Comments

i changed uint8_t xAxis to unsigned char xAxis[10] PROGMEM = {...}; and when i need to use it i use pgm_read_byte(&(xAxis[i])); is there a differnce between pgm_read_byte and pgm_read_word ?
pgm_read_word will read two bytes. Note you are using an array. Hence the pgm function needs to know or line up the size of the elements in the array.
ok i wouldn't need to use integers greater than 256 so i think read_byte is ok for me. on a separate note how does prog_void work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.