Skip to content

Instantly share code, notes, and snippets.

@remcoder
Last active September 23, 2016 12:02

Revisions

  1. remcoder revised this gist Jul 16, 2013. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions game of life with 8x8 bicolor led matrix
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,17 @@
    /*
    I recently got one of those 8x8 LED matrices and I was playing with some Game of Life patterns when I found this pretty repeating pattern. I found it by starting with some random patterns. If you look closely you can see the pattern becoming a mirrored version of itself halfway through. Apparently the pattern doesn't repeat like this on an infinite grid but on this wrapping 8x8 grid it does ;-)

    FYI, the LED matrix is a bicolor one (green/red) and has an I2C interface (http://www.adafruit.com/products/902). I'm using the colors as follows:
    - newly created cells are green
    - cells that are at least 10 generations old are red
    - other living cells are yellow (simultaneously green+red)

    It's hookup up to my Arduino Uno r3.

    here's a video: http://www.youtube.com/watch?v=Ee2hOaQ2RDI

    */

    #include <Wire.h>
    #include "Adafruit_LEDBackpack.h"
    #include "Adafruit_GFX.h"
  2. remcoder revised this gist Jul 16, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion game of life with 8x8 bicolor led matrix
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #include <Wire.h>
    #include "Adafruit_LEDBackpack.h"
    #include "Adafruit_GFX.h"
  3. remcoder created this gist Jul 16, 2013.
    96 changes: 96 additions & 0 deletions game of life with 8x8 bicolor led matrix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@

    #include <Wire.h>
    #include "Adafruit_LEDBackpack.h"
    #include "Adafruit_GFX.h"

    boolean cells[8][8];

    Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();

    // game of life
    int next[8][8];

    void setup() {
    Serial.begin(9600);
    Serial.write("hello");

    randomSeed(analogRead(0));
    for (int r=0 ; r<8 ; r++) {
    for (int c=0 ; c<8 ; c++) {
    if (random(2) >0)
    next[r][c] = 1;
    }
    }

    matrix.begin(0x70); // pass in the address
    }

    void loop() {

    game_of_life();
    }

    int current[8][8] =
    { {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,1,1,1,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0} };

    int mod(int a) { return (a+8)%8; }

    void game_of_life() {

    matrix.clear();

    // draw
    for (int r=0 ; r<8 ; r++) {
    for (int c=0 ; c<8 ; c++) {
    int color;
    if (next[r][c] == 0)
    color = 0;
    else if (next[r][c] == 1)
    color = LED_GREEN;
    else if (next[r][c] > 10)
    color = LED_RED;
    else
    color = LED_YELLOW;
    matrix.drawPixel(c,r,color);
    }
    }
    matrix.writeDisplay();

    // calc next state
    for (int r=0 ; r<8 ; r++) {
    for (int c=0 ; c<8 ; c++) {
    // count alive neighbors
    int alive = 0;
    alive += current[mod(r+1)][mod(c) ] != 0;
    alive += current[mod(r) ][mod(c+1)] != 0;
    alive += current[mod(r-1)][mod(c) ] != 0;
    alive += current[mod(r) ][mod(c-1)] != 0;
    alive += current[mod(r+1)][mod(c+1)] != 0;
    alive += current[mod(r-1)][mod(c-1)] != 0;
    alive += current[mod(r+1)][mod(c-1)] != 0;
    alive += current[mod(r-1)][mod(c+1)] != 0;
    if (current[r][c])
    if (alive < 2 || alive > 3)
    next[r][c] = 0;
    else
    next[r][c] = current[r][c] + 1;
    else
    if (alive == 3)
    next[r][c] = 1;
    }
    }

    for (int r=0 ; r<8 ; r++) {
    for (int c=0 ; c<8 ; c++) {
    current[r][c] = next[r][c];
    }
    }
    delay(100);
    }