Triple Pi Game

by just_another_person in Circuits > Raspberry Pi

979 Views, 6 Favorites, 0 Comments

Triple Pi Game

IMG_7293.jpeg
IMG_7291.jpg
Pi Game

For this project, I wanted to make a fun and innovative game centered around the idea of 'pie', by using three different forms of it: the microcontroller Raspberry Pi, the number pi, as well as... well, pie. The idea of the game is that users have to insert the correct digits of pi in order, up to the 15th digit. If they arrive there successfully, they are congratulated by a LED lights display, and are spared of a face full of whipped cream. But if, at any point, the user enters an incorrect number, then a servo triggers the game to pie the user in the face.

I was actually pretty surprised when making this project, as it proved to be quite helpful, alongside entertaining. Along the way, with the time spent making the project as well as testing it (with the constant fear of getting pied), I actually managed to memorize quite a few digits of pi!

*Note: for this project, I didn't include the period after 3 in pi, nor did I include a button for 0 as it does not appear within the first 15 digits. However, if you want, feel free to add additional buttons for that by using the same general format for the while true statements in the code.

Supplies

circuit materials.png
Screenshot 2024-04-07 232118.png

Materials:

Circuit:

Construction:

  • 1x box (should be large enough to hold the circuit + drill buttons)
  • 2x straws
  • 1x mini pie tin
  • shaving cream/whipped cream
  • paper
  • colored pencils/pens (optional)

Tools:

  • Scissors/Exacto knife
  • Tape
  • Hot glue
  • Wire stripper

You should also have Thonny installed in your computer for this project, and have a power outlet nearby to plug your raspberry pi pico into.

Code

code1.png
code 3.png

For this project, we'll be coding using Thonny, through the Raspberry Pi pico interpreter. The link to the code is attached below this step, but here's a brief explanation of what it's doing (the numbers on the image correlate to the explanations):

1) Imports + Pin setups:

For this code, we'll be using PWM (Pulse Width Modulation), which is useful for controlling the amount of power delivered to a load or the output. Since we're also importing Pin, it means we refer to the pins while setting up by their GPIO number rather than pin (board) number.

Values for the LED's (the output) and the push-buttons (input) are also assigned and given names here. By using 'Pin.PULL_UP' for the push-buttons, there's also a default of a 'high' value, or 1.

2) While True loop:

**This explanation is only for one if statement, although there are many in the overall code**

Each of these if statements follow the same basic structure, and code for the varying outputs for a given input.

a) If the value of button 3 is 0, it means that the button was pressed. If this condition is met, then it checks for the statements within it.

b &c) The variable 'num' counts the number of digits pressed (ex. 3 is the first digit, so num=0, for 1, num=1, for 4, num=2, ect.) Using that, the code checks to see if the buttons were pressed in the correct order. For the example of 3, it would be pressed on the first and tenth digit, or when n=0 and n=9. If this condition is fulfilled, then 1 is added to num, because another digit is added to the sequence. An LED also lights up to tell the player they got it correct, and it also serves to temporarily stop the button from inputting values, to make sure it does not accidently detect the push twice.

d) The else statement basically tells the code what to do if neither the if or elif statements above (b&c) are met. In this case, it triggers the servo to turn a little over 90 degrees, and then breaks out of the code to prevent the while true loop from repeating.


Later in the code, there's also a light sequence that starts if the player reaches the 15th digit of pi! Also, make sure to save this code as 'main.py' so it can run remotely when connected to a power outlet.

Code: https://github.com/just-another-person1/Projects-/blob/Pi-project/Pi%20Game

Circuit

IMG_7249.jpg
Screenshot 2024-04-04 223449.png
Screenshot 2024-04-10 192501.png

LEDs-- connect the positive end of the LED to the corresponding GPIO pin (refer to the code & pinout chart above), and the negative end to a 220 ohm resistor, and through that to the negative end. One example LED connection is shown in the image above.

Buttons-- similar to the LED connections, but use a 4.4k ohm resistor for each one. For testing, I also labeled each button with the corresponding number to keep it organized.

Servo motor-- connect the VCC, GND & Signal Pin to the 3.3V, GND & GP17 pin of the pico.


Preparing the Box

Screenshot 2024-04-10 193716.png

Cover your box with paper, and then cut holes where each of the components should go (reference the image above). Also include a hole in the side of the box for the USB cable to exit from.

Inset Components

IMG_7264.jpeg
IMG_7263.jpeg

Carefully, add the breadboard, along with the servo and components into the box. To make sure none of the connections were lost, I also cut the top off and then re-secured it after adding everything in. Tape the bottom of the breadboard to the inside of the box, and guide the USB cable through the hole in the back.

Attach Servo and Pie Tin

IMG_7265.jpeg

Hot glue two straws together, and then glue the pie tin at the top. Attach it to the servo motor, making sure it does not interfere with it's motion/hit the box when it moves.

(optional) Decorate!

IMG_7269.jpeg
IMG_7289.jpeg

Add numbers for each button, color your project and add decorations :)