Skip to content

Instantly share code, notes, and snippets.

@alexymik
Last active April 12, 2023 02:56
Show Gist options
  • Save alexymik/9497361 to your computer and use it in GitHub Desktop.
Save alexymik/9497361 to your computer and use it in GitHub Desktop.
Cycles through animations of random colors on my NeoPixel board. Uses the Adafruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#define PIN 4
// 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(49, PIN, NEO_GRB + NEO_KHZ800);
uint32_t randomColor;
// Declare the matrix as a mutlidimensional array in order,
// since it's wired sequentially
int matrix[7][7] = {
{ 0, 1, 2, 3, 4, 5, 6},
{13, 12, 11, 10, 9, 8, 7},
{14, 15, 16, 17, 18, 19, 20},
{27, 26, 25, 24, 23, 22, 21},
{28, 29, 30, 31, 32, 33, 34},
{41, 40, 39, 38, 37, 36, 35},
{42, 43, 44, 45, 46, 47, 48}
};
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip.setBrightness(254);
}
void loop() {
// 7 x 7 Grid
int wipeDelay = 50;
for (int loops = 0; loops < 50; loops++) {
randomColor = strip.Color(random(32,254), random(32,254), random(32,254));
// 'Maximize effect'
for (int i = 0; i < 7; i++) {
for (int j = 0; j <= i; j++) {
strip.setPixelColor(matrix[i][j], randomColor);
strip.setPixelColor(matrix[j][i], randomColor);
strip.show();
}
delay(500 / (loops + 1));
}
}
delay(1000);
// Flash alternating rows with alternating colors
for (int loops = 0; loops < 20; loops++) {
randomColor = strip.Color(random(32,254), random(32,254), random(32,254));
for (int i = 0; i < 7; i += 2) { //Every other row
wipeRow(i, randomColor, 0);
}
delay(100);
randomColor = strip.Color(random(32,254), random(32,254), random(32,254));
for (int i = 1; i < 7; i += 2) {
wipeRow(i, randomColor, 0);
}
delay(100);
}
// Wipe the board once from every direction with a random color
for (int rowColDelay = 10; rowColDelay <= 15; rowColDelay++) {
randomColor = strip.Color(random(32,254), random(32,254), random(32,254));
for (int i = 0; i < 7; i++) {
wipeCol(i, randomColor, rowColDelay);
delay(wipeDelay);
}
randomColor = strip.Color(random(32,254), random(32,254), random(32,254));
for (int i = 0; i < 7; i++) {
wipeRow(i, randomColor, rowColDelay);
delay(wipeDelay);
}
randomColor = strip.Color(random(32,254), random(32,254), random(32,254));
for (int i = 6; i >= 0; i--) {
wipeCol(i, randomColor, rowColDelay);
delay(wipeDelay);
}
randomColor = strip.Color(random(32,254), random(32,254), random(32,254));
for (int i = 6; i >= 0; i--) {
wipeRow(i, randomColor, rowColDelay);
delay(wipeDelay);
}
}
}
void wipeCol(int col, uint32_t color, int wipeDelay) {
for (int i = 0; i < 7; i++) {
int pixelToLightUp = matrix[i][col];
strip.setPixelColor(pixelToLightUp, color);
strip.show();
delay(wipeDelay);
}
}
void wipeRow(int row, uint32_t color, int wipeDelay) {
for (int i = 0; i < 7; i++) {
int pixelToLightUp = matrix[row][i];
strip.setPixelColor(pixelToLightUp, color);
strip.show();
delay(wipeDelay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment