0

Programming Neomatrix with Array
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Programming Neomatrix with Array

by Mjroman20 on Thu Mar 08, 2018 8:59 pm

I've scoured the web trying to find a close example as a starting point but came up empty handed. Essentially I am trying to pass an 8x8 array to a neomatrix to display "images" on the matrix, just for the sake of making it easy to do small changes or add additional frames of animation. For example:
1,0,0,0,1
0,1,0,1,0
0,0,1,0,0

Would result in a "V" being displayed. Any suggestions on where to start? I've gone through the Neopixle Uberguide and installed all the useful libraries.

Mjroman20
 
Posts: 4
Joined: Sun Mar 04, 2018 12:21 am

Re: Programming Neomatrix with Array

by Mjroman20 on Fri Mar 09, 2018 12:22 am

Update, finally found something close to what I'm trying to do. I've modified the code a bit to my needs so far but now I see one issue; when changing from 1 array to the next it doesn't seem to properly update all the pixels. I added a couple of color wipes between the transitions and it fixes the issue but creates a new one; it creates a very obvious full refresh of the matrix. Any suggestions for smoothing it out?

RGB.h
Code: Select all | TOGGLE FULL SIZE
struct RGB {
  byte r;
  byte g;
  byte b;
};
 
// Define some colors we'll use frequently
RGB white = { 255, 255, 255 };
RGB red = { 80, 0, 0 };
RGB blu = {0,0,80};
RGB blu2 = {0,0,100};
RGB off = { 0, 0, 0 };


Main Code
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include "RGB.h"
#define PIN 6

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
  NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
  NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,
  NEO_GRB            + NEO_KHZ800);

  void setup() {
  matrix.begin();
  matrix.setBrightness(30);
  matrix.setTextColor( matrix.Color(255, 255, 255) );
  matrix.setTextWrap(false);
}


void loop() {

EyeNeu();
delay(1000);
crossFade(off, off, 1, 0);
EyeLeft3();
delay(1000);
crossFade(off, off, 1, 0);
}

// Fade pixel (x, y) from startColor to endColor
void fadePixel(int x, int y, RGB startColor, RGB endColor, int steps, int wait) {
  for(int i = 0; i <= steps; i++)
  {
     int newR = startColor.r + (endColor.r - startColor.r) * i / steps;
     int newG = startColor.g + (endColor.g - startColor.g) * i / steps;
     int newB = startColor.b + (endColor.b - startColor.b) * i / steps;
     
     matrix.drawPixel(x, y, matrix.Color(newR, newG, newB));
     matrix.show();
     delay(wait);
  }
}

// Crossfade entire screen from startColor to endColor
void crossFade(RGB startColor, RGB endColor, int steps, int wait) {
  for(int i = 0; i <= steps; i++)
  {
     int newR = startColor.r + (endColor.r - startColor.r) * i / steps;
     int newG = startColor.g + (endColor.g - startColor.g) * i / steps;
     int newB = startColor.b + (endColor.b - startColor.b) * i / steps;
     
     matrix.fillScreen(matrix.Color(newR, newG, newB));
     matrix.show();
     delay(wait);
  }
}

void EyeNeu() {
  // This 8x8 array represents the LED matrix pixels.
  // A value of 1 means we’ll fade the pixel to white
  int logo[8][8] = { 
   {0, 0, 1, 1, 1, 1, 0, 0},
   {0, 1, 1, 1, 1, 1, 1, 0},
   {1, 1, 1, 0, 0, 1, 1, 1},
   {1, 1, 0, 0, 0, 0, 1, 1},
   {1, 1, 0, 0, 0, 0, 1, 1},
   {1, 1, 1, 0, 0, 1, 1, 1},
   {0, 1, 1, 1, 1, 1, 1, 0},
   {0, 0, 1, 1, 1, 1, 0, 0}
  };
   
  for(int row = 0; row < 8; row++) {
    for(int column = 0; column < 8; column++) {
     if(logo[row][column] == 1) {
      fadePixel(column, row, blu, blu, 1, 0);
     }
   }
  }
}

void EyeLeft3() {
  // This 8x8 array represents the LED matrix pixels.
  // A value of 1 means we’ll fade the pixel to white
  int logo[8][8] = { 
   {0, 0, 1, 1, 1, 1, 0, 0},
   {0, 1, 1, 1, 1, 1, 1, 0},
   {1, 0, 1, 1, 1, 1, 1, 1},
   {0, 0, 0, 1, 1, 1, 1, 1},
   {0, 0, 0, 1, 1, 1, 1, 1},
   {1, 0, 1, 1, 1, 1, 1, 1},
   {0, 1, 1, 1, 1, 1, 1, 0},
   {0, 0, 1, 1, 1, 1, 0, 0}
  };
   
  for(int row = 0; row < 8; row++) {
    for(int column = 0; column < 8; column++) {
     if(logo[row][column] == 1) {
      fadePixel(column, row, blu, blu, 1, 0);
     }
   }
  }
}

