STARLEAP / SPIKE PRIME PYTHON / 2026–27

Build 2026.09.22-090630-0400

On this page

Project 14: Safe Deposit Box Lock

You need: a LEGO SPIKE Prime set and the SPIKE App Python editor.

Goal

Make a simple code lock using hub buttons.

Build

Build Safe-Deposit Box or Super-Safe-Deposit Box from the LEGO SPIKE Prime building instructions.

Starting port:

Change the lock_motor label if your build uses a different port.

Using the code: gray lines are already in your program; dark lines are the edit. Keep the shown indentation. Replace a displayed main() definition as a whole, keeping imports and the final start call.

What You Will Learn

Step 1: Open The Door Motor

Open a new Python project named Code Lock.

Start with this short code:

CODE
from hub import port
import runloop
import motor

lock_motor = port.A

async def main():
    await motor.run_to_absolute_position(lock_motor, 90, 300)
    await runloop.sleep_ms(1000)
    await motor.run_to_absolute_position(lock_motor, 0, 300)

runloop.run(main())

Run the program.

Observe: The lock or door motor moves out and back.

Step 2 - Count Separate Presses

Change the first import to from hub import button, port. Add these helpers above main() at the left edge. The release test prevents one long press from being counted many times.

CODE
def left_pressed():
    return button.pressed(button.LEFT) != 0

def buttons_released():
    return (button.pressed(button.LEFT) == 0
            and button.pressed(button.RIGHT) == 0)

Replace only main() with this version. Keep the imports, lock_motor, the helpers, and the final start call.

EDIT VIEW · Gray = already there; dark = add or change
async def main():
    left_count = 0
    while True:
        if left_pressed():
            left_count += 1
            print("count:", left_count)
            await runloop.until(buttons_released)
        await runloop.sleep_ms(50)

Run the program. Press and release left three times. The console should count 1, 2, 3. Hold left down: it should count only once. Stop the program before editing.

Step 3 - Set A Secret And Test Right

Add a variable beside lock_motor, at the left edge.

EDIT VIEW · Gray = already there; dark = add or change
lock_motor = port.A
secret_count = 3

Add a right-button helper above main().

CODE
def right_pressed():
    return button.pressed(button.RIGHT) != 0

Add the dark block after the left-button block, inside while True. The two button tests line up at eight spaces. Keep the final pause inside the loop.

EDIT VIEW · Gray = already there; dark = add or change
            await runloop.until(buttons_released)

        if right_pressed():
            if left_count == secret_count:
                print("correct")
                break
            else:
                print("try again")
                left_count = 0
                await runloop.until(buttons_released)

        await runloop.sleep_ms(50)

Run the program. Try two left presses, then right: the console should print try again and reset the count. Then try three left presses and right: it should print correct and exit the loop. break ends the nearest loop.

Step 4 - Open Only After A Correct Code

Add this action helper above main(). Its movement is the one you already tested in Step 1.

CODE
async def open_lock():
    await motor.run_to_absolute_position(lock_motor, 90, 300)

Add the dark call inside the correct-code branch, before break.

EDIT VIEW · Gray = already there; dark = add or change
        if right_pressed():
            if left_count == secret_count:
                print("correct")
                await open_lock()
                break

Run the program from the closed position. Try a wrong code first: the motor should stay still. Enter the correct code: the lock should open. Stop the program and reset the build to the same starting position before another trial.

Step 5 - Change The Secret

Change only secret_count = 3 to secret_count = 4, then run the program. Show that the old code fails and the new code works. Use distinct presses and release both buttons between them.

Fix it: left_count = 0 belongs before the loop so the count can grow. The reset in the wrong-code branch is intentional. Keep await open_lock() inside the matching-count branch so a wrong code cannot open the box.

Physical Challenge: Code Lock

Teacher Check

Show your teacher:

Before You Put The Kit Away

Save your program. Stop the program before moving wires or changing the build. Show the teacher your working behavior, point to one changed line, and explain what it controls. Keep your place for next time.