SPIKE™ Prime with Python

Stretch Your Muscles and Lists

Use lists to determine the moves to make

90-120 min.
Intermed.
Years 7-9 or Key Stage 3

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 up_face, 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 hub import motion_sensor
import runloop

async def main():
    # Create a list with the hub's orientation
    yoga_moves = [motion_sensor.FRONT, motion_sensor.BACK, motion_sensor.LEFT, motion_sensor.RIGHT]

    orientation = motion_sensor.up_face()

    while True:        
        await runloop.until(lambda: motion_sensor.up_face() != orientation)
        orientation = motion_sensor.up_face()
        
        if orientation == yoga_moves[3]:
            print('Good Job!')
            await runloop.sleep_ms(1000)

runloop.run(main())

Note: The speaker side is motion_sensor.FRONT, the USB side is motion_sensor.BACK, the A, C, E port side is motion_sensor.LEFT, the B, D, F port side is motion_sensor.ROGHT, the light matirx is motion_sensor.TOP, the battery side is motion_sensor.BOTTOM.

Discuss the program with students. Ask students to identify what each position or index value is in the list. For example, the motion_sensor.LEFT is index value 3 and motion_sensor.BAXK is index value 1. 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 hub import motion_sensor, light_matrix
import runloop

async def main():
    # Create a list with the hub's orientation
    yoga_moves = [motion_sensor.FRONT, motion_sensor.BACK, motion_sensor.LEFT, motion_sensor.RIGHT]

    # Follow this routine
    print('LEFT')
    print('RIGHT')
    print('BACK')
    print('FRONT')
    print('READY GO!')
    orientation = motion_sensor.up_face()
    
    while True:        
        await runloop.until(lambda: motion_sensor.up_face() != orientation)
        orientation = motion_sensor.up_face()
        if orientation == yoga_moves[2]:
            print('Left Side')
            light_matrix.show_image(light_matrix.IMAGE_ARROW_W)
        elif orientation == yoga_moves[3]:
            print('Right Side')
            light_matrix.show_image(light_matrix.IMAGE_ARROW_E)
        elif orientation == yoga_moves[1]:
            print('Back')
            light_matrix.show_image(light_matrix.IMAGE_ARROW_N)
        elif orientation == yoga_moves[0]:
            print('Front')
            light_matrix.show_image(light_matrix.IMAGE_ARROW_S)

runloop.run(main())

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 hub import motion_sensor, light_matrix
import runloop
import random

async def main():
    # Create a list with the hub's orientation
    yoga_moves = [motion_sensor.FRONT, motion_sensor.BACK, motion_sensor.LEFT, motion_sensor.RIGHT]
    random_move = random.choice(yoga_moves)

    # Follow this routine
    print('LEFT')
    print('RIGHT')
    print(random_move)
    print('BACK')
    print('FRONT')
    print(random_move)
    print('READY GO!')

    orientation = motion_sensor.up_face()
    
    while True:
        await runloop.until(lambda: motion_sensor.up_face() != orientation)
        orientation = motion_sensor.up_face()

        if orientation == yoga_moves[2]:
            print('Left Side')
            light_matrix.show_image(light_matrix.IMAGE_ARROW_W)
        elif orientation == yoga_moves[3]:
            print('Right Side')
            light_matrix.show_image(light_matrix.IMAGE_ARROW_E)
        elif orientation == random_move:
            print('Random')
            light_matrix.show_image(light_matrix.IMAGE_SILLY)
        elif orientation == yoga_moves[1]:
            print('Back')
            light_matrix.show_image(light_matrix.IMAGE_ARROW_N)
        elif orientation == yoga_moves[0]:
            print('Front')
            light_matrix.show_image(light_matrix.IMAGE_ARROW_S)

runloop.run(main())

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 top and bottom 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:

from hub import motion_sensor, light_matrix
import runloop

async def main():
    # Create a list with the hub's orientation
    yoga_moves = [motion_sensor.FRONT, motion_sensor.BACK, motion_sensor.LEFT, motion_sensor.RIGHT]

    yoga_moves.insert(0, motion_sensor.TOP)
    yoga_moves.insert(1, motion_sensor.BOTTOM)

    orientation = motion_sensor.up_face()

    while True:        
        await runloop.until(lambda: motion_sensor.up_face() != orientation)
    orientation = motion_sensor.up_face()

        if orientation == yoga_moves[0]:
            print('Top')
            light_matrix.show_image(light_matrix.IMAGE_TRIANGLE)
        elif orientation == yoga_moves[1]:
            print('Bottom')
            light_matrix.show_image(light_matrix.IMAGE_SQUARE)

runloop.run(main())

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 motion_sensor.BOTTOM’ from the list.

yoga_moves = [motion_sensor.FRONT, motion_sensor.BACK, motion_sensor.LEFT, motion_sensor.RIGHT]
print(yoga_moves)
del yoga_moves[1]
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 students to rate themselves on a scale of 1-3, on their materials (parts) management today.

Teacher Support

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.