Issue with programming adafruit 32x32 matrix

Hey guys, I have been working with my 32x32 led matrix from adafruit recently and I have the demo code running just fine, but when I try to run my own code it doesn't do anything near expected. I am just trying to get an idea of controlling the LED's right now, and when I try to turn one on, it turns on entire rows in random colors.

Here is the code I have so far:

#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
#include <avr/pgmspace.h> 


// If your matrix has the DOUBLE HEADER input, use:
#define CLK 11  // MUST be on PORTB!
#define LAT 9
#define OE  10
#define A   A0
#define B   A1
#define C   A2
#define D   A3
//variables 
#define ALIVE 1
#define DEAD 0
#define MAX_ROWS 16
#define MAX_COLS 16

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

int cells[16][16] = {0};
int16_t color = 0; //holds current cell color value

void setup(){
  matrix.begin();
  Serial.begin(9600);
  color = matrix.Color333(0,191,255);
  Serial.print("matrix color set\n");
  matrix.drawPixel(15,0, color);
  Serial.print("matrix dot drawn");
  //matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
  //matrix.updateDisplay();
}

The loop function does nothing at the moment so I didn't inlcude it. When I simply try to turn on a single led like in both the sparkfun and adafruit code, I get this random display of LEDs:

Imgur

Has anyone here had any experience with coding these? Any ideas on fixing this would be greatly appreciated!

You say you have a 32x32 matrix, then what is
int cells[16][16] = {0}; ?

ieee488:
You say you have a 32x32 matrix, then what is
int cells[16][16] = {0}; ?

I remember not having enough memory to hold the 32x32 matrix on the uno last summer when I was playing with this matrix, but i had no issues when I just tried it. I now have the full 32x32 matrix represented.

This line is commented out:

//matrix.updateDisplay();

Before that is called, the chips driving the matrix will probably contain random data.

Did you try un-commenting it?

PaulRB:
This line is commented out:

//matrix.updateDisplay();

Before that is called, the chips driving the matrix will probably contain random data.

Did you try un-commenting it?

I just gave it a shot and it did more or less the same thing lighting up a few rows randomly. When I uncommented the fillRect line however, I did get it down to one middle row being lit up solid red(although it should be green).

You say the demo code runs fine with absolutely no changes to the hardware? Is this it? What kind of Arduino are you using (adafruit says uno/mega only).

// colorwheel demo for Adafruit RGBmatrixPanel library.
// Renders a nice circle of hues on our 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607

// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.

#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library

// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8  // MUST be on PORTB! (Use pin 11 on Mega)
#define OE  9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8  // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE  10
//#define A   A3
//#define B   A2
//#define C   A1
//#define D   A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

void setup() {
  int      x, y, hue;
  float    dx, dy, d;
  uint8_t  sat, val;
  uint16_t c;

  matrix.begin();

  for(y=0; y < matrix.width(); y++) {
    dy = 15.5 - (float)y;
    for(x=0; x < matrix.height(); x++) {
      dx = 15.5 - (float)x;
      d  = dx * dx + dy * dy;
      if(d <= (16.5 * 16.5)) { // Inside the circle(ish)?
        hue = (int)((atan2(-dy, dx) + PI) * 1536.0 / (PI * 2.0));
        d = sqrt(d);
        if(d > 15.5) {
          // Do a little pseudo anti-aliasing along perimeter
          sat = 255;
          val = (int)((1.0 - (d - 15.5)) * 255.0 + 0.5);
        } else
        {
          // White at center
          sat = (int)(d / 15.5 * 255.0 + 0.5);
          val = 255;
        }
        c = matrix.ColorHSV(hue, sat, val, true);
      } else {
        c = 0;
      }
      matrix.drawPixel(x, y, c);
    }
  }
}

void loop() {
  // do nothing
}

Are you using the same pins as the demo? This line implies you changed at least one:

#define CLK 11  // MUST be on PORTB!

Pin 11 is indeed on port B. Does the demo work with pin 11?

PaulRB:
You say the demo code runs fine with absolutely no changes to the hardware? Is this it? What kind of Arduino are you using (adafruit says uno/mega only).

// colorwheel demo for Adafruit RGBmatrixPanel library.

// Renders a nice circle of hues on our 32x32 RGB LED matrix:
// 32x32 RGB LED Matrix Panel - 4mm Pitch : ID 607 : $29.95 : Adafruit Industries, Unique & fun DIY electronics and kits

// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.

#include <Adafruit_GFX.h>  // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library

// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8  // MUST be on PORTB! (Use pin 11 on Mega)
#define OE  9
#define LAT 10
#define A  A0
#define B  A1
#define C  A2
#define D  A3
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8  // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE  10
//#define A  A3
//#define B  A2
//#define C  A1
//#define D  A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

void setup() {
  int      x, y, hue;
  float    dx, dy, d;
  uint8_t  sat, val;
  uint16_t c;

matrix.begin();

for(y=0; y < matrix.width(); y++) {
    dy = 15.5 - (float)y;
    for(x=0; x < matrix.height(); x++) {
      dx = 15.5 - (float)x;
      d  = dx * dx + dy * dy;
      if(d <= (16.5 * 16.5)) { // Inside the circle(ish)?
        hue = (int)((atan2(-dy, dx) + PI) * 1536.0 / (PI * 2.0));
        d = sqrt(d);
        if(d > 15.5) {
          // Do a little pseudo anti-aliasing along perimeter
          sat = 255;
          val = (int)((1.0 - (d - 15.5)) * 255.0 + 0.5);
        } else
        {
          // White at center
          sat = (int)(d / 15.5 * 255.0 + 0.5);
          val = 255;
        }
        c = matrix.ColorHSV(hue, sat, val, true);
      } else {
        c = 0;
      }
      matrix.drawPixel(x, y, c);
    }
  }
}

void loop() {
  // do nothing
}




Are you using the same pins as the demo? This line implies you changed at least one:


#define CLK 11  // MUST be on PORTB!



Pin 11 is indeed on port B. Does the demo work with pin 11?

All example code runs fine. I used the sparkfun pin guide when wiring it which used pin 11 instead of 8 for the clock.

Ok. Could it be that the dot you are trying to set is in fact set correctly, but all the other dots are random because you have not set them to anything?

Maybe you are thinking this line sets every dot on the display (which it doesn't)?

 color = matrix.Color333(0,191,255);

PaulRB:
Ok. Could it be that the dot you are trying to set is in fact set correctly, but all the other dots are random because you have not set them to anything?

Maybe you are thinking this line sets every dot on the display (which it doesn't)?

 color = matrix.Color333(0,191,255);

According to the documentation the other dots don't matter, so that should be working.

I deleted the code in my main and just copy pasted example code which was identical to what I had. Oddly enough, its now being responsive and working like it should. Thanks so much for helping though!

Actually I found a new problem that seems sort of common when programming these matrices. If I use memset for any size of 2d array such as cells in the code, I get gibberish output on the display. It will compile if I just predefine the array values which is simple enough for a 4x4 array. However, once I tried to print it it again gives me random crap on the display.

With the 4x4 cells array, I am only using 337 bytes of ram so its not a memory issue. if I was to comment out the print line, it works fine displaying the purple dot in the center. Really not seeing what is wrong with the code here!

int cells[4][4] {
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};

//int cells2[32][32];

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

void setup(){
  matrix.begin();
  matrix.drawPixel(16, 16, matrix.Color333(7, 0, 7));
  Serial.begin(9600);
  print();
 
}
void loop(){
}

void print(){
  int i, j;
  for(i = 0; i < MAX_ROWS; i++){
    for(j = 0; j < MAX_COLS; j++){
      if(cells[i][j] == 1){
        matrix.drawPixel(i, j, matrix.Color333(7, 0, 7)); 
      }
    }
  }
}

Update: So I tried a few changes and found no results. Moving the code from print to main or loop gives the same result. Interestingly, when I comment out the drawDot in print and replace it with a serial print, it prints as expected but it also generates random colors on the display.

  for(i = 0; i < MAX_ROWS; i++){
    for(j = 0; j < MAX_COLS; j++){
      if(cells[i][j] == 1){

If MAX_ROWS and MAX_COLS are 16, your code will be accessing memory outside that allocated for cells[][] which is only 4x4. That memory will have random contents.

PaulRB:

  for(i = 0; i < MAX_ROWS; i++){

for(j = 0; j < MAX_COLS; j++){
      if(cells[i][j] == 1){



If MAX_ROWS and MAX_COLS are 16, your code will be accessing memory outside that allocated for cells[][] which is only 4x4. That memory will have random contents.

I forgot to reflect that change in the code i have. I assigned them to 4 so it would work with the new cells array.

It seems any function I am trying to call is leading to random colors being output. If I try to print a variable to the serial output it will start printing crap, whereas without it, it works fine. Is the Arduino just incapable of driving this board?

You said yourself that the demo sketches work, so clearly it is capable.