game of life and push Botton

Hi
I wondering if some could guide to
add a push botton to this game of life code

#include "LedControl.h"

LedControl lc=LedControl(12,11,10,1);

#define Largeur 8
#define Hauteur 8


unsigned long delaytime=150;

int idx=0;
 int t1[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 t2[8][8];
/*******************************************************************************/
/* set all the box to zero*/

void mise_a_0(int t1[8][8],int t2[8][8])
{
  int i,j;
  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	t1[i][j]=0;
	t2[i][j]=0;
    }
  }
}

/*******************************************************************************/
/* clear window */

void clear_fenetre()
{
  lc.clearDisplay(0);
}

/*******************************************************************************/
/* display cells */

void affiche(int t1[8][8])
{
  int i,j;
  //lc.clearDisplay(0);
  //_setcursortype(_NOCURSOR);
  //textcolor(14);
  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	lc.setLed(0,i,j,t1[i][j]);

    }
  }
}

/*******************************************************************************/
/* compute previous generation*/

void affect_etat_precedent(int t1[8][8],int t2[8][8])
{
  int i,j;

  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	t2[i][j]=t1[i][j];
    }
  }
}

/*******************************************************************************/
/* compute next generation */

void affect_etat_suivant(int t1[8][8],int t2[8][8])
{
  int i,j;

  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	t1[i][j]=t2[i][j];
    }
  }
}

/*******************************************************************************/
/* compute neighborough cells */
#
void calcul_cellule(int t1[8][8],int t2[8][8])
{
  int i,j,a;

  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	if((i==0)&&(j==0))
	{
	  a=t1[i][j+1]+t1[i+1][j]+t1[i+1][j+1]+t1[i][69]+t1[i+1][69]+t1[19][j]+t1[19][j+1]+t1[19][69];
	}

	if((i!=0)&&(j!=0)&&(i!=(Largeur-1))&&(j!=(Hauteur-1)))
	{
	  a=t1[i-1][j-1]+t1[i-1][j]+t1[i-1][j+1]+t1[i][j+1]+t1[i+1][j+1]+t1[i+1][j]+t1[i+1][j-1]+t1[i][j-1];
	}

	if((i==0)&&(j!=0)&&(j!=(Hauteur-1)))
	{
	  a=t1[i][j-1]+t1[i+1][j-1]+t1[i+1][j]+t1[i+1][j+1]+t1[i][j+1]+t1[19][j-1]+t1[19][j]+t1[19][j+1];
	}

	if((i==0)&&(j==(Hauteur-1)))
	{
	  a=t1[i][j-1]+t1[i+1][j-1]+t1[i+1][j]+t1[i][0]+t1[i+1][0]+t1[19][0]+t1[19][j]+t1[19][j-1];
	}

	if((i==(Largeur-1))&&(j==0))
	{
	  a=t1[i-1][j]+t1[i-1][j+1]+t1[i][j+1]+t1[i][69]+t1[i-1][69]+t1[0][j]+t1[0][j+1]+t1[0][69];
	}

	if((i==(Largeur-1))&&(j!=0)&&(j!=(Hauteur-1)))
	{
	  a=t1[i][j-1]+t1[i][j+1]+t1[i-1][j-1]+t1[i-1][j]+t1[i-1][j+1]+t1[0][j]+t1[0][j-1]+t1[0][j+1];
	}

	if((i==(Largeur-1))&&(j==(Hauteur-1)))
	{
	  a=t1[i][j-1]+t1[i-1][j-1]+t1[i-1][j]+t1[0][j]+t1[0][j-1]+t1[i][0]+t1[i-1][0]+t1[0][0];
	}

	if((i!=0)&&(i!=(Largeur-1))&&(j==0))
	{
	  a=t1[i-1][j]+t1[i-1][j+1]+t1[i][j+1]+t1[i+1][j+1]+t1[i+1][j]+t1[i][69]+t1[i-1][69]+t1[i+1][69];
	}

	if((i!=0)&&(i!=(Largeur-1))&&(j==(Hauteur-1)))
	{
	  a=t1[i-1][j]+t1[i-1][j-1]+t1[i][j-1]+t1[i+1][j-1]+t1[i+1][j]+t1[i][0]+t1[i-1][0]+t1[i+1][0];
	}

	if((t1[i][j]==0)&&(a==3)){t2[i][j]=1;}
	if((t1[i][j]==1)&&((a==2)||(a==3))){t2[i][j]=1;}
	if((t1[i][j]==1)&&((a==1)||(a==0)||(a>3))){t2[i][j]=0;}
    }
  }
}

