Skip to content

PROGMEM

JaredTheWolf edited this page May 25, 2020 · 2 revisions

If you are using a microcontroller with a limited amount of RAM, such as the Arduino Nano, you will very quickly run out of space when creating LED arrays.

The solution is to store these values in ROM and pull them into RAM as you need them. This is PROGMEM's job. When getting a value that has been given the PROGMEM modifier, you must use one of the provided macros to access the value. It's not always clear which macro you should use, as it's a bit abstract (as is all memory mapping), but for the scope of this program pgm_read_word(&array[i]) is all you need.

In OSPC's code, all PGM code is currently handled by drawPattern().

// Purpose: Draws LEDs by running through the pixels, one by one, and seeing if they should be lit up according to their array
// Accepts: uint16_t[] of your desired pattern, int size of the pattern array, long int hex code
// Returns: nothing
void drawPattern(uint16_t patternArray[], int patternSize, long int colour) {
  // PGM comes into play here. For more info about PGM, see the declarations header or the wiki
  
  uint16_t nextPixel; // holds current RAM value from PGM
  int pixelCounter = 0; // increments when a pixel is placed
  for (int i = 0; i < NUM_LEDS; i++) { // loop through LED in the matrix
    for (int j = 0; j < patternSize; j++) { // check every element in the pattern array for the current LED
      nextPixel = pgm_read_word(&patternArray[pixelCounter]); // pull element into RAM
      if (i == nextPixel) { // if the pixel in the matrix matches the array element
        leds[i] = colour; // turn the pixel the specified colour
        pixelCounter++; // move to the next pixel
      }
    }
  }
}

If you are using a model with an abundance of RAM and don't want to deal with memory abstraction, you can probably ignore this little keyword. However, you'll need to make sure you are using the more basic version of this method:

void drawPattern(int patternArray[], int patternSize, long int colour){
    for (int i = 0; i < NUM_LEDS; i++){ // run through the entire array, pixel by pixel
    for (int j = 0; j < patternSize; j++){ // run through the array
      if (i == patternArray[j]){ // if the index of this pixel is found in the array
        leds[i] = colour; // turn on this LED with the colour specified
      }
    }
  }
}

For more info on PROGMEM, see https://www.arduino.cc/reference/en/language/variables/utilities/progmem/ and https://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html

Clone this wiki locally