32x32 led panel, working code to improve

Status
Not open for further replies.

freto

Active member
I have been playing with a 32x32 led panel sold by Sparkfun or Adafruit and quite a few places in China. It seems that most people with Arduinos use the library made by Adafruit however it doesn't work with Teensy 3.0, see this thread.

I could not find a working library for the teensy 3.0 so I wrote a simple functional sketch for the panel, it displays the colors stored in a matrix (32x32 is a very nice size to display simple icon based animations). The code works but flickers a lot, I know this can be optimised by using a few tricks and more advanced coding but this is over my head. So I would like to share the code to improve it with the help of advanced coders in this forum.

Code:
#include "image.h"
#define RED0 5 // PWM pins for first 16 rows
#define GRE0 6
#define BLU0 9
#define RED1 23 // PWM pins for last 16 rows
#define GRE1 22
#define BLU1 21

#define CLK 13 
#define OE  10
#define LAT 11
#define A   17
#define B   16
#define C   15
#define D   14

boolean rowA, rowB, rowC, rowD = 0;

void setup() {                
    for (int i = 5; i <= 23; i++) 
        pinMode(i, OUTPUT);   

    for (int i = 5; i <= 23; i++) 
        digitalWrite(i, LOW);

    analogWriteResolution(8);
    analogWriteFrequency(23, 187500); 

}

void loop() {

    for (int r = 0; r < 16; r++) { 
        rowA = rowB = rowC = rowD = 0;
        if (r == 1 || r == 3 || r == 5 || r == 7 || r == 9 || r == 11 || r == 13 || r == 15) rowA = 1;
        if (r == 2 || r == 3 || r == 6 || r == 7 || r == 10 || r == 11 || r == 14 || r == 15) rowB = 1;
        if (r == 4 || r == 5 || r == 6 || r == 7 || r == 12 || r == 13 || r == 14 || r == 15) rowC = 1;
        if (r >7) rowD = 1;
        digitalWriteFast(A,rowA);
        digitalWriteFast(B,rowB);
        digitalWriteFast(C,rowC);
        digitalWriteFast(D,rowD);        
        drawMatrix(r);
    }
}

void drawMatrix(uint8_t row)
{
    digitalWriteFast(OE, HIGH);  // disable display 
    digitalWriteFast(LAT, LOW); // latch
    int r = row << 5; //row *32
    int r2 = (row+16) << 5; // (row+16) * 32
    for (int i = 0; i < 32; i++) {
        analogWrite(RED0, (mario[r+i] >> 16) & 0xFF); 
        analogWrite(GRE0, (mario[r+i] >> 8) & 0xFF); 
        analogWrite(BLU0, (mario[r+i]) & 0xFF); 
        analogWrite(RED1, (mario[r2+i] >> 16) & 0xFF); 
        analogWrite(BLU1, (mario[r2+i] >> 8) & 0xFF); 
        analogWrite(GRE1, (mario[r2+i]) & 0xFF);     
        digitalWriteFast(CLK, HIGH);     // toggle clock 
        digitalWriteFast(CLK, LOW);  
    }    
    digitalWriteFast(LAT, HIGH); // latch
    digitalWriteFast(OE, LOW); // enable display  
    delayMicroseconds(100); // delay needed to change light strength
}

/* 
 A B C D 
 0 0 0 0 row 1
 1 0 0 0 row 2
 0 1 0 0 row 3
 1 1 0 0 row 4
 0 0 1 0 row 5
 1 0 1 0 row 6
 0 1 1 0 row 7
 1 1 1 0 row 8
 0 0 0 1 row 9
 1 0 0 1 row 10
 0 1 0 1 row 11
 1 1 0 1 raw 12
 0 0 1 1 row 13
 1 0 1 1 row 14
 0 1 1 1 row 15
 1 1 1 1 row 16
 */


In the code above, "image.h" is just a matrix with 1024 values, formatted like this:
int mario[1024] {
0xFF0000,
0x00FF00,
0x0000FF,
0xFFFFFF,
0xFFFFFF,
0xFFFFFF, ......
};
 
Status
Not open for further replies.
Back
Top