/*******************************************************************************/
/* randomize */

void alea(int t1[8][8])
{
  int i,j;
  randomSeed(millis());
  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	t1[i][j]=random(2);
    }
  }
}

/*******************************************************************************/
/*permutation*/

void permut(int *a,int *b)
{
  int aux;
  aux=*a;
  *a=*b;
  *b=aux;
}





void setup() {
   lc.shutdown(0,false);
   lc.setIntensity(0,15);
   lc.clearDisplay(0);
  // alea(t1);
}


void loop()
{

   if (idx++ > 30)
   {
     alea(t1);

     lc.clearDisplay(0);
     delay(300);
     idx=0;
   }

    affect_etat_precedent(t1,t2);
    calcul_cellule(t1,t2);
    affect_etat_suivant(t1,t2);
    affiche(t1);
    delay(delaytime);


}

original code from

http://forum.arduino.cc//index.php?PHPSESSID=hn7s31bm96cosm8uj44jeu81v0&topic=92787.0

If you want the button to do anything you should probably say exactly what you want it to do.

John is right. ;).
If you know what you want to Do with the button,
you should check for a change of button state, and maybe jump to function , in which you say what to do.try the button debouncing tutorial, then put together a function where you maybe reset the game stage. Cheers Andre

Thanks John and xlr8_at

I just want to pushbotton to start the game of life while the botton is push
and when is not no game of life.

Got it working
here is code if some one needs it
hardware setup has a pushbotton on digital 2

#include "LedControl.h"





const int buttonPin = 2;     // 
int buttonState = 0;

LedControl lc=LedControl(12,11,10,1);

#define Largeur 8
#define Hauteur 8

int analogPin = 0;
unsigned long delaytime=150;

int idx=0;
 int t1[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 t2[8][8];
/*******************************************************************************/
/* set all the box to zero*/

void mise_a_0(int t1[8][8],int t2[8][8])
{
  int i,j;
  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	t1[i][j]=0;
	t2[i][j]=0;
    }
  }
}

/*******************************************************************************/
/* clear window */

void clear_fenetre()
{
  lc.clearDisplay(0);
}

/*******************************************************************************/
/* display cells */

void affiche(int t1[8][8])
{
  int i,j;
  //lc.clearDisplay(0);
  //_setcursortype(_NOCURSOR);
  //textcolor(14);
  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	lc.setLed(0,i,j,t1[i][j]);

    }
  }
}

/*******************************************************************************/
/* compute previous generation*/

void affect_etat_precedent(int t1[8][8],int t2[8][8])
{
  int i,j;

  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	t2[i][j]=t1[i][j];
    }
  }
}

/*******************************************************************************/
/* compute next generation */

void affect_etat_suivant(int t1[8][8],int t2[8][8])
{
  int i,j;

  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	t1[i][j]=t2[i][j];
    }
  }
}

