Lock & Unload Game

by RexTheWeeb in Circuits > Arduino

73 Views, 0 Favorites, 0 Comments

Lock & Unload Game

ce733116-6d72-4dd4-821f-625e0420e5b7.jpg

As part of a school project we had to construct something using Arduino. After some experimenting with the pieces we have gotten I came to the idea to make a multiplayer game using the two pushbuttons we got. It's a simple game based around timing and keeping your focus on the servo which will spin around a random amount of times and the person it faces when the blue light turns on has to hit the button in time.

Supplies

You will need:


  • Arduino Uno R3.
  • USB-B Cable.
  • 3 LEDs: Red, Green and Blue.
  • A Breadboard.
  • 1 Mini Servo.
  • 3 220 Ohm Resistors.
  • 2 Pushbuttons.
  • A lot of wires.
  • Sticky Gum.
  • Cardboard.
  • Woodglue.
  • Tape.

Construct the Device

Screenshot_225.png
Screenshot_226.png
IMG_6111.jpg
IMG_6110.jpg
IMG_6109.jpg
IMG_6108.jpg

First things first all the parts need to be connected to the Arduino and Breadboard. I made a poor sketch to visualize the initial idea I had for the circuit as well as a less crude Tinkercad construction to test out the system. You can put the wires in whatever ports work best for you. Just make sure to connect the right things to both ground and the 5 volt. Additionally you can weld together wires and use a weld board instead. It's optional but it does help. I also recommend putting a wire in one of the analog slots that isn't connected to anything else.

Coding Time

Once everything is constructed (or prior if you're using Tinkercad to test first) you can begin the coding process.

First we have to define everything and let the system know that a servo is included.

Here is the entire set of code.



//Definitions.
#include <Servo.h>
#define TheServo 9
#define LED 6
#define player1Button 7
#define player2Button 4
#define LEDcorrect 2
#define LEDincorrect 3


Servo myservo;  


//Variable list.
int draaiwaarde = random(3, 15);
int aantaldraaien = 0;
int servoWaarde = 0;
bool startServo = false;
//Connecting the attached pieces to the Arduino.
void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A0));
  myservo.attach(9);
  startServo = false;
  pinMode(LED, OUTPUT);
  pinMode(LEDcorrect, OUTPUT);
  pinMode(LEDincorrect, OUTPUT);
  pinMode(TheServo, INPUT);
  pinMode(player1Button, INPUT_PULLUP);
  pinMode(player2Button, INPUT_PULLUP);
  digitalWrite(LEDincorrect, LOW);
}


//Reset set-up.
void(* resetFunc) (void) = 0;


//The loop to start the game.
void loop() {
  if (digitalRead(player1Button) == LOW)
  {
    startServo = true;
  }
  if (startServo == true)
  {
    servo();
  }
  if (digitalRead(player1Button) == LOW)
  {
    Serial.println("Test");
  }
  if (digitalRead(player2Button) == LOW)
  {
    Serial.println("Ook Test");
  }
}


//The core of the game.
void servo(){
  draaiwaarde = random(3, 15);
  Serial.print(draaiwaarde);
  Serial.println(aantaldraaien);
  myservo.write(180);
  aantaldraaien = aantaldraaien + 1;
  servoWaarde = 1;
  delay(2000);
  myservo.write(0);
  aantaldraaien = aantaldraaien + 1;
  servoWaarde = 2;
  delay(1000);
  if (aantaldraaien == draaiwaarde)
  {
    digitalWrite(LED, HIGH);
    delay(1000);
    if (servoWaarde = 1)
    {
      digitalRead(player1Button);
      if (digitalRead(player1Button) == LOW)
          {
            digitalWrite(LEDcorrect, HIGH);
            digitalWrite(LED, LOW);
            delay(1000);
            resetFunc();
          } else {
            digitalWrite(LEDincorrect, HIGH);
            digitalWrite(LED, LOW);
            delay(1000);
            resetFunc();
          }
    }  else if (servoWaarde = 2)
          {
            digitalRead(player2Button);
          if (digitalRead(player2Button) == LOW)
          {
            digitalWrite(LEDcorrect, HIGH);
            digitalWrite(LED, LOW);
            delay(1000);
            resetFunc();
          } else {
            digitalWrite(LEDincorrect, HIGH);
            digitalWrite(LED, LOW);
            delay(1000);
            resetFunc();
          }
          }
  }
  else{
    digitalWrite(LED, LOW);
    delay(1000);
    digitalWrite(LED, LOW);
  }
}

