Introduction: Arduino Button Game

About: Technology enthusiastic, crazy about embedded system, love Arduino and raspberry pi.

This is a LED game of two players.

Check out the video.

Step 1: Parts List

1x Arduino nano

2x Push button

12x 220 Ohms resistor

5x Red LED (Your wish)

5x Green LED (Your wish)

1x Breadboard or stripboard, as you prefer

1x USB Cable

and connecting wires.

Step 2: Circuit Diagram

This is very simple circuit.

Step 3: Code

/*

* Arduino Button Game

*

* Developed by Mohammed Adil

* B.Tech 3rd sem

* Student at Lovely Professional University

* */

int led[5]={4,5,6,7,8}; //LEDs for 1st player

int led_1[5]={9,10,11,12,13};//LEDs for 2nd player

int i=0;

int j=0;

int button_1=0;//Button input for player 1

int button_2=0;//Button input for player 2

void setup() {

//INPUT Pin mode

for(int thispin=4;thispin<14;thispin++)

{

pinMode(thispin,OUTPUT);

}

//OUTPUT Pin mode

pinMode(2,INPUT);

pinMode(3,INPUT);

}

void loop()

{

button_1=digitalRead(2); //Read initial value of player 1

button_2=digitalRead(3); //Read initial value of player 2

if(button_1==HIGH) {

do

{

digitalWrite(led[i],HIGH);

}while(digitalRead(2)==HIGH);

if(i<4)

{

i++;

}

else if(i==4)

{

for(int thispin=4;thispin<9;thispin++)

{

digitalWrite(thispin,LOW);

}

delay(200);

for(int thispin=4;thispin<9;thispin++)

{

digitalWrite(thispin,HIGH);

}

delay(200);

for(int thispin=4;thispin<9;thispin++)

{

digitalWrite(thispin,LOW);

}

delay(200);

for(int thispin=4;thispin<9;thispin++)

{

digitalWrite(thispin,HIGH);

}

delay(200);

for(int thispin=4;thispin<14;thispin++)

{

digitalWrite(thispin,LOW);

}

j=0;

i=0;

}

else

{

i=0;

}

}

if(button_2==HIGH)

{

do

{

digitalWrite(led_1[j],HIGH);

}while(digitalRead(3)==HIGH);

if(j<4)

{

j++;

}

else if(j==4)

{

for(int thispin=9;thispin<14;thispin++)

{

digitalWrite(thispin,LOW);

}

delay(200);

for(int thispin=9;thispin<14;thispin++)

{

digitalWrite(thispin,HIGH);

}

delay(200);

for(int thispin=9;thispin<14;thispin++)

{

digitalWrite(thispin,LOW);

}

delay(200);

for(int thispin=9;thispin<14;thispin++)

{

digitalWrite(thispin,HIGH);

}

delay(200);

for(int thispin=4;thispin<14;thispin++)

{

digitalWrite(thispin,LOW);

}

i=0;

j=0;

}

else

{

j=0;

}

}

}

Download the source code.

Step 4: Rules to Play the Game

There are two buttons one for each player. Both player should press their respective buttons in order to complete switching of their five LED s. The one who turn on their all five LED s before other will be declared as winner... all LED s of winner will start blinking which will indicate the winning state...