Conway’s game of Life displayed on an 8×8 LED matrix

Posted on September 27, 2010

1


(Reposted from my tumblr account)

Conway’s Game of Life on an 8×8 LED Matrix (powered by Processing, Arduinos and the good old serial connection 🙂

Processing code:

================================

/*
   1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
 2. Any live cell with more than three live neighbours dies, as if by overcrowding.
 3. Any live cell with two or three live neighbours lives on to the next generation.
 4. Any dead cell with exactly three live neighbours becomes a live cell.
 */

int[][][] world;
int dx, dy;
int state = 0;
int state_flip = 0;
int agespan = 6; // 3 seconds life step

import processing.serial.*;

Serial myPort;  // Create object from Serial class

int n = 0; // neighbours return var

int w = 8;
int h = 8;

void setup() {
  size(640,480);
  frameRate(15);
  dx = width/w;
  dy = height/h;
  world = new int[w][h][2];    // x, y - can be true/false held in new/old states
  // Initialise
  for (int i=0;i!=w;i++) {
    for (int j=0;j!=h;j++) {
      world[i][j][0] = 0;
      world[i][j][1] = 0;
    }
  }

  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  // glider
  world[0][0][0] = 1;
  world[1][0][0] = 1;
  world[2][0][0] = 1;
  world[2][1][0] = 1;
  world[1][2][0] = 1;
}

void drawcycle() {
  background(0);
  // draw cycle
  for (int i=0;i!=w;i++) {
    for (int j=0;j!=h;j++) {
      if (world[i][j][state] == 1) {
        rect(i*dx, j*dy, dx, dy);
      }
    }
  }
}
void draw() {
  drawcycle();
  state_flip = (state_flip + 1) % agespan;
  if (state_flip == 0) {
    for (int i=0;i!=w;i++) {
      byte x = 0x00;
      for (int j=0;j!=h;j++) {
        n = neighbours(i,j);
        if (world[i][j][state] == 1) {
          if (n<2) {
            // rule 1.
            world[i][j][(state+1)%2] = 0;
          }
          else if (n>3) {
            // rule 2.
            world[i][j][(state+1)%2] = 0;
          }
          else {
            world[i][j][(state+1)%2] = 1;
            x |= (1<<j);
          }
        }
        else {
          if (n==3) {
            world[i][j][(state+1)%2] = 1;
            x |= (1<<j);
          }
          else {
            world[i][j][(state+1)%2] = 0;
          }

        }
      }

      myPort.write(x);
    }
    state = (state + 1) % 2;
  }
}

int neighbours(int x, int y) {
  return world[(x + 1) % w][y][state] +
    world[x][(y + 1) % h][state] +
    world[(x + w - 1) % w][y][state] +
    world[x][(y + h - 1) % h][state] +
    world[(x + 1) % w][(y + 1) % h][state] +
    world[(x + w - 1) % w][(y + 1) % h][state] +
    world[(x + w - 1) % w][(y + h - 1) % h][state] +
    world[(x + 1) % w][(y + h - 1) % h][state];
}

void mouseReleased() {
  world[mouseX/dx][mouseY/dy][state] = 1;
  drawcycle();
}

================================

Arduino code

================================

int CLOCK = 12;

int LATCH = 13;

int DATA  = 11;

byte matrix[8];

byte var;

void setup() {

  pinMode(CLOCK, OUTPUT);

  pinMode(LATCH, OUTPUT);

  pinMode(DATA,  OUTPUT);

  digitalWrite(CLOCK, LOW);

  digitalWrite(LATCH, LOW);

  digitalWrite(DATA,  LOW);

  initLED();

  clearLED();

  Serial.begin(9600);

}

void loop() {

  if (Serial.available()) {

    clearLED();

    for (int i=0;i!=8;i++) {

      var = Serial.read();

      updateRow(var, i);

    }

    refreshLED();

  } 

}

void updateRow(byte var, int i) {

  for (int j=0;j!=8;j++) {

    if (var & (1<<j)) {

      updateLED(i,j,true);

    } 

    else {

      updateLED(i,j,false);

    }

  }

}

void ledOut(int n) {

  digitalWrite(LATCH, LOW);

  shiftOut(DATA, CLOCK, MSBFIRST, (n>>8));

  shiftOut(DATA, CLOCK, MSBFIRST, (n));

  digitalWrite(LATCH, HIGH);

  delay(1);

  digitalWrite(LATCH, LOW);

}

void initLED() {

  ledOut(0x0B07);

  ledOut(0x0A0C);

  ledOut(0x0900);

  ledOut(0x0C01);

}

void clearLED() {

  for (int i=0;i<8;i++) {

    matrix[i] = 0x00;

  }

  refreshLED();

}

void refreshLED() {

  int n1, n2, n3;

  for (int i=0;i<8;i++) {

    n1 = i+1;

    n2 = matrix[i];

    n3 = (n1<<8)+n2;

    ledOut(n3);

  }

}

void updateLED(int i, int j, boolean b) {

  int t = 1;

  int n = 0;

  int m = 0;

  if (j==0) {

    m = 7;

  }

  else {

    m = j-1;

  }

  n = t<<m;

  if (b) {

    matrix[i] = n | matrix[i];

  }

  else {

    n = ~n;

    matrix[i] = n & matrix[i];

  }

}
Advertisements
Posted in: Uncategorized