HOW to call animations on a 8x8 LED-Matrix

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Tissi_2
 
Posts: 20
Joined: Mon Jun 22, 2015 10:46 am

HOW to call animations on a 8x8 LED-Matrix

Post by Tissi_2 »

Hi there!
Again a simple question for you. I have tried to show a simple animation with 31 frames on a 8x8 LED matrix with a I2C backpack. I am a little bit confused about the code. I did not find any good guide for calling up functions like this except the tutorial for the https://learn.adafruit.com/trinket-slas ... ource-code. I have edited the code a little bit to make it work on an arduino uno. But I really don´t know what the reorder section is for. As you can see, I am a rookie in this. Am I to stupid for this or is it normal to have such problems in understanding the code?

What I am trying to do consists of this:

Code: Select all

    #define BRIGHTNESS   10 // 0=min, 15=max
    #define I2C_ADDR   0x70 // Edit if backpack A0/A1 jumpers set
    #define REPS 3 // Number of times to repeat the animation loop (1-105)
    #include <Adafruit_LEDBackpack.h>  
    #include <Wire.h>
    #include <Adafruit_GFX.h>


    const uint8_t PROGMEM anim[][8] = {
     
                  B11111111, B00011000, B00011000, B00011000, B00011000, B00011000, B00011000, B11111111,10,   //  1
                  B11111111, B11111111, B00111100, B00111100, B00111100, B00111100, B11111111, B11111111,10,   //  2
                  B11111111, B11111111, B11111111, B01111110, B01111110, B11111111, B11111111, B11111111,10 ,  //  3
                  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111,10 , //  4
                  B00000000, B11100111, B11100111, B11100111, B11100111, B11100111, B11100111, B00000000,10  , //  5
                  B00000000, B00000000, B11000011, B11000011, B11000011, B11000011, B00000000, B00000000,10  ,//  6
                  B00000000, B00000000, B00000000, B10000001, B10000001, B00000000, B00000000, B00000000,10  , //  7
                  B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,10  ,//  8
                  B00011000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,10  ,//  9
                  B00111000, B00001000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,10  ,//  10
                  B01111000, B00001000, B00001000, B00000000, B00000000, B00000000, B00000000, B00000000,10  ,//  11
                  B11111000, B00001000, B00001000, B00001000, B00000000, B00000000, B00000000, B00000000,10  ,//  12
                  B11111001, B00001000, B00001000, B00001000, B00001000, B00000000, B00000000, B00000000,10  ,//  13
                  B11111011, B00001000, B00001000, B00001000, B00001000, B00001000, B00000000, B00000000,10  ,//  14
                  B11111111, B00001000, B00001000, B00001000, B00001000, B00001000, B00001000, B00000000,10  ,//  15
                  B11111111, B00001000, B00001000, B00001000, B00001000, B00001000, B00001000, B00001000,10 ,//  16
                  B00000100, B11111111, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100,10 ,//  17
                  B00000010, B00000010, B11111111, B00000010, B00000010, B00000010, B00000010, B00000010,10 , //  18
                  B00000001, B00000001, B00000001, B11111111, B00000001, B00000001, B00000001, B00000001,10 , //  19
                  B10000000, B10000000, B10000000, B10000000, B11111111, B10000000, B10000000, B10000000,10 , //  20
                  B01000000, B01000000, B01000000, B01000000, B01000000, B11111111, B01000000, B01000000,10 , //  21
                  B00100000, B00100000, B00100000, B00100000, B00100000, B00100000, B11111111, B00100000,10 ,//  22
                  B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B11111111,10 , //  23
                  B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B11111111, B00010000,10 , //  24
                  B00010000, B00010000, B00010000, B00010000, B00010000, B11111111, B00010000, B00010000,10 ,//  10
                  B00010000, B00010000, B00010000, B00010000, B11111111, B00010000, B00010000, B00010000,10 , //  26
                  B00010000, B00010000, B00010000, B11111111, B00010000, B00010000, B00010000, B00010000,10 , //  27
                  B00010000, B00010000, B11111111, B00010000, B00010000, B00010000, B00010000, B00010000,10 ,//  28
                  B00010000, B11111111, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000,10 ,//  29
                  B11111111, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000,10 , //  30
                  B00100000, B11111111, B00100000, B00100000, B00100000, B00100000, B00100000, B00100000,10 , // 31
    }; 
So, what is the correct and easiest way to show these frames? If I have missed to read a guide that explains this, then please post the link.

Thank you guys!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by adafruit_support_rick »

You got me - I don't understand the reordering table, either. But it sure looks like you need it. I'll ask around...

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by pburgess »

The reorder[] table is needed by the Space Invaders code because it addresses the I2C matrix backpack directly (not through the Adafruit_LEDBackpack and Adafruit_GFX libraries). The GFX library's too big for the Gemma microcontroller. The table reverses the order of the bits and rotates left or right one bit, I forget which, because this is the order that the wires needed to be physically routed on the matrix backpack.

If using a larger microcontroller than Trinket or Gemma, you don't need this. The Adafruit_LEDBackpack handles the flip-and-rotate on its own.

To draw one bitmap from the table, use:

Code: Select all

matrix.clear();
matrix.drawBitmap(0, 0, &anim[frame * 9], 8, 8, 1);
The first two arguments (0, 0) are the top-left coordinate where the bitmap will be drawn. 0,0 in this case because we want it using the whole matrix.
&anim[frame * 9] is a pointer to the first byte of the image for a given frame (starting from index 0). '* 9' because each frame is 9 bytes of data (8 for the image, then the last byte is the time in milliseconds, which can be retrieved with t = pgm_read_byte(&anim[frame * 9 + 8]);
8, 8, 1 are the width and height in pixels (both 8 in this case) and the color (gonna assume 1 if you're on a single-color matrix).

User avatar
Tissi_2
 
Posts: 20
Joined: Mon Jun 22, 2015 10:46 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by Tissi_2 »

Ah, good to know! Thank you!

And how do I declare the frames according to the code I have given? Just to be sure... There is no guide for this specific matrix with i2C-backpack?

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by pburgess »

I think they'll work fine the way you have them declared.

User avatar
Tissi_2
 
Posts: 20
Joined: Mon Jun 22, 2015 10:46 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by Tissi_2 »

Nope!

Arduino: 1.6.5 (Windows 7), Platine: "Arduino Uno"

matrix8x8.ino: In function 'void loop()':
matrix8x8:71: error: 'frame' was not declared in this scope
'frame' was not declared in this scope

Here is the complete code:

Code: Select all

/*************************************************** 
  This is a library for our I2C LED Backpacks

  Designed specifically to work with the Adafruit LED Matrix backpacks 
  ----> http://www.adafruit.com/products/872
  ----> http://www.adafruit.com/products/871
  ----> http://www.adafruit.com/products/870

  These displays use I2C to communicate, 2 pins are required to 
  interface. There are multiple selectable I2C addresses. For backpacks
  with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
  with 3 Address Select pins: 0x70 thru 0x77

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

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

#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

void setup() {
  Serial.begin(9600);
  Serial.println("8x8 LED Matrix Test");
  
  matrix.begin(0x70);  // pass in the address
}

    const uint8_t PROGMEM anim[][8] = {
     
                  B11111111, B00011000, B00011000, B00011000, B00011000, B00011000, B00011000, B11111111,10,   //  1
                  B11111111, B11111111, B00111100, B00111100, B00111100, B00111100, B11111111, B11111111,10,   //  2
                  B11111111, B11111111, B11111111, B01111110, B01111110, B11111111, B11111111, B11111111,10 ,  //  3
                  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111,10 , //  4
                  B00000000, B11100111, B11100111, B11100111, B11100111, B11100111, B11100111, B00000000,10  , //  5
                  B00000000, B00000000, B11000011, B11000011, B11000011, B11000011, B00000000, B00000000,10  ,//  6
                  B00000000, B00000000, B00000000, B10000001, B10000001, B00000000, B00000000, B00000000,10  , //  7
                  B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,10  ,//  8
                  B00011000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,10  ,//  9
                  B00111000, B00001000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,10  ,//  10
                  B01111000, B00001000, B00001000, B00000000, B00000000, B00000000, B00000000, B00000000,10  ,//  11
                  B11111000, B00001000, B00001000, B00001000, B00000000, B00000000, B00000000, B00000000,10  ,//  12
                  B11111001, B00001000, B00001000, B00001000, B00001000, B00000000, B00000000, B00000000,10  ,//  13
                  B11111011, B00001000, B00001000, B00001000, B00001000, B00001000, B00000000, B00000000,10  ,//  14
                  B11111111, B00001000, B00001000, B00001000, B00001000, B00001000, B00001000, B00000000,10  ,//  15
                  B11111111, B00001000, B00001000, B00001000, B00001000, B00001000, B00001000, B00001000,10 ,//  16
                  B00000100, B11111111, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100,10 ,//  17
                  B00000010, B00000010, B11111111, B00000010, B00000010, B00000010, B00000010, B00000010,10 , //  18
                  B00000001, B00000001, B00000001, B11111111, B00000001, B00000001, B00000001, B00000001,10 , //  19
                  B10000000, B10000000, B10000000, B10000000, B11111111, B10000000, B10000000, B10000000,10 , //  20
                  B01000000, B01000000, B01000000, B01000000, B01000000, B11111111, B01000000, B01000000,10 , //  21
                  B00100000, B00100000, B00100000, B00100000, B00100000, B00100000, B11111111, B00100000,10 ,//  22
                  B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B11111111,10 , //  23
                  B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B11111111, B00010000,10 , //  24
                  B00010000, B00010000, B00010000, B00010000, B00010000, B11111111, B00010000, B00010000,10 ,//  10
                  B00010000, B00010000, B00010000, B00010000, B11111111, B00010000, B00010000, B00010000,10 , //  26
                  B00010000, B00010000, B00010000, B11111111, B00010000, B00010000, B00010000, B00010000,10 , //  27
                  B00010000, B00010000, B11111111, B00010000, B00010000, B00010000, B00010000, B00010000,10 ,//  28
                  B00010000, B11111111, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000,10 ,//  29
                  B11111111, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000,10 , //  30
                  B00100000, B11111111, B00100000, B00100000, B00100000, B00100000, B00100000, B00100000,10 , 
    }; 
void loop() {
  matrix.clear();
matrix.drawBitmap(0, 0, &anim[frame * 9], 8, 8, 1);
  matrix.writeDisplay();
  delay(500);
  
  matrix.clear();      // clear display
  matrix.drawPixel(0, 0, LED_ON);  
  matrix.writeDisplay();  // write the changes we just made to the display
  delay(500);

  matrix.clear();
  matrix.drawLine(0,0, 7,7, LED_ON);
  matrix.writeDisplay();  // write the changes we just made to the display
  delay(500);

  matrix.clear();
  matrix.drawRect(0,0, 8,8, LED_ON);
  matrix.fillRect(2,2, 4,4, LED_ON);
  matrix.writeDisplay();  // write the changes we just made to the display
  delay(500);

  matrix.clear();
  matrix.drawCircle(3,3, 3, LED_ON);
  matrix.writeDisplay();  // write the changes we just made to the display
  delay(500);

  matrix.setTextSize(1);
  matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  matrix.setTextColor(LED_ON);
  for (int8_t x=0; x>=-36; x--) {
    matrix.clear();
    matrix.setCursor(x,0);
    matrix.print("Hello");
    matrix.writeDisplay();
    delay(100);
  }
  matrix.setRotation(3);
  for (int8_t x=7; x>=-36; x--) {
    matrix.clear();
    matrix.setCursor(x,0);
    matrix.print("World");
    matrix.writeDisplay();
    delay(100);
  }
  matrix.setRotation(0);
}

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by pburgess »

Well yeah, that's just kinda pseudocode...'frame' is the frame number you want to display. Maybe it's an int variable. Maybe it's just a number you hardcode in its place. Depends what you're aiming for.

e.g. for(uint8_t frame=0; frame<10; frame++) { ...draw stuff here... }

User avatar
Tissi_2
 
Posts: 20
Joined: Mon Jun 22, 2015 10:46 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by Tissi_2 »

"frame" could have been code that is implemented in the GFX.h or LEDBackpack.h. for calling bitmaps. Btw I really don´t understand it. Do I have to define each frame by this:

Code: Select all

frame1[] =
  { B00111100,
    B01000010,
    B10100101,
    B10000001,
    B10100101,
    B10011001,
    B01000010,
    B00111100 },
  frame2[] =
  { B00111100,
    B01000010,
    B10100101,
    B10000001,
    B10111101,
    B10000001,
    B01000010,
    B00111100 },
I would like to write all frames as I have done it in my first post or as it is published here.https://learn.adafruit.com/trinket-slas ... /animation

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by pburgess »

No, you don't need to define every frame separately, they can all go in one huge concatenated block like you have there.

In that case though, you do need to declare the 'anim' array as one-dimensional, like this:

Code: Select all

    const uint8_t PROGMEM anim[] = {
The call to drawBitmap() as I showed it (multiplying the frame number by 9) should then work.

The Space Invader project broke out each frame into a separate piece of a 2D array for clarity, but it's not entirely necessary if you don't want to do it that way.

User avatar
Disciple
 
Posts: 852
Joined: Tue Jan 06, 2015 8:13 pm

Re: HOW to call animations on a 8x8 LED-Matrix

Post by Disciple »

Hello there. I just saw this on the blog today.
Animation editor for 8×8 LED backpacks
I don't know if it falls anywhere into this discussion, but if so, it looks like a nifty time saver.

Hallelujah!
Disciple

User avatar
Tissi_2
 
Posts: 20
Joined: Mon Jun 22, 2015 10:46 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by Tissi_2 »

Thank you for this great tool! I used this http://maximumoctopus.com/electronics/builder.htm builder to create my animation.
My only questions that are left:
a) what has to be in the setup
b) what has to be in the loop
to work correctly.

I tried:

Code: Select all

  void setup() {
      Wire.begin();              // I2C init
      clear();                   // Blank display
      }
and

Code: Select all

void loop() {
      for(int i=0; i<sizeof(anim); i) { // For each frame...
        Wire.beginTransmission(I2C_ADDR);
        Wire.write(0);   
        matrix.drawBitmap(0, 0, &anim[i * 9], 8, 8, 1);
        }
        Wire.endTransmission();
  delay(50);
}
but it won´t work.

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by pburgess »

Before setup(), declare the matrix object as a global variable:

Code: Select all

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
In setup(), call matrix.begin(), passing the I2C address as an argument (0x70 is the default, unless you've changed the solder jumpers on the board):

Code: Select all

void setup() {
  matrix.begin(0x70);
}
There are some examples included with the Adafruit_LEDBackpack library that might provide some guidance.

User avatar
Tissi_2
 
Posts: 20
Joined: Mon Jun 22, 2015 10:46 am

Re: HOW to call animations on a 8x8 LED-Matrix

Post by Tissi_2 »

Okay, for all of you who had the same problem. Here is a working code:

Code: Select all

    #define BRIGHTNESS=1 // 0=min, 15=max
    #include <Adafruit_LEDBackpack.h>  
    #include <Wire.h>
    #include <Adafruit_GFX.h>


   const uint8_t PROGMEM anim[] = {
 // Frame 0
  B11111111,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B11111111,
  25,

  // Frame 1
  B11111111,
  B11111111,
  B11000011,
  B11000011,
  B11000011,
  B11000011,
  B11111111,
  B11111111,
  25,

  // Frame 2
  B11111111,
  B11111111,
  B11111111,
  B11100111,
  B11100111,
  B11111111,
  B11111111,
  B11111111,
  25,

  // Frame 3
  B11111111,
  B11111111,
  B11111111,
  B11111111,
  B11111111,
  B11111111,
  B11111111,
  B11111111,
  25,

  // Frame 4
  B00000000,
  B01111110,
  B01111110,
  B01111110,
  B01111110,
  B01111110,
  B01111110,
  B00000000,
  25,

  // Frame 5
  B00000000,
  B00000000,
  B00111100,
  B00111100,
  B00111100,
  B00111100,
  B00000000,
  B00000000,
  25,

  // Frame 6
  B00000000,
  B00000000,
  B00000000,
  B00011000,
  B00011000,
  B00000000,
  B00000000,
  B00000000,
  25,

  // Frame 7
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  B00000000,
  25,
};

 
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();  
void setup() {
     matrix.begin(0x70);
}     
void loop() {
     
  for(int i=0; i<8; i++) { // For each frame...
  matrix.clear();
  matrix.drawBitmap(0, 0, &anim[i * 9], 8, 8, 1);
  matrix.writeDisplay();
  delay(50);
  }
}
If you are using a "time" (in this example ,25) at the end of each frame the matrix.drawBitmap should be:

Code: Select all

matrix.drawBitmap(0, 0, &anim[i * 9], 8, 8, 1)
If you are not using a "time" at the end of each frame and want to do it with a delay the matrix.drawBitmap should be:

Code: Select all

matrix.drawBitmap(0, 0, &anim[i * 8], 8, 8, 1)
Thank you for your patience and advise!

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”