#include <Adafruit_NeoPixel.h>

#define PIN 2 // Define the pin where your NeoPixel matrix is connected
#define WIDTH 32
#define HEIGHT 8

Adafruit_NeoPixel strip = Adafruit_NeoPixel(WIDTH * HEIGHT, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Blink all rows in rainbow colors
  for (int row = 0; row < HEIGHT; row++) {
    rainbowBlinkRow(row);
  }
  
  // Blink all columns in rainbow colors
  for (int col = 0; col < WIDTH; col++) {
    rainbowBlinkColumn(col);
  }
}

void rainbowBlinkRow(int row) {
  for (int i = 0; i < WIDTH; i++) {
    strip.setPixelColor(row * WIDTH + i, Wheel((i * 256 / WIDTH) & 255));
  }
  strip.show();
  delay(200);
  clearRow(row);
  strip.show();
  delay(200);
}

void rainbowBlinkColumn(int col) {
  for (int i = 0; i < HEIGHT; i++) {
    strip.setPixelColor(i * WIDTH + col, Wheel((col * 256 / WIDTH) & 255));
  }
  strip.show();
  delay(200);
  clearColumn(col);
  strip.show();
  delay(200);
}

void clearRow(int row) {
  for (int i = 0; i < WIDTH; i++) {
    strip.setPixelColor(row * WIDTH + i, 0); // Turn off all pixels in the row
  }
}

void clearColumn(int col) {
  for (int i = 0; i < HEIGHT; i++) {
    strip.setPixelColor(i * WIDTH + col, 0); // Turn off all pixels in the column
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
    WheelPos -= 170;
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}