STARLEAP / SPIKE PRIME PYTHON / 2026–27

Build 2026.09.22-090630-0400

On this page

Project 10: Claw Relay

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

Goal

Use a hub button to control a claw or grabber.

Build

Build Robot Hand, Grabber 1, Grabber 2, or another claw from the LEGO SPIKE Prime building instructions.

Starting port:

Change the claw_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

Before you run: test with no brick in the claw. Keep fingers clear and stop if the gears grind. The signs may need reversing for a different claw build.

Step 1: Test The Button

Open a new Python project named Claw Relay.

Start with this short code:

CODE
from hub import button
import runloop

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

async def main():
    await runloop.until(left_pressed)
    print("left button")

runloop.run(main())

Run the program and press the left button on the hub.

Observe: The console prints left button.

Step 2: Add The Claw

Update your existing program to match this version. Keep the gray reference lines; add or change the dark lines:

EDIT VIEW · Gray = already there; dark = add or change
from hub import button, port
import runloop
import motor

claw_motor = port.F

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

def left_released():
    return button.pressed(button.LEFT) == 0

async def close_claw():
    await motor.run_for_degrees(claw_motor, -45, 200)

async def open_claw():
    await motor.run_for_degrees(claw_motor, 45, 200)

async def main():
    await runloop.until(left_pressed)
    await close_claw()
    await runloop.until(left_released)
    await open_claw()
    await runloop.sleep_ms(500)
    motor.stop(claw_motor)

runloop.run(main())

Run the program.

Observe: Press left to close by a small amount. Release to open by the same amount. Start with the claw open and reduce 45 if it reaches its mechanical stop.

Step 3: Keep It Ready

Change main():

EDIT VIEW · Gray = already there; dark = add or change
async def main():
    while True:
        await runloop.until(left_pressed)
        await close_claw()
        await runloop.until(left_released)
        await open_claw()
        await runloop.sleep_ms(500)
        motor.stop(claw_motor)

Run the program.

Observe: The claw should keep responding.

Physical Challenge: Claw Relay

Move a brick from one marked spot to another.

Extra setup idea: Try Collect And Return from the Challenge Setups sheet.

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.