/*******************************************************************************/
/* compute neighborough cells */
#
void calcul_cellule(int t1[8][8],int t2[8][8])
{
  int i,j,a;

  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	if((i==0)&&(j==0))
	{
	  a=t1[i][j+1]+t1[i+1][j]+t1[i+1][j+1]+t1[i][69]+t1[i+1][69]+t1[19][j]+t1[19][j+1]+t1[19][69];
	}

	if((i!=0)&&(j!=0)&&(i!=(Largeur-1))&&(j!=(Hauteur-1)))
	{
	  a=t1[i-1][j-1]+t1[i-1][j]+t1[i-1][j+1]+t1[i][j+1]+t1[i+1][j+1]+t1[i+1][j]+t1[i+1][j-1]+t1[i][j-1];
	}

	if((i==0)&&(j!=0)&&(j!=(Hauteur-1)))
	{
	  a=t1[i][j-1]+t1[i+1][j-1]+t1[i+1][j]+t1[i+1][j+1]+t1[i][j+1]+t1[19][j-1]+t1[19][j]+t1[19][j+1];
	}

	if((i==0)&&(j==(Hauteur-1)))
	{
	  a=t1[i][j-1]+t1[i+1][j-1]+t1[i+1][j]+t1[i][0]+t1[i+1][0]+t1[19][0]+t1[19][j]+t1[19][j-1];
	}

	if((i==(Largeur-1))&&(j==0))
	{
	  a=t1[i-1][j]+t1[i-1][j+1]+t1[i][j+1]+t1[i][69]+t1[i-1][69]+t1[0][j]+t1[0][j+1]+t1[0][69];
	}

	if((i==(Largeur-1))&&(j!=0)&&(j!=(Hauteur-1)))
	{
	  a=t1[i][j-1]+t1[i][j+1]+t1[i-1][j-1]+t1[i-1][j]+t1[i-1][j+1]+t1[0][j]+t1[0][j-1]+t1[0][j+1];
	}

	if((i==(Largeur-1))&&(j==(Hauteur-1)))
	{
	  a=t1[i][j-1]+t1[i-1][j-1]+t1[i-1][j]+t1[0][j]+t1[0][j-1]+t1[i][0]+t1[i-1][0]+t1[0][0];
	}

	if((i!=0)&&(i!=(Largeur-1))&&(j==0))
	{
	  a=t1[i-1][j]+t1[i-1][j+1]+t1[i][j+1]+t1[i+1][j+1]+t1[i+1][j]+t1[i][69]+t1[i-1][69]+t1[i+1][69];
	}

	if((i!=0)&&(i!=(Largeur-1))&&(j==(Hauteur-1)))
	{
	  a=t1[i-1][j]+t1[i-1][j-1]+t1[i][j-1]+t1[i+1][j-1]+t1[i+1][j]+t1[i][0]+t1[i-1][0]+t1[i+1][0];
	}

	if((t1[i][j]==0)&&(a==3)){t2[i][j]=1;}
	if((t1[i][j]==1)&&((a==2)||(a==3))){t2[i][j]=1;}
	if((t1[i][j]==1)&&((a==1)||(a==0)||(a>3))){t2[i][j]=0;}
    }
  }
}

/*******************************************************************************/
/* randomize */

void alea(int t1[8][8])
{
  int i,j;
  randomSeed(millis());
  for(i=0;i<Largeur;i++)
  {
    for(j=0;j<Hauteur;j++)
    {
	t1[i][j]=random(2);
    }
  }
}

/*******************************************************************************/
/*permutation*/

void permut(int *a,int *b)
{
  int aux;
  aux=*a;
  *a=*b;
  *b=aux;
}





void setup() {
  
  
  
   
   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input:
  
  
  
   lc.shutdown(0,false);
   lc.setIntensity(0,15);
   lc.clearDisplay(0);
  // alea(t1);
}



///////loop fun display is hrere////


void loop()
{

  
  buttonState = digitalRead(buttonPin);  // read the state of the pbutton value:


if (buttonState == HIGH){
// -- 
   if (idx++ > 30)
   {
     alea(t1);

     lc.clearDisplay(0);
     delay(300);//was 300
     idx=0;
   }

    affect_etat_precedent(t1,t2);
    calcul_cellule(t1,t2);
    affect_etat_suivant(t1,t2);
    affiche(t1);
    delay(delaytime);


}
// --

else {
  
  
  lc.clearDisplay(0);
     delay(100);//was to 300
     idx=0;

}
}
int buttonState = 0;
  buttonState = digitalRead(buttonPin);  // read the state of the pbutton value:
if (buttonState == HIGH){
// code
}

You don't need to create a variable if you aren't going to use the value more than once. You can shorten your code to:

if (digitalRead(buttonPin) == HIGH) {
// code
}

Since digitalRead() returns 0 (LOW) or 1 (HIGH) those can be used directly as 'false' and 'true':

if (digitalRead(buttonPin) ) {
// code
}

Thanks you for the advice
:smiley: