Digital Input & Timing — Day 1 of 2
'F' → Fullscreen
Today's game needs three LEDs, not five. A blue LED is the GO signal, green means your reaction was fast, and red means it was slow. Create a new MicroPython script named 2-7-reaction-timer.
3V3 pin and GPIO 33.GPIO 33 is the last pin in the block you have been using — immediately to the left of 32:
The button connects GPIO 33 to 3V3 when it is pressed. The 10K resistor connects the pin to GND the rest of the time:
With the USB cable unplugged, change Activity 2.6's array into the game's three lights, then press the button into the empty left side of the board so it straddles the gap down the middle. The 3V3 wire goes to the top + rail rather than straight to the button, so the parts you add in later activities can share it:
| Part | Where it goes |
|---|---|
| GPIO 31 and 32's wires, LEDs, and resistors | Take them off |
| LED on GPIO 28 (e16, e17) | Swap in a blue LED, long leg in e16 |
| LED on GPIO 29 (e19, e20) | Swap in a green LED, long leg in e19 |
| LED on GPIO 30 (e22, e23) | Swap in a red LED, long leg in e22 |
| Button legs | e9, e11, f9, f11 |
| Wire from GPIO 33 | a9 (above the left leg) |
Wire from the 3V3 pin | Top + rail |
| Jumper | Top + rail to a11 (above the right leg) |
| 10K resistor (brown, black, orange) | c9 to c6 |
| Jumper | a6 to the top − rail |
Both wires go on the same side of the gap. A button's four legs are two pairs that are always joined, and pressing it joins the pairs to each other. The button only fits across the gap one way, and then each pair straddles the gap: e9 is joined to f9, and e11 to f11. So a9 and a11 are on different pairs, and only a press connects them. If the blue LED stays lit with nothing pressed, one of your wires is in the bottom half, on the same pair as the other.
The 10K resistor goes on GPIO 33's side of the button, in column 9 with the pin's wire, and runs left, away from 3V3. With the button open, it is the only thing connected to the pin, and it leads to GND, so the pin reads 0. Pressing connects the pin straight to 3V3, and the pin reads 1. Only a little current flows through 10K, so 3V3 wins easily.
from machine import Pin
blue = Pin(28, Pin.OUT)button = Pin(33, Pin.IN)
while True: if button.value() == 1: blue.value(1) else: blue.value(0)time and random.The delay has to be random. A fixed pause lets you anticipate the GO, and then you are measuring your counting, not your reflexes.
The inner while loop does nothing at all on purpose — pass just means "keep checking." The program sits there until the button goes down.
from machine import Pinimport timeimport random
blue = Pin(28, Pin.OUT)button = Pin(33, Pin.IN)
while True: print("When the blue LED turns on, push the button!") time.sleep_ms(random.randint(2000, 4000)) blue.value(1) start = time.ticks_ms()
while button.value() == 0: pass
react_time = time.ticks_diff(time.ticks_ms(), start) blue.value(0)
print("Your reaction time was", react_time, "ms") time.sleep(3)from machine import Pinimport timeimport random
blue = Pin(28, Pin.OUT)green = Pin(29, Pin.OUT)red = Pin(30, Pin.OUT)button = Pin(33, Pin.IN)
while True: print("When the blue LED turns on, push the button!") time.sleep_ms(random.randint(2000, 4000)) blue.value(1) start = time.ticks_ms()
while button.value() == 0: pass
react_time = time.ticks_diff(time.ticks_ms(), start) blue.value(0)
if react_time <= 215: green.value(1) else: red.value(1)
print("Your reaction time was", react_time, "ms") time.sleep(5) green.value(0) red.value(0)best that holds your fastest time so far.Your finished program should behave like this:
When the blue LED turns on, push the button!Your reaction time was 243 msWhen the blue LED turns on, push the button!Your reaction time was 198 msAnswer the following questions before submitting your work.
while loop to explain why.Submit the required files to the appropriate dropbox.