PyRaspBeta

← All practice problems

Raspberry Pi practice problem · Buttons & input

Button combination lock

HardBudget $6Python · gpiozero

Three buttons numbered 1, 2, 3 on GPIO5, GPIO6, GPIO13 (to ground), a green LED on GPIO17 and a red LED on GPIO27. The code is 3 – 1 – 3 – 2.

  • Collect presses in groups of four. After the fourth: if it matches the code, light green for 2 s; otherwise light red for 1 s. Then start a new group.
  • If 3 s pass without a press, throw away an unfinished group (no LED).
  • Each press counts once, however bouncy or long.

The test enters 1-2-3-3 (from 1.0 s, every 0.4 s); then 3-1 (at 4.0 and 4.4 s) and waits; then 3-1-3-2 (from 8.0 s, every 0.4 s).

More in Buttons & input