Last active
March 4, 2021 05:34
-
-
Save ogredude/004addc11cab65d1842ca4d60f4c74b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "FastLED.h" | |
// Matrix size | |
#define NUM_ROWS 16 | |
#define NUM_COLS 16 | |
// LEDs pin | |
#define DATA_PIN 3 | |
// LED brightness | |
#define BRIGHTNESS 180 | |
#define NUM_LEDS NUM_ROWS * NUM_COLS | |
// Define the array of leds | |
CRGB leds[NUM_LEDS]; | |
int board[NUM_COLS][NUM_ROWS]; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("Welcome to the Serial Playground!"); | |
Serial.println("---------------------------------"); | |
for (int i = 0; i < NUM_COLS; i++) { | |
for(int j = 0; j < NUM_ROWS; j++) { | |
board[i][j] = rand()%2; | |
} | |
} | |
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); | |
FastLED.setBrightness(BRIGHTNESS); | |
} | |
int counter = 0; | |
void loop() { | |
for (byte row = 0; row < NUM_ROWS; row++) { | |
for (byte col = 0; col < NUM_COLS; col++) { | |
int delta = abs(NUM_ROWS - row * 2) + abs(NUM_COLS - col * 2); | |
leds[row * NUM_COLS + col] = CHSV(delta * 4 + counter, 255, 255); | |
if (board[row][col] == 0) { | |
leds[row * NUM_COLS + col] = CRGB(255,255,255); | |
} | |
} | |
} | |
FastLED.show(); | |
gameOfLife(board); | |
delay(5000); | |
counter++; | |
} | |
void printBoard(int board[NUM_COLS][NUM_ROWS]) { | |
Serial.println("----------========== BOARD VIEW =============-----------"); | |
for (int i = 0; i < NUM_COLS; i++){ | |
char rowContent = ""; | |
for(int j = 0; j < NUM_COLS; j++) { | |
Serial.print("["); | |
Serial.print(i); | |
Serial.print(","); | |
Serial.print(j); | |
Serial.print(": "); | |
Serial.print(board[i][j]); | |
Serial.print("] "); | |
} | |
Serial.println(); | |
} | |
} | |
void gameOfLife(int board[NUM_COLS][NUM_ROWS]) { | |
int next[NUM_COLS][NUM_ROWS] = {0}; | |
printBoard(next); | |
int dirs[8][2] = { | |
{NUM_COLS -1,NUM_ROWS -1}, | |
{NUM_COLS-1,0}, | |
{NUM_COLS-1,1}, | |
{0,NUM_ROWS -1}, | |
{0,1}, | |
{1,NUM_ROWS -1}, | |
{1,0}, | |
{1,1} | |
}; | |
for (int i = 0; i < NUM_COLS; i++) { | |
for (int j = 0; j < NUM_ROWS; j++) { | |
int liveCount = 0; | |
for (auto dir : dirs) { | |
int x = (dir[0] + i) % i; | |
int y = (dir[1] + j) % j; | |
if (board[x][y] == 1) { | |
liveCount++; | |
} | |
} | |
if (board[i][j] == 0 && liveCount == 3) { | |
next[i][j] = 1; | |
} else if (board[i][j] == 1) { | |
if (liveCount == 3 || liveCount == 2) { | |
next[i][j] = 1; | |
} | |
} | |
} | |
} | |
for(int i = 0; i < NUM_COLS; i++) { | |
for(int j = 0; j < NUM_ROWS; j++) { | |
board[i][j] = next[i][j]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment