I'm EXTREMELY new to Arduino and fairly new to programming as a whole, but I thought it would be fun to try and write a program that draws arbitrary 1-bit bitmaps to an LED matrix using the Adafruit RGBMatrixPanel library. I'm trying to draw this image to the screen:
But it conks out after 16 pixels each row...
Below is my code in full. It seems like the bit mask only works for the first 16 columns, but I can't seem to figure out why it stops at that point. I'm using an Arduino Nano, if that helps.
#define CLK 8
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);
unsigned long int pixelGrid[] = {
0x00000000,
0x31CEFDCE,
0x78CCFCCC,
0x78CC84CC,
0x78CC04CC,
0x78CC04CC,
0x78CC24CC,
0x78783CFC,
0x783024FC,
0x303004CC,
0x303004CC,
0x303004CC,
0x003084CC,
0x3030FCCC,
0x3078FDCE,
0x00000000
};
unsigned long int bitMask = 0x00000001;
void setup() {
matrix.begin();
uint8_t r=0, g=0, b=0;
for(uint8_t y=0; y<16; y++) {
unsigned long data = pixelGrid[y]; // select row of bits from image
for (byte x = 0; x <32; x++) { //iterate through columns
if (data & (bitMask << x)) {// works!
r = 1; // set LED to ON
}
else {
r = 0; // LED is OFF
}
matrix.drawPixel(x, y, matrix.Color333(r, g, b));
}
}
}
void loop() {
}
Here's the part of the code that's probably messing up, isolated:
for (byte x = 0; x <32; x++) {
if (data & (1 << x)) {
r = 1;
}
else {
r = 0;
}
Thanks for your help!
Edit: Changing the data
variable from int
to long
to avoid it being truncated lead adds another column of pixels, but it kind of looks like they're all squished into column 16 for some reason:
Note: I tried drawing a rectangle from (0,0) to (31, 15) to make sure that all the LEDs worked, and I was able to do that.
Edit 2: Instead of the bitmask being 1
, I made it so that:
unsigned long int bitMask = 0x00000001;
// ...
if (data & (bitMask << x)) { // works!
r = 1; // set LED to ON
}
else {
r = 0; // LED is OFF
}
I'm guessing regular numbers default to 16-bit unsigned ints. The issue is fixed, woo! I edited the big code block to match.
matrix.drawPixel(x, y, matrix.Color333(r, g, b));
to see if the data is layed out in landscape and not in portrait.