Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);*/
}
share|improve this question
    
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. –  blu Nov 30 '13 at 16:23

1 Answer 1

up vote 0 down vote accepted

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...

share|improve this answer
    
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 ? –  blu Nov 30 '13 at 16:01
    
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. –  mpflaga Nov 30 '13 at 16:06
    
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? –  blu Nov 30 '13 at 16:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.