How to Make a Rock Paper Scissors Game on Tinkercadd!

by 772151 in Circuits > Arduino

1905 Views, 0 Favorites, 0 Comments

How to Make a Rock Paper Scissors Game on Tinkercadd!

Screen Shot 2020-06-19 at 8.15.23 PM.png

Hey world! Back at it again with yet another Tinkercadd tutorial! Today, I have a much more complex circuit that allows you to play Rock Paper Scissors with a computer! However, it won't be that difficult if you follow this tutorial! Let's get started!

Supplies

1 Breadboard

1 Arduino R3

9 330 Ohm Resistors

3 10 Ohm Resistors (Or Any Weak Resistors)

3 Button Switches

6 LEDs (Half Being One Color, Other Half Being Another Color)

1 LED RGB

And, for something optional, include:

1 DC Motor

1 H-Bridge Motor Driver (L293D)

Lots of Wires

Organization

Screen Shot 2020-06-19 at 6.16.38 PM.png

The first thing you should do is organize your materials that you are going to use. As you can see, I connected power (5V) and ground to the breadboard, and all of my materials are on the top of the breadboard. And, don't forget the optional materials if you want to add something at the end!

Option Selector

Screen Shot 2020-06-19 at 6.20.48 PM.png

The first thing we will create is the option selector when you play Rock Paper Scissors with the computer. We will be using push buttons to act as a selector for your choice, being either rock, paper, scissors. Some thing to pay attention to is how I'm trying to use the least amount of space possible, as this project does use the entire breadboard! Make sure your push buttons are as close to the right side of the breadboard as possible.

In terms of the wiring, place all three push buttons close to each other, but with a one hole space between each. Use wiring to connect each button to power, and use your 10 Ohm resistors to connect the buttons to ground. Remember, you don't have to use 10 Ohm resistors, just a really weak resistor so that the buttons can work!

Digital Pin Connections

Screen Shot 2020-06-19 at 6.23.51 PM.png

We might have push buttons, but these buttons are rather useless at the moment. If we connect them to the Arduino, we could code later to make a working Rock Paper Scissors game!

Pay close attention! Starting from the button on the left, connect the buttons to digital pins 2, 3, and 4. That means the button on the left is connected to digital pin 2, the button in the middle is connected to digital pin 3, and the button on the right is connected to digital pin 4. One really important thing to keep in mind while doing this is wire management! If your wires are all over the place, you won't really be able to understand what you created and might mess up! In this case, I used orange wiring to represent the button pins. You don't necessarily have to use orange, but assign the wires for the buttons a certain colour and organize the wires so you don't mess up!

Player Selection LEDs

Screen Shot 2020-06-19 at 7.18.32 PM.png

Now that we have fully wired buttons for selecting an option in Rock Paper Scissors, we need LEDs to show that we clicked a certain option! Paying attention to our lack of space, we will be using 3 LEDs just above the push buttons, and in this case, they will be green LEDs. Of course, you can use any colour LEDs you want.

The first thing to do would be to put resistors on the digital pins we will be using for the LEDs, which are digital pins 5, 6 and 7. Next, we will place the green LEDs similar to how we placed the push buttons; close together but two pins away from each other. We will connect each cathode to ground, and then connect the LEDs to their individual digital pins. The LED on the very left connects to digital pin 5, the LED in the middle connects to digital pin 6, and the LED on the very right connects to digital pin 7. Make sure your wires are colour coded and organized like mine! In this case, I used yellow wires to represent the player selection LEDs.

Win-Lose Selector

Screen Shot 2020-06-19 at 7.22.07 PM.png

Now that we have LEDs that show what the player chose, we should create the RGB LED that will decide whether the game is a win, loss, or tie. If the RGB LED is green, it would mean we won, if it were red, it would mean we lost, and if it were yellow, it would mean the game was a tie.

For the wiring, we can connect the cathode of the RGB LED to the ground pin. Similar to the player LEDs, we connect three of the 330 Ohm resistors to digital pins 9, 10, and 11. Next, we connect the blue anode to digital pin 9, the red anode to digital pin 10, and the green anode to digital pin 11. With that, we have an RGB LED that tells us whether we have won, lost, or tied the game! Again, be sure to use unique wire colours and have organized wires. In this case, I used the same colour of each anode for the wire.

Computer Selection LEDs

Screen Shot 2020-06-19 at 7.27.37 PM.png

All that's left is the LEDs for the selection of the computer, and the wiring will be complete!

Similar to how we placed the push buttons and the player LEDs, we will place the computer LEDs close together but separated by 2 pins. Since this process will be decided by the computer/Arduino, we just have to connect the cathodes to ground, and the anodes to analog pins. We will use our remaining resistors and connect each LED to a specific analog pin. The LED on the left will be connected to analog pin A3, the one in the middle will be connected to analog pin A2, and the one on the right will be connected to analog pin A1. Make sure your wires are organized and coloured! In this case, I used purple to represent the computer LEDs.

Code!

Finally, to get this game to work, we need to add the code to the Arduino! Here it is:

int buttonState = 0;

int clickRock = 0;

int startGame = 0;

int clickPaper = 0;

int clickScissors = 0;

int compChoise = 0;

int userChoise = 0;

int userWin = 0;

int compWin = 0;

void setup()

{

pinMode(2, INPUT);

Serial.begin(9600);

//Rock Paper Scissor Pins

pinMode(5, OUTPUT);

pinMode(3, INPUT);

pinMode(6, OUTPUT);

pinMode(4, INPUT);

pinMode(7, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

pinMode(9, OUTPUT);

pinMode(A1, OUTPUT);

pinMode(A2, OUTPUT);

pinMode(A3, OUTPUT);

//Fan Pins (OPTIONAL)

pinMode(13, OUTPUT);

pinMode(12, OUTPUT);

pinMode(8, OUTPUT);

digitalWrite(13, HIGH);

}

void loop()

{ delay(10); // Wait for 10 millisecond(s)

// Testing for input (rock)

clickRock = digitalRead(2);

if (clickRock == HIGH) {

userChoise = 1;

startGame = 1;

digitalWrite(5, HIGH);

} else {

digitalWrite(5, LOW);

}

// Testing for input (paper)

clickPaper = digitalRead(3);

if (clickPaper == HIGH) {

userChoise = 2;

startGame = 1;

digitalWrite(6, HIGH);

} else {

digitalWrite(6, LOW);

}

// Testing for input (scissors)

clickScissors = digitalRead(4);

if (clickScissors == HIGH) {

userChoise = 3;

startGame = 1;

digitalWrite(7, HIGH);

} else {

digitalWrite(7, LOW);

}

// start game if user presses a button

if (startGame == 1) {

compChoise = random(1, 3 + 1);

// test for draw; RGB LED Yellow

if (compChoise == userChoise) {

analogWrite(10, 255);

analogWrite(11, 255);

analogWrite(9, 0);

}

// look for winner

if (compChoise == 1 && userChoise == 2) {

userWin = 1;

}

if (compChoise == 1 && userChoise == 3) {

compWin = 1;

}

if (compChoise == 2 && userChoise == 1) {

compWin = 1;

}

if (compChoise == 2 && userChoise == 3) {

userWin = 1;

}

if (compChoise == 3 && userChoise == 1) {

userWin = 1;

}

if (compChoise == 3 && userChoise == 2) {

compWin = 1;

}

if (compChoise == 1) {

digitalWrite(A1, HIGH);

}

if (compChoise == 2) {

digitalWrite(A2, HIGH);

}

if (compChoise == 3) {

digitalWrite(A3, HIGH);

}

if (userWin == 1) {

analogWrite(10, 51);

analogWrite(11, 255);

analogWrite(9, 51);

}

if (compWin == 1) {

analogWrite(10, 204);

analogWrite(11, 0);

analogWrite(9, 0);

}

}

//Epic Fan Code (OPTIONAL)

digitalWrite(12, HIGH);

digitalWrite(8, LOW);

delay(1000); // Game Resets Every Second

startGame = 0;

compChoise = 0;

userChoise = 0;

userWin = 0;

compWin = 0;

digitalWrite(A1, LOW);

digitalWrite(A2, LOW);

digitalWrite(A3, LOW);

}

The code is divided into sections, and all of the sections are labeled with comments (//). Check the code out!

...and, did you see something to do with a fan? Read on to the next step...

The Optional Fan

Screen Shot 2020-06-19 at 8.12.14 PM.png

If you read the code thoroughly, you might have noticed the codes for a fan. You see, the wiring for this Rock Paper Scissors game could potentially, and so building a fan with a simple motor circuit is perfect! While this is optional, if you're up to the challenge, simply get a DC motor and a h-bridge motor driver, and I'll teach you how to build it!

Fan Circuit + Conclusion

Screen Shot 2020-06-19 at 8.15.23 PM.png

With only 3 digital pins left, we can make the most out of the Arduino in order to incorporate a cooling fan into our game.

As you can see, you can place the h-bridge just barely in between the push buttons and the wiring for the computer LEDs. With the h-bridge placed, we can begin wiring it starting with power, connected from the power rail of the breadboard to the power 1 and 2 terminals of the h-bridge. We can also connect both grounds, and all that's left are the digital pins to connect and the output connection for the motor. We connect digital pin 13 to enable 1 & 2 (this turns the motor on or off), digital pin 12 to input 1, and digital pin 8 to input 2. With the code already complete, all we need to do is connect output 1 to the positive side of the motor, and connect output 2 to the negative side of the motor. And with that, alongside the game, you will have a running motor that will be constantly spinning to keep your electronic cool! How convenient!

This is the end of my tutorial. The code, for those who did not understand it, resets the game every second, which means you click the button for around half a second to select an option, and then let go and see the colour of the RBG LED to see whether you won, lost, or tied with the computer!

Thank you for reading my tutorial, and I hope you enjoyed!