This is a simple thing, but it's kind of fun to watch. Four RGB Matrix r2 units are placed in a 2x2 array to create a 16x16 screen. A dot "walks" around the screen with a random color at each step. The trail it leaves behind fades exponentially, creating a natural appearance.
I had a bit of trouble handling the four Matrices in the code. At the outset, I assumed I would be able to create an array of "RGB_Matrix" objects. Then, for example, I would be able to make statements like:
matrix[i].setPixel( row, col, color)
But I was unable to make that happen. Instead, I just declared four separate variables, one for each 8x8 matrix. My object-oriented coding experience is nearly non-existent, so it's possible that I just failed to get the syntax right. I crammed all the inelegant parts into a function called transmitScreen(), included in the code below.
#include <Wire.h>
#include <RGB_Matrix_Arduino.h>
// arrangement of 8x8 matrices
#define MATRIX_ROWS 2
#define MATRIX_COLS 2
// screen dimensions (all LED arrays together)
#define NUM_ROWS 8*MATRIX_ROWS
#define NUM_COLS 8*MATRIX_COLS
// Arrays holding current red, green, blue intensities for every pixel
uint8_t screen_r[NUM_ROWS][NUM_COLS];
uint8_t screen_g[NUM_ROWS][NUM_COLS];
uint8_t screen_b[NUM_ROWS][NUM_COLS];
uint8_t buf[192];
RGB_Matrix matrix0 = RGB_Matrix((uint8_t)0x50, buf);
RGB_Matrix matrix1 = RGB_Matrix((uint8_t)0x51, buf);
RGB_Matrix matrix2 = RGB_Matrix((uint8_t)0x52, buf);
RGB_Matrix matrix3 = RGB_Matrix((uint8_t)0x53, buf);
void setup (void) {
// Serial.begin(57600);
// Serial.println("I live!");
int i,j;
for (i = 0; i < NUM_ROWS; i++) {
for (j = 0; j < NUM_COLS; j++) {
screen_r[i][j] = 0;
screen_g[i][j] = 0;
screen_b[i][j] = 0;
}
}
Wire.begin();
transmitScreen();
}
void loop (void) {
static uint32_t color;
uint8_t i, j, k, walk, red, green, blue;
static int8_t annie_row=4, annie_col=4;
//write out the whole screen
transmitScreen();
delay(20);
walk = random(9);
switch (walk) {
case 0:
if (++annie_row >= NUM_ROWS) annie_row = NUM_ROWS-1 ;
break;
case 1:
if (--annie_row < 0) annie_row = 0 ;
break;
case 2:
if (++annie_col >= NUM_COLS) annie_col = NUM_COLS-1;
break;
case 3:
if (--annie_col < 0) annie_col = 0;
break;
case 4:
if (++annie_row >= NUM_ROWS) annie_row = NUM_ROWS-1 ;
if (++annie_col >= NUM_COLS) annie_col = NUM_COLS-1;
break;
case 5:
if (--annie_row < 0) annie_row = 0 ;
if (++annie_col >= NUM_COLS) annie_col = NUM_COLS-1;
break;
case 6:
if (++annie_row >= NUM_ROWS) annie_row = NUM_ROWS-1 ;
if (--annie_col < 0) annie_col = 0;
break;
case 7:
if (--annie_row < 0) annie_row = 0 ;
if (--annie_col < 0) annie_col = 0;
break;
default:
;//don't move
}
// light up annie's new location
switch (random(6)) {
case 0:
screen_r[annie_row][annie_col]+= 0x60;
break;
case 1:
screen_g[annie_row][annie_col]+= 0x60;
break;
case 2:
screen_b[annie_row][annie_col]+= 0x60;
case 3:
screen_r[annie_row][annie_col]+= 0x30;
screen_g[annie_row][annie_col]+= 0x30;
break;
case 4:
screen_g[annie_row][annie_col]+= 0x30;
screen_b[annie_row][annie_col]+= 0x30;
break;
case 5:
screen_b[annie_row][annie_col]+= 0x30;
screen_r[annie_row][annie_col]+= 0x30;
}
// fade entire array a bit
for (i = 0; i < NUM_ROWS; i++) {
for (j = 0; j < NUM_COLS; j++) {
red = screen_r[i][j];
green = screen_g[i][j];
blue = screen_b[i][j];
red = ((red<<3) - red)>>3;
green = ((green<<3) - green)>>3;
blue = ((blue<<3) - blue)>>3;
screen_r[i][j] = red;
screen_g[i][j] = green;
screen_b[i][j] = blue;
}
}
}
void transmitScreen(void) {
int i,j,k,row,col;
k = 0;
for (i = 0; i < MATRIX_ROWS; i++) {
for (j = 0; j < MATRIX_COLS; j++) {
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
switch (k) {
case 0:
matrix0.setPixel( row, col, screen_r[row+8*i][col+8*j], screen_g[row+8*i][col+8*j], screen_b[row+8*i][col+8*j] );
break;
case 1:
matrix1.setPixel( row, col, screen_r[row+8*i][col+8*j], screen_g[row+8*i][col+8*j], screen_b[row+8*i][col+8*j] );
break;
case 2:
matrix2.setPixel( row, col, screen_r[row+8*i][col+8*j], screen_g[row+8*i][col+8*j], screen_b[row+8*i][col+8*j] );
break;
case 3:
matrix3.setPixel( row, col, screen_r[row+8*i][col+8*j], screen_g[row+8*i][col+8*j], screen_b[row+8*i][col+8*j] );
break;
}
}
}
switch (k) {
case 0:
matrix0.transmitBuf();
break;
case 1:
matrix1.transmitBuf();
break;
case 2:
matrix2.transmitBuf();
break;
case 3:
matrix3.transmitBuf();
break;
}
k++;
}
}
}