STARLEAP / SPIKE PRIME PYTHON / 2026–27
Build 2026.09.22-090630-0400
On this page
Project 13: Coach Dance Reps
You need: a LEGO SPIKE Prime set and the SPIKE App Python editor.
Goal
Use loops and timing to make a repeated physical routine.
Build
Build The Coach, Leo, or Break Dancer from the LEGO SPIKE Prime building instructions.
This packet uses The Coach:
- left foot: Port F
- right foot: Port B
Change the labels if your build uses different ports.
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
- A
forloop repeats a known number of times. range(5)means repeat 5 times.motor.run()starts a motor without waiting. A shared pause gives both motors time to move before the next command.- A loop count can become a score, rep number, or dance count.
Step 1: One Dance Move
Open a new Python project named Dance Reps.
Start with this short code:
from hub import port
import runloop
import motor
left_foot = port.F
right_foot = port.B
time_ms = 250
speed = 300
async def main():
motor.run(right_foot, speed)
motor.run(left_foot, speed)
await runloop.sleep_ms(time_ms)
motor.run(right_foot, -speed)
motor.run(left_foot, -speed)
await runloop.sleep_ms(time_ms)
motor.stop(right_foot)
motor.stop(left_foot)
runloop.run(main())
Run the program.
Observe: Both feet move together, then move back.
Step 2: Repeat It
Put the dance move inside a loop:
async def main():
for rep in range(5):
motor.run(right_foot, speed)
motor.run(left_foot, speed)
await runloop.sleep_ms(time_ms)
motor.run(right_foot, -speed)
motor.run(left_foot, -speed)
await runloop.sleep_ms(time_ms)
motor.stop(right_foot)
motor.stop(left_foot)
Run the program.
Observe: The move repeats 5 times.
Step 3: Count Reps
Add this as the first line inside the loop, indented eight spaces:
async def main():
for rep in range(5):
print(rep + 1)
motor.run(right_foot, speed)
Run the program with the console open.
Observe: The console counts 1 through 5.
Step 4: Tune The Routine
Change one value at a time:
time_msspeedrange(5)
Run the program after each change.
Physical Challenge: Rep Show
- Level 1: repeat a movement 5 times
- Level 2: count the reps in the console
- Level 3: make the routine faster but still stable
- Level 4: add a force sensor stop
Extra setup idea: Try Push Then Dance from the Challenge Setups sheet.
Teacher Check
Show your teacher:
- the loop line
- the repeated movement lines
- the number that controls how many reps happen
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.