Let's go over it step by step.

#include <Servo.h>

#define TheServo 9

#define LED 6

#define player1Button 7

#define player2Button 4

#define LEDcorrect 2

#define LEDincorrect 3


Servo myservo; 


These lines of code tell the Arduino what is attached to what and that a servo is included.


Next we have to determine a couple of integer values before we can get to the set-up:


int draaiwaarde = random(3, 15);

int aantaldraaien = 0;

int servoWaarde = 0;

int buttonstatus1 = 0;

int buttonstatus2 = 0;

bool startServo = false;


The first three variables are named in Dutch but to explain it in English the first gives a random value that you can customize yourself. I went with 3 minimum and 15 maximum but you can change that to fit your needs. Depending on the value that comes out of it the servo will spin that many times. "Aantaldraaien" is a value that gets increased by one every servo rotation and once it is equal to the random number the blue LED will turn on. The "servoWaarde" represents which side the servo is pointing at, and startServo is for the function we will call in later.


Next we need to do the set-up:

void setup() {

 Serial.begin(9600);

 randomSeed(analogRead(A0));

 myservo.attach(9);

 startServo = false;

 pinMode(LED, OUTPUT);

 pinMode(LEDcorrect, OUTPUT);

 pinMode(LEDincorrect, OUTPUT);

 pinMode(TheServo, INPUT);

 pinMode(player1Button, INPUT_PULLUP);

 pinMode(player2Button, INPUT_PULLUP);

}


The Serial Begin is to set the baud rate for the serial monitor. It's to help test everything to make sure it is all connected properly. randomSeed(analogRead(A0)) actives the pseudo-random-generator which allows the random spins to happen in the first place. A0 is the analog port of the same name. If you put the loose wire in a different analog slot make sure the A0 is replaced by the appropriate slot so if you put it in say A1 you have to put in randomSeed(analogRead(A1)). myservo is to attach the servo to a pin. It helps make the earlier definition work better. Note that the Servo only works with pin 9 and 10. The rest is to determine the input and output. Every LED is part of the output whilst the Servo is part of the input. INPUT_PULLUP makes sure the button values are set to HIGH which ensures that the presses are registered properly.


Another important line of code is:


void(* resetFunc) (void) = 0;


This is for the reset function. With this you can restart the game once it is done.


The loop is up next.


void loop() {

 if (digitalRead(player1Button) == LOW)

 {

  startServo = true;

 }

 if (startServo == true)

 {

  servo();

 }

}


Once the player 1 button is pressed it will turn the startServo value on which triggers the servo function.

In said servo function the actual game is coded:


void servo(){

 myservo.write(180);

 aantaldraaien = aantaldraaien + 1;

 servoWaarde = 1;

 delay(2000);

 myservo.write(0);

 aantaldraaien = aantaldraaien + 1;

 servoWaarde = 2;

 delay(1000);

 if (aantaldraaien == draaiwaarde)

 {

  digitalWrite(LED, HIGH);

  delay(1000);

  if (servoWaarde = 1)

  {

   digitalRead(player1Button);

   if (digitalRead(player1Button) == LOW)

     {

      digitalWrite(LEDcorrect, HIGH);

      digitalWrite(LED, LOW);

      delay(1000);

      resetFunc();

     } else {

      digitalWrite(LEDincorrect, HIGH);

      digitalWrite(LED, LOW);

      delay(1000);

      resetFunc();

     }

  } else if (servoWaarde = 2)

     {

      digitalRead(player2Button);

     if (digitalRead(player2Button) == LOW)

     {

      digitalWrite(LEDcorrect, HIGH);

      digitalWrite(LED, LOW);

      delay(1000);

      resetFunc();

     } else {

      digitalWrite(LEDincorrect, HIGH);

      digitalWrite(LED, LOW);

      delay(1000);

      resetFunc();

     }

     }

 }

 else{

  digitalWrite(LED, LOW);

  delay(1000);

  digitalWrite(LED, LOW);

 }

}


That is a lot of new code so we will go over it step by step starting with:

myservo.write(180);

 aantaldraaien = aantaldraaien + 1;

 servoWaarde = 1;

 delay(2000);

 myservo.write(0);

 aantaldraaien = aantaldraaien + 1;

 servoWaarde = 2;

 delay(2000);


This is for the servo. myservo.write allows it to rotate to different degrees. After each spin the "aantaldraaien" variable gets increased by 1. The "servoWaarde" changes depending on which side it is facing and then there is a delay. You can customize this delay to fit your needs. I went for 2000 (2 seconds) but if you want to you can make it longer or shorter. This repeats endlessly until the next condition is met:


 if (aantaldraaien == draaiwaarde)

 {

  digitalWrite(LED, HIGH);

  delay(1000);


If the "aantaldraaien" variable is equal to the randomize value at the start the blue light defined as simply LED will turn on and then there will be a short delay which you can again customize. That delay value determines the difficulty so you can make the game really hard by making it short.


if (servoWaarde = 1)

  {

   digitalRead(player1Button);

   if (digitalRead(player1Button) == LOW)

     {

      digitalWrite(LEDcorrect, HIGH);

      digitalWrite(LED, LOW);

      delay(1000);

      resetFunc();

     } else {

      digitalWrite(LEDincorrect, HIGH);

      digitalWrite(LED, LOW);

      delay(1000);

      resetFunc();

     }


If the servo value is equal to 1 (which is facing player 1) it will read the input of player 1. If the button is pushed it will turn on the green light, then turn the blue light off and after another delay reset the game, ending it. If the button isn't pushed the red light will turn on instead.


 } else if (servoWaarde = 2)

     {

      digitalRead(player2Button);

     if (digitalRead(player2Button) == LOW)

     {

      digitalWrite(LEDcorrect, HIGH);

      digitalWrite(LED, LOW);

      delay(1000);

      resetFunc();

     } else {

      digitalWrite(LEDincorrect, HIGH);

      digitalWrite(LED, LOW);

      delay(1000);

      resetFunc();

     }

     }

 }


This is the same as the part earlier but for the second player.


 else{

  digitalWrite(LED, LOW);

  delay(1000);

  digitalWrite(LED, LOW);

 }

}


This turns the light off and closes the functional. This is optional (outside of closing the function obviously) but it helps in case something goes wrong.


And that should be the code. The entire code is below. There might be some differences with this version and the version uploaded here but those don't affect the system at all and simply serve as an assistant tool to troubleshoot.

Downloads

Case Building

IMG_6124.jpg
IMG_6125.jpg
IMG_6126.jpg
b2cccb56-2861-4cb9-9002-cb09e5a34792.jpg
ce733116-6d72-4dd4-821f-625e0420e5b7.jpg
4a527cfb-c53a-4b3f-a8e6-28b2407442f1.jpg
IMG_6209.jpg
IMG_6210.jpg
IMG_6211.jpg

Next up you have to build the casing. I have several pictures of the process and I will guide you through it carefully.


First I put a small piece of cardboard down and glued two on the sides to create a L shape. The one on the back has 3 holes so you can see all LEDs through it and the one on the side has a hole in it where you can put your finger in to press the button. I put on some sticky gum and attached the breadboard on there with the Arduino next to it. I made a secondary wall on the side which also has a hole in it for the second player to use their button. Then I constructed a roof over it which is taped up so you can open it up to make adjustments if necessary. I cut a hole in the roof for the servo and glued another piece to the end and cut holes down there so it can be plugged into the necessary power source. To end it off I cut more holes in the side for the player to see the LEDs through and taped the servo to the top to ensure it doesn't fall over during its spinning process.


And that should be it. I put two videos down below showing the LEDs in action.

The design isn't perfect and could be improved in ways I can already envision however due to me not knowing what I am doing half the time I had to make it work with what I could do. But that's all from me.