EN US SPIKE Prime Python Programs - Feb 2024
EN US SPIKE Prime Python Programs - Feb 2024
Table of Contents
Help!
runloop.run(main())
3
LEGO® Education SPIKE™ Prime
Hopper Race
await light_matrix.write(‘2’)
await runloop.sleep_ms(1000)
await light_matrix.write(‘1’)
await runloop.sleep_ms(1000)
light_matrix.clear()
runloop.run(main())
4
LEGO® Education SPIKE™ Prime
Super Cleanup
runloop.run(main())
5
LEGO® Education SPIKE™ Prime
Broken
# X motor is in port A.
# Y motor is in port C.
runloop.run(main())
6
LEGO® Education SPIKE™ Prime
Design for
Someone
while True:
if button.pressed(button.RIGHT):
# Make the prothesis let go.
await motor.run_to_absolute_position(port.A, 0, 750)
await motor.run_to_absolute_position(port.E, 0, 750)
break
runloop.run(main())
7
LEGO® Education SPIKE™ Prime
sound.play(‘Connect’)
pixels = [100] * 4
distance_sensor.show(port.C, pixels)
for x in range(10):
light_matrix.show_image(light_matrix.IMAGE_HEART)
await runloop.sleep_ms(500)
light_matrix.show_image(light_matrix.IMAGE_HEART_SMALL)
await runloop.sleep_ms(500)
light_matrix.show_image(light_matrix.IMAGE_HEART)
while True:
# Wait for the color sensor to see magenta.
await runloop.until(lambda: color_sensor.color(port.D) is color.MAGENTA)
await motor.run_for_degrees(port.A, 30, 500)
await motor.run_for_degrees(port.A, -60, 500)
await motor.run_for_degrees(port.A, 60, 500)
await motor.run_for_degrees(port.A, -30, 500)
sound.play(‘Connect’)
light_matrix.show_image(light_matrix.IMAGE_HEART)
runloop.run(main())
8
LEGO® Education SPIKE™ Prime
Out of Order
runloop.run(main())
9
LEGO® Education SPIKE™ Prime
Track your
Packages
runloop.run(main())
10
LEGO® Education SPIKE™ Prime
Keep it Safe
runloop.run(main())
11
LEGO® Education SPIKE™ Prime
Keep it
really safe!
# While the left button is not pressed or the dial motor is less than 180 degrees,
# Move the dial cover closer to the dial.
while not button.pressed(button.LEFT) or motor.relative_position(port.B) < 180:
await sound.beep(262, 200)
await motor.run_for_degrees(port.E, 15, 500)
await runloop.sleep_ms(800)
# If the left button is pressed and the dial is greater than 180 degrees, open the safe.
light_matrix.show_image(light_matrix.IMAGE_YES)
await motor.run_to_absolute_position(port.E, 0, 500)
await motor.run_for_time(port.C, 1000, 500)
await app.sound.play(‘Wand’)
runloop.run(main())
12
LEGO® Education SPIKE™ Prime
Automate it!
runloop.run(main())
13
LEGO® Education SPIKE™ Prime
Break Dance
for x in range(10):
await light_matrix.write(‘1’)
# Move arms and legs at the same time.
motor.run_for_degrees(port.F, 360, -800)
await motor.run_for_degrees(port.B, 360, -800)
await runloop.sleep_ms(450)
await light_matrix.write(‘2’)
# Move arms and legs at the same time.
motor.run_for_degrees(port.F, 360, -800)
await motor.run_for_degrees(port.B, 360, -800)
await runloop.sleep_ms(450)
await light_matrix.write(‘3’)
# Move arms and legs at the same time.
motor.run_for_degrees(port.F, 360, -800)
await motor.run_for_degrees(port.B, 360, -800)
await runloop.sleep_ms(450)
runloop.run(main())
14
LEGO® Education SPIKE™ Prime
Repeat 5 Times
# Leg motors are paired with the left motor in port F an the right motor in port B.
motor_pair.pair(motor_pair.PAIR_1, port.F, port.B)
runloop.run(main())
15
LEGO® Education SPIKE™ Prime
Rain or shine?
Currently, it isn’t possible to use the weather forecast functions with our Python programming language.
runloop.run(main())
16
LEGO® Education SPIKE™ Prime
Wind Speed
Currently, it isn’t possible to use the weather forecast functions with our Python programming language.
# If the wind speed is less than 5.5 move the indicator to the correct position.
if WIND_SPEED_FORECAST < 5.5:
await motor.run_for_degrees(port.A, 30, 200)
await runloop.sleep_ms(1000)
await motor.run_for_degrees(port.A, -30, 200)
else:
await motor.run_for_degrees(port.A, 60, 200)
await runloop.sleep_ms(1000)
await motor.run_for_degrees(port.A, -60, 200)
runloop.run(main())
17
LEGO® Education SPIKE™ Prime
Veggie Love
Currently, it isn’t possible to use the weather forecast functions with our Python programming language.
WEEK_RAIN = 50
ROTATION = 0
await light_matrix.write(str(abs(motor.relative_position(port.E))))
await light_matrix.write(str(WEEK_RAIN))
print(WEEK_RAIN)
runloop.run(main())
18
LEGO® Education SPIKE™ Prime
Brain Game
from hub import light_matrix, button, port #this will read and record its sequence of
import runloop colors in the list called candy 2
import motor for x in range(5):
import color_sensor candy2.append(color_sensor.col-
import color or(port.B))
from app import sound await runloop.sleep_ms(1000)
await motor.run_for_degrees(port.A, 95,
500)
async def main():
#this makes the Game Master eat candy stick 1 for x in range(5):
await runloop.until(lambda: button. print(candy1[x])
pressed(button.LEFT))
light_matrix.clear()
candy1.clear() if candy1_red_index == candy2_red_index:
await motor.run_for_time(port.A, 2000, -500) for x in range(5):
await sound.play(‘Bite’) light_matrix.set_pixel(x, candy1_
await sound.play(‘Bite’) red_index, 100)
await sound.play(‘Win’)
19
LEGO® Education SPIKE™ Prime
The Coach
start_time = time.ticks_ms()
await runloop.sleep_ms(200)
motor.set_duty_cycle(port.B, 7000)
motor.set_duty_cycle(port.F, -7000)
await runloop.sleep_ms(200)
motor.stop(port.B)
motor.stop(port.F)
runloop.run(main())
20
LEGO® Education SPIKE™ Prime
Training Camp 1
runloop.run(main())
21
LEGO® Education SPIKE™ Prime
Training Camp 2
await runloop.sleep_ms(1000)
# Drive base will travel forward until the distance sensor senses closer than 100 mm.
motor_pair.move(motor_pair.PAIR_1, 0, velocity=300)
await runloop.until(lambda: distance_sensor.distance(port.F) < 100)
motor_pair.stop(motor_pair.PAIR_1)
# (Distance / 17.5) * 360 will get you the degrees needed to travel the distance needed.
# To travel 20 cm, calculate (20 / 17.5 * 360 = 411)
# To move the drive base in reverse you make the degrees negative
await motor_pair.move_for_degrees(motor_pair.PAIR_1, -411, 0, velocity=300)
runloop.run(main())
22
LEGO® Education SPIKE™ Prime
Training Camp 3
while True:
# If the left button is pressed, the drive base stops at a black line.
if button.pressed(button.LEFT):
motor_pair.move(motor_pair.PAIR_1, 0, velocity=velocity_value)
await runloop.until(lambda: color_sensor.color(port.B) is color.BLACK)
motor_pair.stop(motor_pair.PAIR_1)
# If the right button is pressed, the drive base follows a blcak line,
if button.pressed(button.RIGHT):
while True:
motor_pair.move_tank(motor_pair.PAIR_1, 0, velocity_value)
await runloop.until(lambda: color_sensor.color(port.B) is color.BLACK)
motor_pair.move_tank(motor_pair.PAIR_1, velocity_value, 0)
await runloop.until(lambda: color_sensor.color(port.B) is color.WHITE)
runloop.run(main())
23
LEGO® Education SPIKE™ Prime
Advanced
Driving Base
await right_turn()
await runloop.sleep_ms(1000)
await left_turn()
runloop.run(main())
24
LEGO® Education SPIKE™ Prime
My Code,
Our Program
await triange()
await sound.beep(262, 200)
await circle()
await sound.beep(262, 200)
runloop.run(main())
25
LEGO® Education SPIKE™ Prime
Time for an
Upgrade
runloop.run(main())
26
LEGO® Education SPIKE™ Prime
Mission Ready
from hub import port # Back up and lower the dozer blade.
import runloop await motor_pair.move_for_degrees(motor_pair.
import motor_pair PAIR_1, 91, 0, velocity= 250)
import motor await motor.run_for_degrees(port.C, -180, 600)
# The dozer blade motor is in port C. # Turn to drive around the mission.
# The lift arm motor is in port D. await motor_pair.move_for_degrees(motor_pair.
# The drive motors are in port A (left) and port E PAIR_1, 146, -100, velocity= 250)
(right). await motor_pair.move_for_degrees(motor_pair.
# The wheel circumference is 27.63 cm. PAIR_1, 100, 0, velocity= 250)
await motor_pair.move_for_degrees(motor_pair.
PAIR_1, 600, -25, velocity= 250)
async def main():
# Drive to the other side of the mission.
await motor_pair.move_for_degrees(motor_pair.
# Pair the motors. PAIR_1, 1250, 0, velocity= 250)
motor_pair.pair(motor_pair.PAIR_1, port.A, # Turn and drive forward to align to the gates.
port.E) await motor_pair.move_for_degrees(motor_pair.
PAIR_1, 300, -50, velocity= 250)
await motor_pair.move_for_degrees(motor_pair.
# Prepare the dozer blade and lift arm. PAIR_1, 228, 0, velocity= 250)
motor.run_for_time(port.C, 1000, -1000) await motor_pair.move_for_degrees(motor_pair.
await motor.run_for_time(port.D, 1000, -1000) PAIR_1, 140, -100, velocity= 250)
await motor.run_for_degrees(port.C, 70, 1000) # Drive forward to place lift arm through the
await motor.run_for_degrees(port.D, 20, 1000) gates.
await motor_pair.move_for_degrees(motor_pair.
PAIR_1, 450, 0, velocity= 250)
# Move forward to push in the lock bar.
await motor_pair.move_for_degrees(motor_pair.
PAIR_1, -26, 0, velocity= 250) # Lift the arm to get the gates.
# Back up and lift the dozer blade. await motor.run_for_degrees(port.D, 100, 1000)
await motor_pair.move_for_degrees(motor_pair.
PAIR_1, 137 , 0, velocity= 250)
await motor.run_for_degrees(port.C, 180, 400) runloop.run(main())
27
LEGO® Education SPIKE™ Prime
while True:
# Wait for the left button to be pressed.
await runloop.until(lambda: button.pressed(button.LEFT))
motor.run(port.F, -750)
runloop.run(main())
28
LEGO® Education SPIKE™ Prime
Ideas,
the LEGO way!
# If the right button is pressed count down 5, 4, 3, 2, 1... one minute at a time.
if button.pressed(button.RIGHT):
await light_matrix.write(‘5’)
await runloop.sleep_ms(60000)
await light_matrix.write(‘4’)
await runloop.sleep_ms(60000)
await light_matrix.write(‘3’)
await runloop.sleep_ms(60000)
await light_matrix.write(‘2’)
await runloop.sleep_ms(60000)
await light_matrix.write(‘1’)
await runloop.sleep_ms(60000)
light_matrix.clear()
await sound.beep(262, 500)
await sound.beep(523, 500)
runloop.run(main())
29
LEGO® Education SPIKE™ Prime
What is this?
runloop.run(main())
30
LEGO® Education SPIKE™ Prime
Going the
Distance
runloop.run(main())
31
LEGO® Education SPIKE™ Prime
Goal!
# Wait for the left button to be pressed to run the motor for 360 degrees.
await runloop.until(lambda: button.pressed(button.LEFT))
await motor.run_for_degrees(port.A, 360, 1000)
# Wait for one second before being able to press the button again.
await runloop.sleep_ms(1000)
runloop.run(main())
LEGO, the LEGO logo and the SPIKE logo are trademarks and/or copyrights of the LEGO Group. ©2023 The LEGO Group. All rights reserved. 32