SPIKE™ Prime with Python

Stretch Your Muscles and Lists

Use lists to determine the moves to make

90-120 min.
Gevorderd
Groep 8 tot en met de tweede klas

Questions to investigate

• How can a physical fitness trainer create a list of movements?

Prepare

• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.

Engage

(Group Activity, 15 minutes)

Engage students in a conversation about stretching.

Discuss different ways that we can stretch our muscles. Consider showing images or video clips of people stretching to help students visualize what stretching looks like.

Prompt students to get up and move in order to stretch their muscles. Play a follow-the-leader type game where students have to match your movements exactly as you either demonstrate from the front or move around the room. Consider selecting a student to act as the leader and then passing the role to another student.

Now that students are all stretched out, have them think about ways that they moved their body. Discuss ways to create lists of the movements or muscles used and how they might be categorized.

Explore

(Small Groups, 45 minutes)

Students will investigate different ways to move and stretch their muscles.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for the Yoga Ring model.  Ask students to build the model. The building instructions are also available at https://education.lego.com/en-us/support/spike-prime/building-instructions.   

Direct students to open a new project in the Python programming canvas. Ask students to erase any code that is already in the programming area. Students should connect their hub.  

Work with students to create a program that uses the values for the motion sensor as the variables in their list. Direct students to find motion sensor in the knowledge base. Under wait for new orientation, students can reference the values that can be used for the motion sensors orientation. Allow students time to try their program in with different options from the list.

Sample Program:

from spike import PrimeHub, MotionSensor

hub = PrimeHub() 
yoga_moves=['leftside', 'rightside', 'front', 'down']

orientation=hub.motion_sensor.wait_for_new_orientation() 

if orientation== yoga_moves[1]:
    print('good job')

Note: Leftside refers to the hub orientation when the left side of the hub is pointed up. Rightside refers to the right side pointed up.

Discuss the program with students. Ask students to identify what each position or index value is in the list. For example, the leftside is index value 0 and down is index value 3. Explain to students that the index values always start with 0 in Python.

Create a Yoga Routine
Challenge students to create a yoga routine by creating a list of the options and then printing the moves in order chosen in the console.

Ask students to modify their programs to include several moves from their list. They should print the list of moves in the console so that another team could follow their yoga routine. Allow students time to practice their moves.

Sample Program:

from spike import PrimeHub, MotionSensor, LightMatrix

hub = PrimeHub()

yoga_moves = ['leftside', 'rightside', 'up', 'down']

\# Follow this routine
print('leftside')
print('rightside')
print('up')
print('down')
print('ready go!')

\# move your yoga ring to match the moves in the list above. 
orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[0]:
    print('leftside')
    hub.light_matrix.show_image('ARROW_W')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[1]:
    print('rightside')
    hub.light_matrix.show_image('ARROW_E')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[2]:
    print('up')
    hub.light_matrix.show_image('ARROW_N')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[3]:
    print('down')
    hub.light_matrix.show_image('ARROW_S')

Encourage students to mix up the moves and have several steps in their routine.

Get Random with Your Moves
After students practice their routine, challenge them to add a random choice line of code to change the routine with an unexpected move.

Sample Program:

from spike import PrimeHub, MotionSensor, LightMatrix
import random  

hub = PrimeHub()

yoga_moves = ['leftside', 'rightside', 'up', 'down']
random_move = random.choice(yoga_moves)

\# Follow this routine
print('leftside')
print('rightside')
print(random_move)
print('up')
print('down')
print(random_move)
print('ready go!')

\# move your yoga ring to match the moves in the list above. 
orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[0]:
    print('leftside')
    hub.light_matrix.show_image('ARROW_W')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[1]:
    print('rightside')
    hub.light_matrix.show_image('ARROW_E')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == random_move:
    print('random!')
    hub.light_matrix.show_image('SILLY')
  
orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[2]:
    print('up')
    hub.light_matrix.show_image('ARROW_N')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[3]:
    print('down')
    hub.light_matrix.show_image('ARROW_S')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == random_move:
    print('random!')
    hub.light_matrix.show_image('SILLY')

Allow time for students to try their programs and play with different ways to include the random choice line of code.

Explain

(Whole Group, 15 minutes)

Ask students to demonstrate their routine and discuss with students how the program worked. Ask students questions like:
• How did you use a list to create your yoga routine?
• What happened when you added random moves to your routine?
• What are some ideas for how you could change your program to create a new routine?

Elaborate

(Small Groups, 25 minutes)

Challenge students to stretch this list and their routine to add some new moves.

Ask students to modify their program to include some new moves. First, students will need to add two new options to their list that allows for the motion sensor to be oriented in the front and back position. However, students need to insert these new moves without changing the current list programed (i.e. they cannot just add the two elements to the end of the existing list).

Introduce the insert line of code list_name.insert(index location, value) to the students and then ask them to add the sample program below to the end of their current program. Students should run the program to see how the newly inserted lines work.

Sample Program:

yoga_moves.insert(4, 'front')
yoga_moves.insert(5, 'back')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[4]:
    print('front')
    hub.light_matrix.show_image('TRIANGLE')

orientation = hub.motion_sensor.wait_for_new_orientation()
if orientation == yoga_moves[5]:
    print('back')
    hub.light_matrix.show_image('SQUARE')

Point out to students where the new value is inserted into the list.

After practicing their own new yoga routine, allow groups to challenge each other in preforming their yoga routines.

Consider also sharing with students how to delete an element from their list using the del listname[index value] line of code, which in this example might look like del yoga_moves[1] to remove ‘rightside’ from the list.

from spike import PrimeHub, MotionSensor
hub = PrimeHub()
yoga_moves = ['leftside', 'rightside', 'front', 'down', 'up', 'back']
print(yoga_moves)
del yoga_moves[4]
print(yoga_moves)

Evaluate

(Group Exercise, 10 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• How are you able to add new elements to or delete elements from your list?
• How were you able to create your yoga plan using your list?
• How were you able to use lists and sensors together?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using lists to create a plan, like a yoga routine, to follow?
• What characteristics of a good teammate did I display today?
• Ask students to rate themselves on a scale of 1-3, on their time management today.
• Ask student to rate themselves on a scale of 1-3, on their materials (parts) management today.

Ondersteuning voor de leraar

Students will:
• Create a program using values for the motion sensor as the variables in their list
• Use a list to create a yoga routine

• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Student journals

CSTA
2-CS-02 Design projects that combine hardware and software components to collect and exchange data.
2-AP-10 Use flowcharts and/or pseudocode to address complex problems as algorithms
2-AP-13 Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
2-AP-17 Systematically test and refine programs using a range of test cases.
2-AP-19 Document programs in order to make them easier to follow, test, and debug.

LEGO, the LEGO logo, the Minifigure, DUPLO, the SPIKE logo, MINDSTORMS and the MINDSTORMS logo are trademarks and/or copyrights of the LEGO Group. ©2022 The LEGO Group. All rights reserved. Use of this site signifies your agreement to the terms of use.