Bug-B-Gone -- Learn to debug a program and circuit here!
In this project, students learn about debugging a program and circuit.
Students are presented with a game design and the code, where player 1 presses the green button, and player 2 presses the blue button. They will need to build the circuit and then follow instructions on how to identify the incorrect code using the print statement.
In this project, students will learn a key skill which is fixing code that someone else has written incorrectly. Students will view code that is written incorrectly and must use the print command with the console to understand what is wrong with the code before making the necessary changes. The print statement is used to view variable values on the console. Other key skills in this project are events, conditionals, variables and functions.
Troubleshooting Tips
The DeBug project is different from previous projects in that students start with code that has already been written. Students must check the code by using the print command and the console to debug the code someone else wrote. The print commands display the current value stored in the variables player_one_score and player_two_score.
There are many new coding concepts to learn at once, and students may struggle to understand what is happening in the code. Note that the code assumes pin 3 is connected to the green button for player one and pin 8 is connected to the blue button for player 2. Those are the two event blocks.
The first 2 Code Solutions on the right are the events. The event for pin 8 (when pin 8 block) is incorrect in the project as it states to "change player_one_score" when it should be coded to "change player_two_score." The third Code Solution on the right shows the function 'winner' that runs when one scores greater than or equal to 3.
The student must remove the print commands for the two event blocks before running the game. The print commands are there only to debug what is wrong with the scores. However, they can leave the print commands in the winner function.
In the event blocks, if blocks are used as conditionals to check the value of the variables that store the number of times the button has been pressed. In the winner function, an if else block is used as a conditional to check the value of the variable player. This variable value is passed to the function with the blocks winner with player "one" and winner with player "two." The if block checks to see if player contains the value of "one" in the first half of the if block and checks for the value of "two" in the "else if" part of a block. When one of those conditions is true, the winner message prints to the display, and the LED turns on.
Our customer support specialists are on hand to ensure your implementation of Piper runs seamlessly. View Support Docs or Contact Support
PROJECT STEPS
Step 1 Build it
This is the code for a two player game. First, build your controllers.
Step 2 Try it
Now press Start. The first player to press their button 3 times should win and their LED should light up. Does it work for both buttons?
Step 3 Print variables
Player One always wins. Why is that? Lets add a Print block to find out. Attach the String block and type Player One.
Step 4 Print player one
Add another Print block. This time attach the playerOneScore variable.
Step 5 Attach to button
Drag these two Print statements into the pin 36 event block.
Step 6 Use the console
Select the Console Tab and press Start. When you press the green button you should see the player one variable count up.
Step 7 Try player two
Player one looks good, now let's try player two. Create Print statements for player two.
Step 8 Attach to the button
Now drag the new Print statements into the pin 26 event block.
Step 9 Check the Console
Now press Start with the console on. What happens when you press the blue button?
Step 10 What's wrong?
Look closely at the code in the pin 26 event block. Is something wrong? Try to fix it.