Mjroman20
 
Posts: 4
Joined: Sun Mar 04, 2018 12:21 am

Re: Programming Neomatrix with Array

by Disciple on Fri Mar 09, 2018 4:10 am

I think I know the thing you were looking for. It's the bitmap portion of the Adafruit GFX Graphics Library. Its purpose is displaying shapes like these. I use them in my holiday LED rings sketches and in other discussions.

With regard to your sketch, both your eye functions call fadePixel() to draw their shapes, which contains matrix.show() and delay(wait). Those can both be in a more efficient place. You could try moving matrix.show() to the end of each eye function and eliminating delay(wait) altogether,
...or use the bitmap function from Adafruit_GFX, which could simplify your sketch quite a lot. Best of success.

Hallelujah!
Disciple

Disciple
 
Posts: 543
Joined: Tue Jan 06, 2015 8:13 pm

Re: Programming Neomatrix with Array

by Mjroman20 on Fri Mar 09, 2018 7:31 pm

Excellent, thank you!! It took some playing with but reading over some of your posts defiantly helped. I threw this together real quick once I figured out the basic process, simple blinking eye animation. I'll have to make up some functions to call up as I go along.

Code: Select all | TOGGLE FULL SIZE
 

    #include <Adafruit_GFX.h>
    #include <Adafruit_NeoMatrix.h>
    #include <Adafruit_NeoPixel.h>
   

    // Parameter 1 = number of pixels in strip
    // Parameter 2 = pin number (most are valid)
    // Parameter 3 = pixel type flags, add together as needed:
    //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
    //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
    //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
    //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
    #define NEO_PIN 6
    Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, NEO_PIN, NEO_MATRIX_BOTTOM + NEO_MATRIX_LEFT + NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE, NEO_GRB + NEO_KHZ800);
    int wait = 50;


const unsigned char PROGMEM EyeNeu[] = {0x3c, 0x7e, 0xe7, 0xc3, 0xc3, 0xe7, 0x7e, 0x3c};

const unsigned char PROGMEM LL1[] = {0x3c, 0x7e, 0xef, 0xc7, 0xc7, 0xef, 0x7e, 0x3c};
const unsigned char PROGMEM LL2[] = {0x3c, 0x7e, 0xdf, 0x8f, 0x8f, 0xdf, 0x7e, 0x3c};
const unsigned char PROGMEM LL3[] = {0x3c, 0x7e, 0xbf, 0x1f, 0x1f, 0xbf, 0x7e, 0x3c};

const unsigned char PROGMEM BLINK1[] = {0x0, 0x7e, 0xe7, 0xc3, 0xc3, 0xe7, 0x7e, 0x0};
const unsigned char PROGMEM BLINK2[] = {0x0, 0x0, 0xe7, 0xc3, 0xc3, 0xe7, 0x0, 0x0};
const unsigned char PROGMEM BLINK3[] = {0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0};

void setup() {

  matrix.begin();            // Initialize all Neopixels to 'off'
  matrix.setBrightness(32);  // Keep colors dim
}


void loop() {
  matrix.fillScreen(0x0);
  matrix.drawBitmap(0, 0, EyeNeu, 8, 8, 0x00f5);
  matrix.show();
  delay(1000);
 
  matrix.fillScreen(0x0);
  matrix.drawBitmap(0, 0, BLINK1, 8, 8, 0x00f5);
  matrix.show();
  delay(wait);

  matrix.fillScreen(0x0);
  matrix.drawBitmap(0, 0, BLINK2, 8, 8, 0x00f5);
  matrix.show();
  delay(wait);
 
  matrix.fillScreen(0x0);
  matrix.drawBitmap(0, 0, BLINK3, 8, 8, 0x00f5);
  matrix.show();
  delay(wait);
 
  matrix.fillScreen(0x0);
  matrix.drawBitmap(0, 0, BLINK2, 8, 8, 0x00f5);
  matrix.show();
  delay(wait);
 
  matrix.fillScreen(0x0);
  matrix.drawBitmap(0, 0, BLINK1, 8, 8, 0x00f5);
  matrix.show();
  delay(wait);
 
}

Mjroman20
 
Posts: 4
Joined: Sun Mar 04, 2018 12:21 am

Please be positive and constructive with your questions and comments.