//
// Rainbox demo using 4 NeoMatrix panels as a single LED panel.
// 
//

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

#define PIN 8

int Delay = 100;

Adafruit_NeoMatrix panel = Adafruit_NeoMatrix(8, 8, 2, 2, PIN,
  NEO_MATRIX_TOP     + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE +
  NEO_TILE_TOP       + NEO_TILE_LEFT   + NEO_TILE_ROWS   + NEO_TILE_PROGRESSIVE, 
  NEO_GRB            + NEO_KHZ800);

//
// Setup function
//
void setup() {
  panel.begin();
  panel.setBrightness(255);

  buildRainbow();
}

//
// Define rainbow colors
//
uint16_t rainbowColors[256]; 


//
// Loop function
//

void loop() {

  for (int loop=0; loop<=15; loop++) {
    for (int y=0; y<=15; y++) {
      for (int x=0; x<=15; x++) {
        int colorIndex = (((y*16) + x) + (loop*16)) % 256;
        panel.drawPixel(x,y,rainbowColors[colorIndex]); 
      } 
    }
    panel.show();
    delay(Delay);
  }

}


void buildRainbow() {

  int index = 0;
  int colorR = 255;
  int colorG = 0;
  int colorB = 0;

  // Red adding Green ...

  for (int i=0; i<=42; i++) {
    colorG = min(int(6.08*i),255);
    rainbowColors[index] = panel.Color(colorR, colorG, colorB);
    index++;
  }

  // Green subtracting Red ...

  for (int i=0; i<=42; i++) {
    colorR = max(int(255-(6.08*i)),0);
    rainbowColors[index] = panel.Color(colorR, colorG, colorB);
    index++;
  }

  // Green adding Blue ...

  for (int i=0; i<=41; i++) {
    colorB = min(int(6.22*i),255);
    rainbowColors[index] = panel.Color(colorR, colorG, colorB);
    index++;
  }

  // Blue subtracting Green ...

  for (int i=0; i<=42; i++) {
    colorG = max(int(255-(6.08*i)),0);
    rainbowColors[index] = panel.Color(colorR, colorG, colorB);
    index++;
  }

  // Blue Adding Red ...

  for (int i=0; i<=42; i++) {
    colorR = min(int(6.08*i),255);
    rainbowColors[index] = panel.Color(colorR, colorG, colorB);
    index++;
  }

  // Red Subtracting Blue ...

  for (int i=0; i<=41; i++) {
    colorB = max(int(255-(6.22*i)),0);
    rainbowColors[index] = panel.Color(colorR, colorG, colorB);
    index++;
  }
}
X Coordinate -->
Y Coordinate -->