Project tutorial
Arduino Controlled LED Multi-Player Game on Arduino "Shield"

Arduino Controlled LED Multi-Player Game on Arduino "Shield" © GPL3+

A custom made PCB fits onto an Arduino Uno. This is a game that can be played by two players.

  • 1,799 views
  • 2 comments
  • 8 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

This project was inspired by a YouTube video from Jabrils.

Jabrils' YouTube video

This project is basically a replica of his, though he used a Raspberry Pi and Python to code it. Here are a few pictures of the finished product:

How to build

The green PCB on top of the Arduino was designed with EasyEda and I ordered it with JLCPCB, scroll down for a download of the Gerber file.

Once the PCB arrives you should solder all the components on with a soldering iron and solder. It should then be able to slot onto an Arduino just like a shield.

TIP: It is recommended for you to straighten the pins of the push buttons with any tool before trying to insert it into the PCB. The value of the resistors are also printed on their respective spaces (3 x 10K, 1 x 220).

Once you have built the whole board and put it onto the Arduino, you can download the code from below, or just copy and paste it into Arduino IDE. Upload the code onto the Arduino via a USB cable. Now that the code is saved, the usb cable can be swapped for a 2.1 mm DC power cable as the program is already saved onto the Arduino flash memory, though powering the Arduino with the USB cable is also fine.

How to play

When the program first starts the lights will be flashing back and forth very slowly, keep in mind that the green LEDs might be very dim. Right now the game is in the main menu state. To exit this and start the game, hold any or both of the two buttons in a row with the LEDs for 5 seconds (5 light flashes). All the lights will turn off, then they will all flash 3 times. After this the main game starts.

This is a two-player game, each player controls a button in line with the LEDs. The third button is for restarting the game. The aim of the game is to stop the lights when the opposite green LED from your button turns on, this is done by pressing your button. Reference Jabrils' Video if unclear. If you don't press the button at exactly the right time, the red LED that was on when you pressed the button will stay on for 500 ms. If you do stop at your scoring LED, it will pause the game, and it will flash as many times as your score. Again, keep in mind that the green LEDs can be very dim. It is impossible to score an "own goal". The first to 5 points will win, when this happens, that player's LED will keep flashing, to restart the game, press the button labeled RESTART, or press the Arduino reset button. This will take the game back to the main menu state.

TIP: Sometimes the restart button on PCB might not work, in that case just press the reset button on the Arduino, if nothing happens still, either change a USB port or restart the computer you are drawing power from.

Code

Arduino codeArduino
the code to run the game
//set pins
int button0pin = 2;
int selectedpin = 11; //selected light from pin 3 to 11
int button10pin = 12;
int resetpin = 13;


//set variables
int clockspeed = 50; //miliseconds
int menutickspeed = 1000; //how long between next LED light up
int holdexitmenu = 5000; //time for holding both buttons to exit main menu mode
int holdrestart = 5000;
int button0holdtime = 0; //how long button has been held in miliseconds
int button10holdtime = 0;

boolean backwards = false;
boolean button0press = false;
boolean button10press = false;

int stoppin; //pin player(s) stop at
int smallplayerscore = 0;
int bigplayerscore = 0;
int notchanging = 1;
int i;
int j;



void(* resetFunc) (void) = 0; //declare reset function @ address 0 





