PIPER MAKE EDUCATOR RESOURCES SERIES
It's a race of the reflexes to see who can hit their button the fastest!
Time: 30 minutes
Age Range: 8+
In this project, students design a game of skill that they may play against each other. Each player will race to press their button the fastest after the light turns on.
Note: There are step by step instructions for the students to follow in the tutorials included in each project on Piper Make. These provide directions both for writing code and for building the electronic circuits. The tutorials are well-defined and most students will be able to follow them with little assistance required.
- Build a head-to-head game of skill and program selection of a winner
- Coding Skills: Procedures, Timer, Sequences, Variables
- Practice coding loops
Review these key electronics understandings:
- Binary state of a button as an input
Practice computational concepts of
- loops: running the same sequence multiple times
- events: while a pin’s condition is on or off, another action happens
- Create programs that use variables to store and modify data.
- Create programs that include events, loops, and conditionals.
- Decompose problems into smaller, manageable tasks which may themselves be decomposed.
- Create programs by incorporating smaller portions of existing programs, to develop something new or add more advanced features.
- Test and debug a program or algorithm to ensure it accomplishes the intended task.
- Perform different roles when collaborating with peers during the design, implementation, and review stages of program development.
CA Computer Science Standards
3-5.3-5.AP.11 Create programs that use variables to store and modify data. (P5.2)
3-5.AP.12 Create programs that include events, loops, and conditionals.
3-5.AP.13 Decompose problems into smaller, manageable tasks which may themselves be decomposed. (P3.2)
3-5.AP.14 Create programs by incorporating smaller portions of existing programs, to develop something new or add more advanced features. (P4.2, P5.3)
3-5.AP.17 Test and debug a program or algorithm to ensure it accomplishes the intended task. (P6.2)
CCSS.ELA.L.W.3.8: Recall information from experiences or gather information from print and digital sources; take brief notes on sources and sort evidence into provided categories.
CCSS.ELA.L.W.3.10: Write routinely over extended time frames (time for research, reflection, and revision) and shorter time frames (a single sitting or a day or two) for a range of discipline-specific tasks, purposes, and audiences.
CA ELD.3.C.11: Supporting own opinions and evaluating others’ opinions in speaking and writing
CA ELD.3.C.12: Selecting and applying varied and precise vocabulary and language structures to effectively convey ideas
MICHIGAN INTEGRATED TECHNOLOGY COMPETENCIES FOR STUDENTS (MITECS)
1B-AP-09 Create programs that use variables to store and modify data. Subconcept: Variables; Practice 5.2
1B-AP-10 Create programs that include sequences, events, loops, and conditionals. Subconcept: Control; Practice 5.2
1B-AP-11 Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process. Subconcept: Modularity; Practice 3.2
1B-AP-12 Modify, remix, or incorporate portions of an existing program into one’s own work, to
develop something new or add more advanced features. Subconcept: Modularity; Practice 5.3
1B-AP-15 Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended. Subconcept: Program Development; Practice 6.1, 6.2
Michigan ELA, Grade 3-8, Research, 8: Recall information from experiences or gather information from print and digital sources; take brief notes on sources and sort evidence into provided categories.
Michigan ELA, Grade 3-8, Range of Writing, 10: Write routinely over extended time frames (time for research, reflection, and revision) and shorter time frames (a single sitting or a day or two) for a range of discipline-specific tasks, purposes, and audiences.
WIDA ELD Standards
- Ask questions about others’ opinions
- Support own opinions with reasons
- Clarify and elaborate ideas based on feedback
- Defend change in one’s own thinking
- Revise one’s own opinions based on new information
Interpret scientific arguments by
- Identifying potential evidence from data, models, and/or information from investigations of phenomena or design solutions
- Analyzing whether evidence is relevant or not
- Distinguishing between evidence and opinions
In this project, students will learn to program functions / procedures, a key skill for programming. Other key skills in this project include timer, sequences and variables.
Raspberry Pi Pico, breadboard, charging cable, M2M Blue Jumper Wire, M2M Red Jumper Wire, M2M Black Jumper Wire, Red LED, 330 Ω Resistor, Small Tactile Button
OVERVIEW OF STEPS
Step 1: Ready to play?
Warm up your fingers my friends, because in this project we’ll be testing those lightning fast reflexes!
Let’s build a game where you and a friend can see who has the fastest reaction time!
Click NEXT to get started.
Step 2: Get your electronics
Let’s start by gathering our supplies. You’ll need your Pico and breadboard, an LED of any color, a single 330 Ω resistor, two push-button switches, and three jumper wires.
Step 3: Wire your LED
Here’s how the game works. An LED will flash on at a random moment, and the first person to hit their button wins!
To start, let’s build our LED that will signal the start of the game.
First connect one end of a jumper wire to GP15 and the other a few rows down. Push the leads of the LED into the same row straddling the center gap, Make sure that the longlead of the LED (the anode) is on the side of the breadboard with the jumper wire connected to GP15.
Connect a resistor from the row with the shortlead of the LED to the GROUND rail. Then, connect the GROUND rail to a ground (GND) pin on the Pico with another jumper wire.
Check out the Blink tutorial if you have any questions!
Once you've added your LED, click NEXT.
Step 4: Wire your push buttons
Next, let’s add the push buttons that you’ll be competing with!
Go ahead and add the push buttons as shown in the picture below.
Connect one of the buttons to GP14 and the other to GP13. Make sure you connect the other side of each button to the GROUND rail. Remember, when you press down the button, the GPIO pin will detect the drop in voltage as an INPUT.
Click NEXT when you finish adding your buttons.
Step 5: Wiring is done!
Now let’s write some code to create our game! Let’s think about how we want the game to run.
First, we want our LED to turn on at a random time. How do we code that?
Start by dragging a start block from your Chip menu, followed by the turn pin ON block. Change the pin number variable to GP15 (where your LED is connected).
But wait! We don’t want our LED to immediately turn on, right? Let’s add a wait block in there. Drag a wait block from the Chip menu and connect it between the start and turn pin blocks.
You’ll notice that a time is automatically set to 1 second. However, if we know the time the LED will turn on, we might just count down in our heads instead of playing the game.
Click NEXT to learn how to make the wait time random.
Step 6: Write the code
To make the wait time random, let’s use the random integer block in the Values menu, and drag it into the wait step.
This will cause the number block to "pop-out" - you can just drag that block to the trash can.
The leftover number block might be behind your program - just drag your whole program by grabbing the start block to move it and see what might be underneath it.
The random integer block returns a random number from the range you provide, so go ahead and put in the minimum and maximum number of seconds you want to wait for the LED to come on.
A good range is between 3 and 8 seconds, so we don’t get too impatient.
Step 7: What's your function?
Okay so now we have our LED turning on, and we’re ready with our fingers on the buttons. But how can we get the code to detect which button is pressed first?
To do this, we’re going to write a Function. A function is a set of code that will run and return (send back) a value—in our case, the winner!
Drag out a to "do something" - return block from the Function menu and change the name of the function to “determine winner”.
Step 8: Send it back!
The first thing to think of when creating a function is what it will return. In this case, we want to know who pressed the button fastest, so we’ll want our function to return the name of the player who won!
To start, let’s create a variable (from the Variables menu) and call it “winner”. Then, drag the winner block and place it as the return of your function, so that it can be printed when the game ends.
Step 9: Setup the ifs
To figure out what that winner variable should be set to, we’ll need to check whether player 1 or player 2 pushed down their button.
To do this, let’s write an if-else if statement. This will test which, if any, button is pressed.
Drag an if-do block from the top of the Logic menu into your "determine winner" function block to get started.
We still need to turn the if-do block into an if-else if block. To do this, click the small blue gear icon on the if-do block. This will bring up a new menu that lets you change the shape of the if block:
In that menu, drag an else if block over to the if block and connect it. Clock the blue gear icon again to close the menu.
Click NEXT when your if-else if block is ready.
Step 10: Checking in
Remember we want the code to do the following: if the button connected to GP14 is pressed, we want “player 1” to be saved to our winner variable. Otherwise, if the button connected to GP13 is pressed, we want “player 2” to be saved to the winner variable. How would you code that?
Click NEXT to find out!
Step 11: Looking for button presses
Drag out an is pin LOW when pulled UP block and add it to the first test of our if-else if block. Change the pin number variable to 14.
Duplicate the is pin block for the else-if test. Change that pin number variable to 13.
Step 12: Setting the variable
If the GP14 circuit completes, we want to set our variable to “player 1”—so drag a set winner to block from the Variables menu and place it in the first do statement.
Next, you'll need a "_" block from the Values menu. Grab one and drag it into the set winner to block.
Type “player 1” as the value into the "_" block.
Repeat all of the steps for player 2 below the GP13 test step.
When your blocks are ready, click NEXT.
Step 13: Keep checking
But we want our code to be constantly looping, testing if either button is pressed and we want that process to be happening FAST!
Let’s add a loop function to the check steps. Drag out repeat forever block and change the wait time to 0.
Why zero? Well if we set it to 1 second, you might see the LED come on, both players press the button, and THEN the loop will execute, automatically making the first player in the loop, “player 1” the winner. Every time. Boo!
Instead, with the wait time set to zero, the loop happens so quickly that tiny differences in your reaction time are picked up!!
Step 14: Exiting the loop
But we also want the loop to stop running when a button is pressed, right? Otherwise, we’ll never know who won!
To have the function return the winner value, we have to stop the loop from running using an exit loop block. So if player 1 hits their button, the winner variable will be set to "player 1" and the loop will be exited.
If play two hits their button first, then the same process should happen for player two in the if-else part of the block.
Drag out two exit loop blocks and add them to your function code like this:
Step 15: Who won?
So let’s go back to our main code and code up the big reveal! Right now we have the LED coming on, but after that, we want to execute the function we’ve written. And ideally, because the function’s output is the winner—we want to print that in our console.
To do that, drag a print block from the Chip menu and add the output from the function to it.
Keep in mind, this MUST be the output from the function, so drag the determine winner block from the Function menu.
After that, go ahead and turn off the LED using a turn pin 15 OFF block:
Ready to try it out? Click NEXT.
Step 16: Play your game!
Now your code is ready to go.
Here's a reminder on how you play! An LED will flash on at a random moment, and the first person to hit their button wins!
Grab a friend and hit START to play! Who can hit the button fastest when the LED comes on?
Keep tweaking your game! Can you modify the messages that print? Can you add a third button so that three people can play at once? Is there a limit to how many buttons you could add?
Step 17: You Finished!
Click EXIT to return to the menu and start your next coding challenge.