22warehamD
Published © GPL3+

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

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

BeginnerFull instructions provided6,406
Arduino Controlled LED Multi-Player Game on Arduino "Shield"

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Resistor 10k ohm
Resistor 10k ohm
×3
Resistor 220 ohm
Resistor 220 ohm
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×3
Male-Header 36 Position 1 Row- Long (0.1")
Male-Header 36 Position 1 Row- Long (0.1")
×1
3 mm LED: Red
3 mm LED: Red
×7
5 mm LED: Green
5 mm LED: Green
×2
Custom PCB
Custom PCB
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

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.

Schematics

custom PCB design

This is the PCB Arduino "shield" that will slot onto the Arduino

Code

Arduino code

Arduino
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;
    }
  
  }
  

} 
  

Credits

22warehamD

22warehamD

3 projects • 6 followers

Comments