diff --git a/README.md b/README.md index 7bde312..a91ef38 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Introduction -In this project, you will build a two player reaction time game to test which player can push their button fastest when the computer turns off an LED after a random amount of time. +In this project, you will build a two player reaction time game to test which player can push their button fastest when the Raspberry Pi turns off an LED after a random amount of time. ## Materials @@ -8,7 +8,7 @@ In this project, you will build a two player reaction time game to test which pl * Breadboard * Two momentary pushbutton switches * One LED \(any colour\) -* Resistor for connecting LED +* One 1kΩ Resistor * Hookup Wire diff --git a/assets/Breadboard Circuit.png b/assets/Breadboard Circuit.png new file mode 100644 index 0000000..dec7832 Binary files /dev/null and b/assets/Breadboard Circuit.png differ diff --git a/circuit.md b/circuit.md index 611837f..25c718b 100644 --- a/circuit.md +++ b/circuit.md @@ -1,3 +1,5 @@ # The Circuit -Using your knowledge of electrical circuit components, construct a circuit on your breadboard which connects the LED and both buttons to the GPIO pins on the Raspberry Pi. Place the LED centrally so both players can see the LED, and place each button on opposite sides of the LED. Keep track of which GPIO pins you used, so you can use the right numbers in your code. \ No newline at end of file +Construct the following circuit on your breadboard and connect it to the Raspberry Pi. + +![](/assets/Breadboard Circuit.png) \ No newline at end of file diff --git a/disqualifying-players.md b/disqualifying-players.md index f0428c9..2fcb658 100644 --- a/disqualifying-players.md +++ b/disqualifying-players.md @@ -12,13 +12,19 @@ In order to workaround this issue, the game will disqualify players who push the To track when button presses are allowed, and will not disqualify a player, add a variable named `button_press_allowed` above `start_time` and set it to `False`. -Before turning the LED off, but after updating `start_time`, set `button_press_allowed` to `True`. This order ensures that `start_time` is correctly set before button presses are allowed. The order also prevents there from being a very small amount of time just after the LED turns off that we disqualify players even though the LED is off at that point. +Before turning the LED off, but after updating `start_time`, set `button_press_allowed` to `True`. The order should be as follows: -Update the `button_pressed` function to check if button presses are allowed. If they are not, print either "Player one pushed their button before the LED turned off and is disqualified. Player two wins." or "Player two pushed their button before the LED turned off and is disqualified. Player one wins." depending on which button was pressed. If button presses are allowed, the function should continue to print which player won and their reaction time. In both cases, the function should set `game_over` to `True`. +1. Set `start_time` +2. Set `button_press_allowed` +3. Turn the LED off + +This order ensures that `start_time` is correctly set before button presses are allowed. The order also prevents there from being a very small amount of time just after the LED turns off that we disqualify players even though the LED is off at that point. + +Update the `button_pressed` function to check if button presses are allowed. If they are not, print either "Player one pushed their button before the LED turned off and is disqualified. Player two wins." or "Player two pushed their button before the LED turned off and is disqualified. Player one wins." depending on which button was pressed. If button presses are allowed, the function should continue to print which player won and their reaction time. In both cases, the function should continue to set `game_over` to `True`. ## Playing the Game -Run your program and see if it works as expected. If it doesn't do what you thought it would, identify and fix any problems, then run your program again to check if the problem has been fixed. +Run your program and try pressing the buttons before the LED turns off. Make sure that the appropriate player is disqualified depending on which button was pressed. Also make sure it continues to work if you press a button after the LED turns off. If your program doesn't do what you thought it would, identify and fix any problems, then run your program again to check if the problem has been fixed. -Once it works, try playing against someone! +Once it's working, try challenging someone to a competition! diff --git a/measuring-reaction-time.md b/measuring-reaction-time.md index 613e452..37fc8fe 100644 --- a/measuring-reaction-time.md +++ b/measuring-reaction-time.md @@ -14,11 +14,11 @@ from time import perf_counter, sleep Above the `button_pressed` function and below where you setup the LED and buttons, add a variable named `start_time` and set it's value to zero. -Immediately before turning off the LED, store the return value of the `perf_counter` function in `start_time`. +Immediately before your program turns off the LED, store the return value of the `perf_counter` function in `start_time`. ## Calculating Reaction Time -At the beginning of the `button_pressed` function, add a variable named `end_time` and assign it the return value of the `perf_counter` function. This should be the first line in the `button_pressed` function so that you stop timing as soon as possible, and are not including the time your code takes to run in the reaction time. +In the `button_pressed` function, and add a variable named `end_time` and assign it the return value of the `perf_counter` function. This line should come as early as possible in the function so that you stop timing as soon as possible, and are not including the time your code takes to run in the reaction time. Update the `button_pressed` function to calculate the reaction time of the winning player using `start_time` and `end_time`, and print out the calculated reaction time for the winning player. diff --git a/programming-buttons.md b/programming-buttons.md index 9c8c237..ea88c64 100644 --- a/programming-buttons.md +++ b/programming-buttons.md @@ -11,23 +11,28 @@ from gpiozero import Button, LED Near the top of your file, after you setup the LED, setup your buttons with the pin numbers that you connected them to when building your circuit. ```py -player_one_button = Button(25) # Change the pin number to match your circuit +player_one_button = Button(-1) # Change the pin number to match your circuit player_two_button = # Setup based on your circuit ``` ## Running Code when the Buttons are Pressed -In order to run code when the buttons are pressed, below where you setup the buttons, create a function named `button_pressed` which requires a single argument named `button`. This functions will take the button that was pressed as an argument and will not return anything. +In order to run code when the buttons are pressed, copy the incomplete `button_pressed` function below where you setup the buttons. -To run this function whenever the buttons are pressed, configure the GPIO library to call them. Below the `button_pressed` functions, add the following code **for each button**. For example, to configure player one's button, use the following code. +```py +def button_pressed(button): + # Code to respond to buttons being pressed goes here +``` + +The `button_pressed` function requires a single argument named `button` and will not return anything. To run this function whenever the buttons are pressed, configure the GPIO library to call it. Below the `button_pressed` function, add the following code **for each button**. For example, to configure player one's button, use the following code. ```py player_one_button.when_pressed = button_pressed ``` -It is important to **reference the function, but not call it** when configuring the GPIO library. You refer to the function by using it's name alone, without any parentheses or arguments. If you call the function, the GPIO library will be configured to run the _return value_ of the function when the button is pressed or released. This might cause an error when this line of your program runs, or your program may work without any errors, but it will not behave how you expect it to. +It is important to **reference the function, but not call it** when configuring the GPIO library. You refer to the function by using it's name alone, without any parentheses or arguments. If you call the function, the GPIO library will be configured to run the _return value_ of the function when the button is pressed. This might cause an error when this line of your program runs, or your program may work without any errors, but it will not behave how you expect it to. -Once correctly configured, the GPIO library will call the `button_pressed` function with the button that was pressed as the argument when a button is pressed. For example, if player one's button was pressed, `button_pressed` would run, and the value of it's argument would be equal to `player_one_button`. +When correctly configured, the GPIO library will call the `button_pressed` function with the button that was pressed as the argument whenever a button is pressed. For example, if player one's button was pressed, `button_pressed` would run, and the value of it's argument would be equal to `player_one_button`. ## Responding to Button Presses @@ -35,8 +40,9 @@ Add a variable named `game_over` before the `button_pressed` function and after Implement the `button_pressed` function so that when a button is pressed, it -1. Prints "Player one wins." or "Player two wins.", depending on which button was pressed -2. Sets `game_over` to `True` +1. Checks if `game_over` is `True`. If it is not `True` + 1. Set `game_over` to `True` + 2. Print "Player one wins." or "Player two wins.", depending on which button was pressed To change the value of `game_over` in the `button_pressed` function, you will need to add the following line in `button_pressed` before you try changing the value of `game_over`. This line tells the Python interpreter that you want to change the value of `game_over` instead of making a new variable named `game_over` that exists only in the `button_pressed` function. @@ -48,14 +54,15 @@ At the end of your file, add the following loop to wait until `game_over` is `Tr ```py while not game_over: - sleep(0.1) + sleep(0.01) ``` -This code waits a small amount of time as long as the game is not over \(meaning a button has not been pressed\). The`button_pressed`function is called by the GPIO library whenever a button is pressed, and you can imagine it as running at the same time as the loop above. The loop periodically checks if the `button_pressed` function has changed the value of `game_over` to indicate that the program should end. The small sleep time allows other code, such as the `button_pressed` function or other programs on the Raspberry Pi, to run while your code waits for `game_over` to change. +This code waits a small amount of time as long as the game is not over, meaning a button has not been pressed. + +If you're wondering how the above code works, recall that the `button_pressed` function is called by the GPIO library whenever a button is pressed, and you can imagine it as running at the same time as the loop above. The loop periodically checks if the `button_pressed` function has changed the value of `game_over` to indicate that the program should end. The small sleep time allows other code, such as the `button_pressed` function or other programs on the Raspberry Pi, to run while your code waits for `game_over` to change. ## Testing Before going further, make sure that your button code is working. Run your program and push each button when the LED turns off. Make sure that the correct player is printed when you push the button. -If the buttons don't do what you expect them to, there may be a problem in your circuit or code. Identify and fix any problems, then run your program again to check if the problem has been fixed. - +If the buttons don't do what you expect them to, there may be a problem in your circuit or code. Identify and fix any problems, then run your program again to check if the problem has been fixed. \ No newline at end of file diff --git a/programming-led.md b/programming-led.md index ec88309..2fc9d5f 100644 --- a/programming-led.md +++ b/programming-led.md @@ -9,7 +9,7 @@ At the top of the file, import `LED` from the GPIO module. Setup your LED with t ```py from gpiozero import LED -led = LED(0) # Change the pin number to match your circuit +led = LED(-1) # Change the pin number to match your circuit ``` At the end of the file, write code which turns the LED on, waits five seconds, and then turns the LED off. @@ -30,7 +30,7 @@ In order to make the LED turn off randomly, so players can't be prepared to push from random import uniform ``` -The `uniform` function can be used to generate a random floating point number. The random module's documentation provides the following description of `uniform`. +The `uniform` function can be used to generate a random floating point number (i.e. a decimal instead of an integer). The random module's documentation provides the following description of `uniform`. > `random.uniform(a, b)` > @@ -42,7 +42,7 @@ Replace the five second time you added above with a random time by using the ret ## Testing -Before going further, make sure that your LED code is working. Run your program several times and check that the LED turns on and then off after a random amount of time. The time it takes the LED to turn off should be different each time your program runs \(although you might not be able to see a different time depending on the minimum and maximum delay you chose\). +Before going further, make sure that your LED code is working. Run your program several times and check that the LED turns on and then off after a random amount of time. The time it takes until the LED turns off should be different each time your program runs, although you might not be able to see a difference between the times depending on how close the minimum and maximum delay you chose are. If the LED doesn't do what you expect it to, there may be a problem in your circuit or code. Identify and fix any problems, then run your program again to check if the problem has been fixed.