16x8 LED Matrix Driver Backpack HT16K33 Arduino Code help

Hi all,

I am recreating this project for Halloween http://www.instructables.com/id/Pumpktris-The-Tetris-Pumpkin/?ALLSTEPS

All the work is done aside from carving the pumpkin however I am having a little trouble. It's simple but I cannot figure this one out. I have had to alter the code a little as I am using the 16x8 Matrix from Adadafruit rather than the two 8x8 suggested. The top 8 light up and run Tetris however the other half does not, I can't figure out where to go from here.

I know the Matrix works as I can run this: Adafruit_LED_Backpack/HT16K33.ino at master · adafruit/Adafruit_LED_Backpack · GitHub perfectly so something is up with my code.

Here is my program: Dropbox - Error - Simplify your life

My guess is the basic problem is associated with this line:

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

The original program had this:

Adafruit_8x8matrix matrixTop = Adafruit_8x8matrix();
Adafruit_8x8matrix matrixBottom = Adafruit_8x8matrix();

Now, I see you've modified your sketch (hopefully) accordingly, but you would need to modify the Adafruit library accordingly. Have you done so? If yes, can you please post your modified library code here as that's where the likely problem is.

arduinodlb:
My guess is the basic problem is associated with this line:

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

The original program had this:

Adafruit_8x8matrix matrixTop = Adafruit_8x8matrix();
Adafruit_8x8matrix matrixBottom = Adafruit_8x8matrix();

Now, I see you've modified your sketch (hopefully) accordingly, but you would need to modify the Adafruit library accordingly. Have you done so? If yes, can you please post your modified library code here as that's where the likely problem is.

Thank you for getting back to me Arduinodlb. When I change Adafruit_8x8matrix matrix = Adafruit_8x8matrix(); to Adafruit_LEDBackpack matrix = Adafruit_LEDBackpack(); I get an error on most lines (attached image)

I'm not sure what you mean by changing the library but the library has all the relevant downloads from adafruit themselves

Screen Shot 2013-10-30 at 18.26.28.png

Look at the name of the class:

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

That is, it is designed for 8x8 matrices, NOT 16x8 matrices.

So, you need to leave the constructor the same, but you will need to actually edit the Adafruit_8x8matrix class in the library code itself. Alternatively, you can extend the class and modify your local class.

The 8x8matix class is defined in the library file Adafruit_LEDBackpack.cpp. Here is that portion of the code:

Adafruit_8x8matrix::Adafruit_8x8matrix(void) : Adafruit_GFX(8, 8) {
}

void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
  if ((y < 0) || (y >= 8)) return;
  if ((x < 0) || (x >= 8)) return;

 // check rotation, move pixel around if necessary
  switch (getRotation()) {
  case 1:
    swap(x, y);
    x = 8 - x - 1;
    break;
  case 2:
    x = 8 - x - 1;
    y = 8 - y - 1;
    break;
  case 3:
    swap(x, y);
    y = 8 - y - 1;
    break;
  }

  // wrap around the x
  x += 7;
  x %= 8;


  if (color) {
    displaybuffer[y] |= 1 << x;
  } else {
    displaybuffer[y] &= ~(1 << x);
  }
}

So, there's a few things here you'll need to look into and change if you want to use this class.

Everything currently assumes 8x8 (hence the class name) so you'll have to modify for 16x8, including buffer sizes, etc.

In any case, you need to find this file on your system and examine it so that you can work out what changes you want to make. Normally, you shouldn't modify libraries directly, but at least to get you going this may be the quickest way. Alternatively, you can duplicate the functionality in your sketch, or extend the library and create a 16x8 version.

Thank you for your support. I am a little bit new to coding, to be honest I thought this would be an easy and fun to do for Halloween. So most of what you are telling me to do is a bit beyond me. I was recommended to use the 16x8 matrix as it was cheaper/supposedly easier than using two 8x8.

If possible could you show me what I need to change and write where and I can perhaps go from there with a better understanding?

Unfortunately, I don't have a 16x8 so have no way of testing any of this. If you're lucky, someone else might chime in.

But, at a guess, you need to modify:

In Adafruit_LEDBackpack.h:
uint16_t displaybuffer[8]; to
uint16_t displaybuffer[16];

In Adafruit_LEDBackpack.cpp
line 84 (change the 8 to 16):
for (uint8_t i=0; i<8; i++) {

line 92 (change the 8 to 16):
for (uint8_t i=0; i<8; i++) {

either 101 or 102 (change the 8 to 16):
if ((y < 0) || (y >= 8)) return;
if ((x < 0) || (x >= 8)) return;

If you're REALLY lucky, that might be enough, or might be enough to get you pointed in the right direction.

You may also have to change lines 105-118, but you can probably get away with not changing these, fingers crossed.

After changing the above lines, if it doesn't work, someone else will have to pipe in.

Unfortunately it makes it worse and the LEDs flicker/barely turn on.

Is it that the 16x8 won't support what I am trying to do or that there need to be new code written for it?

I don't know, but I suspect the 16x8 can probably do in principle what you are trying to do but yes, someone will have to modify some code.

Unfortunately you have a bit of a problem because your sketch calls the setRotation() function. The Adafruit_8x8matrix::drawPixel() method calls the getRotation() function to determine where to draw the pixels.

Line 100 of Adafruit_LEDBackpack.cpp

void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
  if ((y < 0) || (y >= 8)) return;
  if ((x < 0) || (x >= 8)) return;

 // check rotation, move pixel around if necessary
  switch (getRotation()) {
  case 1:
    swap(x, y);
    x = 8 - x - 1;
    break;
  case 2:
    x = 8 - x - 1;
    y = 8 - y - 1;
    break;
  case 3:
    swap(x, y);
    y = 8 - y - 1;
    break;
  }

  // wrap around the x
  x += 7;
  x %= 8;


  if (color) {
    displaybuffer[y] |= 1 << x;
  } else {
    displaybuffer[y] &= ~(1 << x);
  }
}

Unfortunately, because 16x8 is not square, and this code is for 8x8, "rotation" is a little tricky.

For 8x8, rotating 0.0 to 7,0 then 7,7 then 0,7 makes sense.

What do you do on a 16x8? 0,0 to 7,0 makes sense, but 7,15?

So basically, you need to look at your sketch and its use of rotation, and how it interacts with rotation here. This is the core of your current problem I think.

If you're new to programming, this isn't the easiest task. But, with the will and time I'm sure you could do it.

In the interim, you could comment out all setRotation() calls in your sketch and see if the thing sort of works (my guess is tetris blocks would never rotate, but hopefully other than that it would work-ish).

Or, you could try changing the library code to this as a test:

void Adafruit_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
  // wrap around the x
  x += 7;
  x %= 8;

  if (color) {
    displaybuffer[y] |= 1 << x;
  } else {
    displaybuffer[y] &= ~(1 << x);
  }
}

Thanks for all of your help Arduinodlb. This is looking way too complicated for my programming level right now and I've already invested so much time in to this project. I think I am going to cut my losses and replace the 16x8 with two 8x8 and resurrect this project for thanksgiving

Yeah, that sounds like a good idea to me.

Good Luck.

Truly appreciate your help!