void setup() {
//set pinmodes
pinMode(button0pin, INPUT);
for (i=3; i<=11; i=i+1) {
      pinMode(i, OUTPUT);
}
pinMode(button10pin, INPUT);
pinMode(resetpin, INPUT);


//be in main menu state until both buttons pressed
while (button0holdtime < holdexitmenu && button10holdtime < holdexitmenu) {
  delay(menutickspeed);  //delay each "tick" by clock speed

  if (digitalRead(resetpin) == HIGH) {
    for (i=3; i<=11; i=i+1) {    //reset all pins
      digitalWrite(i, LOW);
    }
    resetFunc();  //restart program
  
  }
  if (digitalRead(button0pin) == HIGH) {   //test if button0 is pressed
    button0press = true;
    button0holdtime = button0holdtime + menutickspeed; 
  }
  else {
    button0press = false;
    button0holdtime = 0;
  }
  
  
  if (digitalRead(button10pin) == HIGH) {    //test if button10 is pressed
    button10press = true;  
    button10holdtime = button10holdtime + menutickspeed; 
  }
  else {
    button10press = false;
    button10holdtime = 0;
  }
  



  
  if (backwards == false) {  ////meue lighting program
  
    if (selectedpin != 11) {
      digitalWrite(selectedpin, HIGH);
      digitalWrite((selectedpin - 1), LOW);
      selectedpin = selectedpin + 1;
    }
  
    else {
      digitalWrite(selectedpin, HIGH);
      digitalWrite((selectedpin - 1), LOW);
      selectedpin = selectedpin - 1;
      backwards = true;
    }
  
  }
  
  else {
  
    if (selectedpin != 3) {
      digitalWrite(selectedpin, HIGH);
      digitalWrite((selectedpin + 1), LOW);
      selectedpin = selectedpin - 1; 
    }
  
    else {
      digitalWrite(selectedpin, HIGH);
      digitalWrite((selectedpin + 1), LOW);
      selectedpin = selectedpin + 1;
      backwards = false;
    }
    
  }
  
}  
  
  
//exit main menu (all lights flash 3 times)
for(j=1; j<=3; j=j+1) {
  for (i=3; i<=11; i=i+1) {
    digitalWrite(i, LOW);
  }
  delay(500);
  for (i=3; i<=11; i=i+1) {
    digitalWrite(i, HIGH);
  }
  delay(500);
}

 for (i=3; i<=11; i=i+1) {    //reset all pins
    digitalWrite(i, LOW);
  }

button0holdtime = 0;
button10holdtime = 0;

}



  





  
void loop() {
  delay(clockspeed);
  //test if reset
  if (digitalRead(resetpin) == HIGH) {
     for (i=3; i<=11; i=i+1) {    //reset all pins
       digitalWrite(i, LOW);
     }
     resetFunc();  //restart program
  }

  
  //read 0 button 
  if (digitalRead(button0pin) == HIGH){
    button0press = true;    

    if (selectedpin == 11) { //test if big player score
      bigplayerscore = bigplayerscore + 1; //add score
      
      if (bigplayerscore >= 5) { //test if win       
        for (i=3; i<=11; i=i+1) {     //run win program
          digitalWrite(i, LOW);
        }

        while(notchanging == 1) {//make infinite loop unless reset
          digitalWrite(11, HIGH);
          delay(500);
          digitalWrite(11, LOW);
          delay(500);

          //test if reset
          if (digitalRead(resetpin) == HIGH) {
            for (i=3; i<=11; i=i+1) {    //reset all pins
              digitalWrite(i, LOW);
            }
            resetFunc();  //restart program
           }
        }
      }

      
      for (i=3; i<=11; i=i+1) { //turn all lights off
        digitalWrite(i, LOW);
      }
      for (i=1; i<=bigplayerscore; i=i+1) { //if score, flash light
        digitalWrite(11, LOW);
        delay(500);
        digitalWrite(11, HIGH);
        delay(500);
      }
    }
    
    for (i=3; i<=11; i=i+1) { //turn all lights off
      digitalWrite(i, LOW);
    }
    for (i=3; i<=11; i=i+1) {    //reset all pins
    digitalWrite(i, LOW);
    }
    digitalWrite(selectedpin, HIGH);
    delay(500);
    digitalWrite(selectedpin,LOW);  
  
  }
  
  else {
    button0press = false;      
  }

  


    //read 10 button
  if (digitalRead(button10pin) == HIGH){    
    button10press = true;

    if (selectedpin == 3) { //test if small player score
      smallplayerscore = smallplayerscore + 1; //add score
     
      if (smallplayerscore >= 5) { //test if win
        for (i=3; i<=11; i=i+1) {     //run win program
          digitalWrite(i, LOW);
        }

        while(notchanging == 1) {//make infinite loop unless reset
          digitalWrite(3, HIGH);
          delay(500);
          digitalWrite(3, LOW);
          delay(500);

          //test if reset
          if (digitalRead(resetpin) == HIGH) {
            for (i=3; i<=11; i=i+1) {    //reset all pins
              digitalWrite(i, LOW);
            }
            resetFunc();  //restart program
           }
        }
      }

      
      for (i=3; i<=11; i=i+1) { //turn all lights off
        digitalWrite(i, LOW);
      }
      for (i=1; i<=smallplayerscore; i=i+1) { //if score, flash light
        digitalWrite(3, LOW);
        delay(500);
        digitalWrite(3, HIGH);
        delay(500);
      }
    }
    
    for (i=3; i<=11; i=i+1) { //turn all lights off
      digitalWrite(i, LOW);
    }
    for (i=3; i<=11; i=i+1) {    //reset all pins
    digitalWrite(i, LOW);
    }
    digitalWrite(selectedpin, HIGH);
    delay(500);
    digitalWrite(selectedpin,LOW); 

  }
  else {
    button10press = false;   
  }

  



  if (backwards == false) {  ////flashing back and forth
  
    if (selectedpin != 11) {
      digitalWrite(selectedpin, HIGH);
      digitalWrite((selectedpin - 1), LOW);
      selectedpin = selectedpin + 1;
    }
  
    else {
      digitalWrite(selectedpin, HIGH);
      digitalWrite((selectedpin - 1), LOW);
      selectedpin = selectedpin - 1;
      backwards = true;
    }
 
  }



  
  else {
  
    if (selectedpin != 3) {
      digitalWrite(selectedpin, HIGH);
      digitalWrite((selectedpin + 1), LOW);
      selectedpin = selectedpin - 1;
    }
  
    else {
      digitalWrite(selectedpin, HIGH);
      digitalWrite((selectedpin + 1), LOW);
      selectedpin = selectedpin + 1;
      backwards = false;
    }
  
  }
  

} 
  

Custom parts and enclosures

Gerber file of PCB design
This is the design of the PCB that slots onto the Arduino, it can be sent to any manufacturer for it to be made. It is in Gerber format which is what most manufacturers take.
gerber_pcb_20190115202606_Hbh7ydLPQ8.zip

Schematics

custom PCB design
This is the PCB Arduino "shield" that will slot onto the Arduino
Asdf 5jnzztxg94

Comments

Similar projects you might like

LED Roulette Game

Project tutorial by Arduino “having11” Guy

  • 8,386 views
  • 4 comments
  • 21 respects

LED Controlled by 3D Gesture Sensor

Project tutorial by HomeMadeGarbage

  • 5,862 views
  • 0 comments
  • 19 respects

Neopixel LED Eyeball Controlled by Nunchuck

Project tutorial by HomeMadeGarbage

  • 5,447 views
  • 7 comments
  • 30 respects

Multi-Source Controlled USB Lamp

Project tutorial by Amal Mathew

  • 3,550 views
  • 2 comments
  • 20 respects

Bluetooth control led with lcd led status display real time.

Project tutorial by Youssef Sabaa

  • 18,227 views
  • 4 comments
  • 35 respects

LED Emergency Lights using WS2812 RGB LED Module

Project tutorial by bigboystoys13

  • 10,004 views
  • 5 comments
  • 20 respects
Add projectSign up / Login