Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // game of life
- // fastled 16x16 matrix demo
- // Yaroslaw Turbin 18.08.2020
- // based on sketch https://github.com/CodingTrain/website/blob/master/CodingChallenges/CC_085_The_Game_of_Life/P5/sketch.js
- //
- #include "FastLED.h"
- // Matrix size
- #define NUM_ROWS 16
- #define NUM_COLS 16
- // LEDs pin
- #define DATA_PIN 3
- // LED brightness
- #define BRIGHTNESS 255
- #define NUM_LEDS NUM_ROWS * NUM_COLS
- // Define the array of leds
- CRGB leds[NUM_LEDS];
- byte grid [NUM_LEDS];
- byte next [NUM_LEDS];
- byte counter = 0;
- void setup() {
- FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS);
- random16_set_seed(4832); // Awesome randomizer
- random16_add_entropy(analogRead(2));
- int ranstart = random16();
- }
- void loop() {
- if (counter == 0) { initarr ();
- counter=1; }
- EVERY_N_MILLISECONDS(200) {
- updatelife();
- FastLED.show();
- }
- EVERY_N_MILLISECONDS(50000) {
- for (byte i = 0; i < 50; i++) {
- fadeToBlackBy(leds, NUM_LEDS, 255*i/49);
- FastLED.delay(50);
- }
- initarr ();
- }
- }
- void updatelife () {
- for (byte i = 0; i < NUM_COLS; i++) {
- for (byte j = 0; j < NUM_ROWS; j++) {
- byte state = grid[XY(i,j)];
- byte neighbors = countNeighbors(i, j);
- if (state == 0 && neighbors == 3) {
- next[XY(i,j)] = 1;
- } else if (state == 1 && (neighbors < 2 || neighbors > 3)) {
- next[XY(i,j)] = 0;
- } else {
- next[XY(i,j)] = state;
- }
- }
- }
- for (int i = 0; i < NUM_LEDS; i++) {
- grid [i] = next [i];
- leds [i] = CHSV(grid [i] , 255, grid [i]*BRIGHTNESS);
- }
- }
- uint16_t XY (uint8_t x, uint8_t y) { return (y * NUM_COLS + x);}
- byte countNeighbors(byte x, byte y) {
- int sum = 0;
- for (int i = -1; i < 2; i++) {
- for (int j = -1; j < 2; j++) {
- int col = (x + i + NUM_COLS) % NUM_COLS;
- int row = (y + j + NUM_ROWS) % NUM_ROWS;
- sum += grid[XY(col,row)];
- }
- }
- sum -= grid[XY(x,y)];
- return sum;
- }
- void initarr () {
- for (int i = 0; i < NUM_LEDS; i++) {
- grid[i] = random8(2);
- }
- }
Add Comment
Please, Sign In to add comment