The Reaction Timer

Now they've learnt how to setup the Pi, write some simple programs, flash an LED and detect a button - they're ready to start making some cool stuff!

The last 2 lessons focus on building a reaction timer and then running a class competition to see who has the fastest reactions.

Students have mostly copied code and made minor modifications. This time they'll work together to plan a program and to turn it into Python using pseudo code.

Learning objectives

Students learn:

Resources

Lesson Plan

What do we need for a reaction timer? : 25 minutes

Ask what hardware is needed? They react to the LED and push a button. Ask what software is needed? The LED has to light after a random time so they can't predict it.

Discuss with the class how the software will work.

Pseudo code

Tell the students to get into groups of 4 and use the program cards to lay out a program sequence (from top to bottom). They'll need to use the blank ones to add their own variable names or extra codes.

After a few minutes ask 2 members of each team to walk around and get ideas from the other teams. They can ask questions, and the remaining members answer them.

Example code

My variable names are written in blue.

 Example pseudo code

If you're feeling shaky with Python, then you can ask the whole class to agree to follow one team's program structure. This will make it easier to debug code later. Then take a photo and show it on the projector or ask someone to copy it onto the white board. Make sure you save it for the next lesson!

Convert pseudo code to Python comments

Ask students to get back in pairs, open a new program with Idle and save it as reaction.py.

Ask students to copy the code structure into their program, starting each line with a # so it becomes a comment. The lines will change colour to remind them it's a comment.

It should end up something like this:

#pick a random number and store in sleep_time

#sleep for sleep_time seconds

#turn on the led 

#store the current time in start_time

#in a loop:
    #if the button is pressed:
        #break out of the loop

#work out how long they took by subtracting the start_time now from the time now

#print out how long they took

Extra Python

We've introduced 2 commands with the program cards that they haven't covered yet in Python. These are also included in the handouts.

Generating a random number

Here we import a new library called random and then use randint() to produce a random number between 1 and 5:

1
2
3
import random
my_number = random.randint(1,5)
print(my_number)

Ending a loop

Here we use break to end the while loop:

1
2
3
4
5
counter = 0
while True:
    counter = counter + 1
    if counter == 10:
        break

Build reaction timer software : 30 minutes

Students work in pairs to convert the comments into Python code. They can reference any of the programs they've saved so far. They should write the Python below the comment, and leave the comment in for reference.

Ask them to start at the top and to test each line of code at a time by saving and running the code.

If students get stuck, remind them to look at their previous programs:

Reference code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#import the libraries
import time
import random
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

button_pin = 8
led_pin = 10

#set the button pin to be True to start,
#when we press the button, the pin will change to False.
GPIO.setup(button_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)

#set the led pin to be an output
GPIO.setup(led_pin, GPIO.OUT)
GPIO.output(led_pin,False)

#how long should we wait before turning on the led
sleep_time = random.randint(3,10)

#wait for our random time
time.sleep(sleep_time)

#turn on the led and store the current time in a variable
GPIO.output(led_pin, True)
start_time = time.time()

#wait for the button to be pressed...
while True:
    if GPIO.input(button_pin) == False:
        print("stopped!")
        #finish the while loop
        break

#work out how long they took:
reaction_time = time.time() - start_time
print("you got" + str(reaction_time) + "seconds")

Pack away : 5 minutes

Pack away the Pi.

Homework

Continue with the homework project from the last week. Students convert the software description they came up with to comments, and then start working on converting the comments into Python code.

Outcome

All students:

Most students: