Issue with PROGMEM and bitRead

I am trying to store bit arrays in flash memory and recall them to display patterns on an LED strip. The PatternLoop function works fine when the patterns are stored in SRAM, but when I try to use pgm_read_byte the data comes back random. Any help would be appreciated :slight_smile:

#include <Adafruit_NeoPixel.h>

#define PIN            3
#define NUMPIXELS      33

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);

byte wheelCount = 1;
int wheelRate = 1;
byte currentColor = 0;

const uint8_t Triangle[] PROGMEM = {
  8,
  5,
  B11111110,
  B01111100,
  B00111000,
  B00010000,
  B00000000
};
const uint8_t Strobe[] PROGMEM = {
  4,
  4,
  B11110000
};

const uint8_t Pattern1[] PROGMEM = {
  4,
  4,
  B10100100,
  B10100001
};

const uint8_t *pattern_table[] = {
  Triangle,
  Strobe,
  Pattern1,
};

void setup() {
  pixels.begin();
  pixels.show();
}

void loop() {
  PaternLoop((const uint8_t*)&(pattern_table[0][0]));
}

void PaternLoop(const uint8_t PROGMEM *pattern) {
  uint8_t width = pgm_read_byte(&pattern[0]);
  uint8_t height = pgm_read_byte(&pattern[1]);
  uint8_t *c;
  for (uint8_t frame = 0; frame < height; frame++) {
    uint8_t firstBit = frame * width;
    for (uint8_t led = 0; led < NUMPIXELS; led++) {
      uint8_t currentBit = firstBit + (led % width);
      uint8_t byteIndex = currentBit / 8;
      uint8_t bitIndex = 7 - (currentBit % 8);
      uint8_t byteBuffer;
      memcpy_P(&byteBuffer,(uint8_t*)pgm_read_byte(&pattern[byteIndex + 2]), 1);
      if (bitRead(byteBuffer, bitIndex)) {
        c = Wheel(wheelCount % 255);
        pixels.setPixelColor(led, pixels.Color(*c, *(c + 1), *(c + 2)));
        //PATTERN
        if (currentColor == 0)
          ColorShift();
      }
      else {
        pixels.setPixelColor(led, pixels.Color(0, 0, 0));
        //PATTERN
        if (currentColor == 1)
          ColorShift();
      }
      //PATTERN
      if (currentColor == 2)
        ColorShift();
    }
    //PATTERN
    if (currentColor == 3)
      ColorShift();

    pixels.show();
    delay(1);
  }
  //PATTERN
  if (currentColor == 4)
    ColorShift();
}

void ColorShift() {
  wheelCount = wheelCount + wheelRate;
  if (wheelCount > 255) {
    wheelCount = 1;
    wheelRate++;
  }
}

byte * Wheel(byte WheelPos) {
  static byte c[3];

  if (WheelPos < 85) {
    c[0] = WheelPos * 3;
    c[1] = 255 - WheelPos * 3;
    c[2] = 0;
  }
  else if (WheelPos < 170) {
    WheelPos -= 85;
    c[0] = 255 - WheelPos * 3;
    c[1] = 0;
    c[2] = WheelPos * 3;
  }
  else {
    WheelPos -= 170;
    c[0] = 0;
    c[1] = WheelPos * 3;
    c[2] = 255 - WheelPos * 3;
  }

  return c;
}