Hello. I'm about half done with a project of making a 8x8 LED matrix and controlling it with a MAX7219 IC. I want it to run Conway's Game of Life and I'll make it into a wall hanging. I have tinkered with some code I found and got it mostly working. Can someone look over the code and tell me if (and how) it can be improved. I noticed that (in my video) the lower left LED if on, stays on till it starts over, when it looks as if it should be blinking with the others, and I don't know why. So I'm just looking for some advice. Much thanks. Here is my code and a link to a short video.
//Game of Life with MAX7219
/*
* dataPin pin on the Arduino where data gets shifted out
* clockPin pin for the clock
* csPin pin for selecting the device
* numDevices maximum number of devices that can be controled
LedControl(int dataPin, int clkPin, int csPin, int numDevices=1);
*/
/*******************************************************************************/
#include "LedControl.h" //Imports the library
LedControl lc=LedControl(12,11,10,1);
#define SIZEX 8 //Sets the X axis size
#define SIZEY 8 //Sets the Y axis size
#define reseedrate 30 //Sets the rate the world is re-seeded
#define brightness 15 //Values from 1 to 15 to set the brightness
long density = 35; //Sets density % during seeding
unsigned long delaytime=150; //Sets the time each generation is shown
int generation = 0; //Counter for re-seeding
int analogPin = 0; //analogPin for the random seed data
int world1[8][8] = {{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0},
{0, 1, 0, 1, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 0, 1, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0}};
int world2[8][8];
/*******************************************************************************/
/* display cells */
void display_cells(int world1[8][8])
{
int i,j;
for(i=0;i<SIZEX;i++)
{
for(j=0;j<SIZEY;j++)
{
lc.setLed(0,i,j,world1[i][j]);
}
}
}
/*******************************************************************************/
/* compute previous generation*/
void previous_generation(int world1[8][8],int world2[8][8])
{
int i,j;
for(i=0;i<SIZEX;i++)
{
for(j=0;j<SIZEY;j++)
{
world2[i][j]=world1[i][j];
}
}
}
/*******************************************************************************/
/* compute next generation */
void next_generation(int world1[8][8],int world2[8][8])
{
int i,j;
for(i=0;i<SIZEX;i++)
{
for(j=0;j<SIZEY;j++)
{
world1[i][j]=world2[i][j];
}
}
}
/*******************************************************************************/
/* compute neighborough cells */
#
void neighbours(int world1[8][8],int world2[8][8])
{
int i,j,a;
for(i=0;i<SIZEX;i++)
{
for(j=0;j<SIZEY;j++)
{
if((i==0)&&(j==0))
{
a=world1[i][j+1]+world1[i+1][j]+world1[i+1][j+1]+world1[i][69]+world1[i+1][69]+world1[19][j]+world1[19][j+1]+world1[19][69];
}
if((i!=0)&&(j!=0)&&(i!=(SIZEX-1))&&(j!=(SIZEY-1)))
{
a=world1[i-1][j-1]+world1[i-1][j]+world1[i-1][j+1]+world1[i][j+1]+world1[i+1][j+1]+world1[i+1][j]+world1[i+1][j-1]+world1[i][j-1];
}
if((i==0)&&(j!=0)&&(j!=(SIZEY-1)))
{
a=world1[i][j-1]+world1[i+1][j-1]+world1[i+1][j]+world1[i+1][j+1]+world1[i][j+1]+world1[19][j-1]+world1[19][j]+world1[19][j+1];
}
if((i==0)&&(j==(SIZEY-1)))
{
a=world1[i][j-1]+world1[i+1][j-1]+world1[i+1][j]+world1[i][0]+world1[i+1][0]+world1[19][0]+world1[19][j]+world1[19][j-1];
}
if((i==(SIZEX-1))&&(j==0))
{
a=world1[i-1][j]+world1[i-1][j+1]+world1[i][j+1]+world1[i][69]+world1[i-1][69]+world1[0][j]+world1[0][j+1]+world1[0][69];
}
if((i==(SIZEX-1))&&(j!=0)&&(j!=(SIZEY-1)))
{
a=world1[i][j-1]+world1[i][j+1]+world1[i-1][j-1]+world1[i-1][j]+world1[i-1][j+1]+world1[0][j]+world1[0][j-1]+world1[0][j+1];
}
if((i==(SIZEX-1))&&(j==(SIZEY-1)))
{
a=world1[i][j-1]+world1[i-1][j-1]+world1[i-1][j]+world1[0][j]+world1[0][j-1]+world1[i][0]+world1[i-1][0]+world1[0][0];
}
if((i!=0)&&(i!=(SIZEX-1))&&(j==0))
{
a=world1[i-1][j]+world1[i-1][j+1]+world1[i][j+1]+world1[i+1][j+1]+world1[i+1][j]+world1[i][69]+world1[i-1][69]+world1[i+1][69];
}
if((i!=0)&&(i!=(SIZEX-1))&&(j==(SIZEY-1)))
{
a=world1[i-1][j]+world1[i-1][j-1]+world1[i][j-1]+world1[i+1][j-1]+world1[i+1][j]+world1[i][0]+world1[i-1][0]+world1[i+1][0];
}
if((world1[i][j]==0)&&(a==3)){world2[i][j]=1;}
if((world1[i][j]==1)&&((a==2)||(a==3))){world2[i][j]=1;}
if((world1[i][j]==1)&&((a==1)||(a==0)||(a>3))){world2[i][j]=0;}
}
}
}
/*******************************************************************************/
/* randomize */
void seedWorld(int world1[8][8])
{
int i,j;
randomSeed(analogRead(analogPin));
for(i=0;i<SIZEX;i++)
{
for(j=0;j<SIZEY;j++)
{
if (random(100) < density); {
world1[i][j]=random(2);
}
}
}
}
/*******************************************************************************/
void setup() {
lc.shutdown(0,false);
lc.setIntensity(0,brightness);
lc.clearDisplay(0);
seedWorld(world1);
}
/*******************************************************************************/
void loop()
{
if (generation++ > reseedrate)
{
seedWorld(world1);
lc.clearDisplay(0);
delay(300);
generation=0;
}
previous_generation(world1,world2);
neighbours(world1,world2);
next_generation(world1,world2);
display_cells(world1);
delay(delaytime);
}