This site is archival. For maintained and responsive content, visit efuller.net.
© 2008 Elizabeth

Physical Computing Final: Life (week 1, Monday)

Earlier in Life

I have already written code to run the game of life on Arduino and collected materials for the electronics. Now, I need to adjust the code so that it is writing out to the LEDs.

This week in Life

Monday

Today I wrote a blinking function so that I can adjust the rest of my code to control an array of 4 lights rather than printing out to Processing. The Game of Life really doesn’t work with only two cells. With the blinking function, I can alter the code so that it is turning LEDs on and off and set up the LEDs so that their coordinates are held in an array and adjusted through it. This will hopefully aid my move to using shift registers.


//derrived from:

//Daniel Shiffman, Nature of Code, 3/1/2005
//http://stage.nyu.edu/nature

//a basic implementation of John Conway’s Game of Life CA

int cellsize = 2;
const int COLS = 2;
const int ROWS = 2;
//game of life board
boolean old_board[COLS][ROWS];
boolean new_board[COLS][ROWS];
boolean lights[COLS][ROWS]; // array of light locations

void setup()
{
// configure the serial connection:
Serial.begin(9600);
initBoard();
}

void loop()
{
//calculate();
blink();
while(Serial.available() > 0) {
int xToLive = Serial.read();
int yToLive = Serial.read();
new_board[xToLive][yToLive] = 1;
}
sendData();
delay(1000);
}

void blink()
{
for (int x = 0; x < COLS;x++) {
for (int y = 0; y < ROWS;y++) {
if (old_board[x][y] == 0) {
new_board[x][y] = 1;
old_board[x][y] = 1;}
else {
new_board[x][y] = 0;
old_board[x][y] = 0; }
}
}

}

void calculate()
{

int alive = 1;
int dead = 0;
//loop through every spot in our 2D array and check spots neighbors
for (int x = 0; x < COLS;x++) {
for (int y = 0; y < ROWS;y++) {
int nb = 0;
//Note the use of mod (“%”) below to ensure that cells on the edges have “wrap-around” neighbors
//above row
if (old_board[(x+COLS-1) % COLS ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
if (old_board[ x ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
//middle row
if (old_board[(x+COLS-1) % COLS ][ y ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][ y ] == 1) { nb++; }
//bottom row
if (old_board[(x+COLS-1) % COLS ][(y+1) % ROWS ] == 1) { nb++; }
if (old_board[ x ][(y+1) % ROWS ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][(y+1) % ROWS ] == 1) { nb++; }

//RULES OF “LIFE” HERE
if ((old_board[x][y] == 1) && (nb < 2)) { new_board[x][y] = 0; } //loneliness
else if ((old_board[x][y] == 1) && (nb > 3)) { new_board[x][y] = 0; } //overpopulation
else if ((old_board[x][y] == 0) && (nb == 3)) { new_board[x][y] = 1; } //reproduction
else { new_board[x][y] = old_board[x][y]; } //stasis
}
}
}
void sendData()
{
int ctr = 0;
//RENDER game of life based on “new_board” values
for ( int i = 0; i < COLS; i++) {
for ( int j = 0; j < ROWS; j++) {
// write old board like new board
old_board[i][j] = new_board[i][j];
// send board settings to Processing
if ((new_board[i][j] == 1)) {
// send living cell
Serial.print(1, DEC);
Serial.print(“,”);
digitalWrite(lights[i][j], HIGH);
}
else
{
// send empty cell
Serial.print(0, DEC);
Serial.print(“,”);
digitalWrite(lights[i][j], LOW);
}
ctr++;
}
}
Serial.print(“\n”);
}

//init board with random “alive” squares
void initBoard() {
int ctr = 2;
//Serial.println(“call initBoard”);
for (int i =0;i < COLS;i++) {
for (int j =0;j < ROWS;j++) {
if (int(random(2)) == 0) {
old_board[i][j] = 1;
} else {
old_board[i][j] = 0;
}
lights[i][j] = ctr; // set array value for pin number
pinMode(ctr, OUTPUT); // set LED pin for output
Serial.println(ctr, DEC);
ctr++;
}
}
}

Next: shift registers

Related posts:

  1. Life Dress: LED Drivers and Lilypads The Life Dress has a maintained page at efuller.net/life-dress-f2. LED...
  2. Physical Computing Final: Life (week 1, Tuesday) Today I have been working with shift registers. I found...
  3. Life Dress [Matrix] The Life Dress has a maintained page at efuller.net/life-dress-f3. After...
  4. Life Dress [Multiplexers v2] The Life Dress has a maintained page at efuller.net/life-dress-f3. Contents...
  5. Physical Computing Final Proposal: Life Concept Inspiration strikes in the strangest of places. Classically...

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>