#include <FastLED.h>
#define LED_PIN 3
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define BRIGHTNESS 255
// Params for width and height
const uint8_t kMatrixWidth = 32;
const uint8_t kMatrixHeight = 32;
// Param for different pixel layouts
const bool kMatrixSerpentineLayout = true;
CRGB leds_plus_safety_pixel[kMatrixWidth * kMatrixHeight];
CRGB* const leds = leds_plus_safety_pixel;
void setup() {
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, kMatrixWidth * kMatrixHeight).setCorrection(TypicalSMD5050);
FastLED.setBrightness(BRIGHTNESS);
// Seed the random number generator with an analog pin's reading
randomSeed(analogRead(0));
// Set LEDs randomly to CRGB::DarkBlue or CRGB::Black
for (int x = 0; x < kMatrixWidth; x++) {
for (int y = 0; y < kMatrixHeight; y++) {
if (random(2) == 0) {
leds[XY(x, y)] = CRGB::DarkBlue;
} else {
leds[XY(x, y)] = CRGB(255, 255, 0);
}
}
}
}
void loop() {
// Buffer to hold the next state of the LEDs
boolean next_state[kMatrixWidth][kMatrixHeight];
// Calculate the next state based on the current state
for (int x = 0; x < kMatrixWidth; x++) {
for (int y = 0; y < kMatrixHeight; y++) {
int neighbors = countNeighbors(x, y);
// Apply the rules of Conway's Game of Life
if (leds[XY(x, y)] == CRGB::DarkBlue && (neighbors < 2 || neighbors > 3)) {
next_state[x][y] = false;
} else if (leds[XY(x, y)] == CRGB::DarkBlue && (neighbors == 2 || neighbors == 3)) {
next_state[x][y] = true;
} else if (leds[XY(x, y)] == CRGB::Black && neighbors == 3) {
next_state[x][y] = true;
} else {
next_state[x][y] = false;
}
}
}
// Update the LEDs based on the next state
for (int x = 0; x < kMatrixWidth; x++) {
for (int y = 0; y < kMatrixHeight; y++) {
leds[XY(x, y)] = next_state[x][y] ? CRGB::DarkBlue : CRGB::Black;
}
}
// Show the LEDs
FastLED.show();
// Introduce a delay to control the speed of the animation
delay(1);
}
// Function to count the number of live neighbors around a cell
int countNeighbors(int x, int y) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int nx = (x + i + kMatrixWidth) % kMatrixWidth;
int ny = (y + j + kMatrixHeight) % kMatrixHeight;
if (leds[XY(nx, ny)] == CRGB::DarkBlue && !(i == 0 && j == 0)) {
count++;
}
}
}
return count;
}
// Function to map 2D coordinates to the LED strip
uint16_t XY(uint8_t x, uint8_t y) {
uint16_t i;
if (kMatrixSerpentineLayout == false) {
i = (y * kMatrixWidth) + x;
}
if (kMatrixSerpentineLayout == true) {
if (y & 0x01) {
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
} else {
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
return i;
}
FPS: 0
Power: 0.00W
Power: 0.00W