SPIKE™ Prime with Python

Controlling Motion with Tilt

Explore writing conditional statements in Python using the motion sensor

45 min
Người bắt đầu
Lớp 6-8

Questions to investigate

• How can you set conditions for how a sensor will respond?
• How does a motion sensor work?

Prepare

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

Engage

(Group Activity, 5 minutes)

Engage students in an experience with conditional statements.

Students need the following elements from their set:
• Magenta frame
• 3 different yellow elements
• 3 different red elements

Ask students to place the magenta frame in the middle of a sheet of paper with the other elements set outside the frame.

Students will identify which elements meet the given condition by placing the elements that are true (meet condition) inside the frame and false (do not meet the condition) outside the frame.

Practice with students. Tell students the condition is yellow. Discuss with students which elements meet the conditions and ask students to place those elements inside the frame. All other pieces will remain outside the frame as false.

Empty the frame between rounds.

Round 1: The condition is red.
Round 2: The condition is larger than a 2x2 brick (students may need to compare).
Round 3: The condition is any element that is not a traditional brick shape (such as an axle or a technic piece).

Discuss the experience together as a whole group. Ask students what a conditional statement is. Explain that conditional statements are pieces of code that only run when the stated condition is met or true. The Boolean values, true and false, are used in conditional statements.

Explore

(Small Groups, 20 minutes)

Students will explore creating simple conditional statements while also exploring how to use the motion sensor in the hub.

Direct students to get the hub from their set and 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. 

Tap for Action
Sensors can be used in a variety of ways. The motion sensor can be programed to take different actions based on the way you move it. Brainstorm with students how they think the hub can be moved when held in a hand. Ask students how they think the motion sensor should be programmed. What information will the software need to run the hardware correctly?

Write a pseudocode program for making the motion sensor work to show an image on the light matrix when the hub is tapped. Pseudocode is writing in words what you want the program to do.

Sample Pseudocode:
• Import PrimeHub
• Initialize hub
• If hub is tapped
• Show image
Note: The code you write out in steps does not need to match exactly what will be put in the program. This is to help student think through what is needed.   

Provide students with the sample code to use the motion sensor to show different images depending on the gesture or action taken on the hub. Students need to type this program into the programming canvas.

Ask students to run the program.

from spike import PrimeHub, LightMatrix

hub = PrimeHub()

gesture = hub.motion_sensor.wait_for_new_gesture()
if gesture == 'tapped': 
hub.light_matrix.show_image('SAD')
print('Stop Please')

Discuss the program with students. Ask students to point out things about this program that are new. Students might notice the use of “if” and double equal signs (==).

Explain to students that this program is a conditional statement. It is telling the hub to take one action if the condition is met. Discuss the condition – tapping the hub. The condition is set by the “if” statement. Only if the condition is met does the sad face appear on the hub. If the condition is not met, nothing appears on the hub.

Also point out the double equal signs to the students. A single equal sign cannot be used in our conditional statement because it is used to set the value of a variable or an object. Python uses a double equal sign to designate a comparison. A conditional statement is comparing the input to whatever condition is set.

Allow students time to investigate setting a condition in their program by changing the different inputs. The gesture is a string type input meaning that only words can be used. The options are 'shaken', 'tapped', 'doubletapped', 'falling'. The orientation options can also be referenced in the Knowledge Base Motion section under wait_for_new_gesture().

Tilt for Action
Students will explore orienting the hub different ways for action.

In addition to gestures, the motion sensor can be used to set conditions for how you orient or tilt it. Provide students with the sample code to use the motion sensor to show different images depending on the orientation or tilt of the hub. Students need to type this program into the programming canvas.   

Ask students to run the program. 

Discuss the condition met in this example – orienting or tilting the hub up. Only if the condition is met does the arrow appear on the hub. If the condition is not met, nothing appears on the hub.

Challenge students to add to their program. If the condition is met, print a saying in the console and play a sound. Allow students time to write and run their program.

Sample Code:

from spike import PrimeHub, LightMatrix, App 
 
hub = PrimeHub()  
app = App() 
 
orientation = hub.motion_sensor.wait_for_new_orientation() 
if orientation == 'up':  
hub.light_matrix.show_image(‘ARROW_N’) 
print('I am awake') 
app.play_sound('Applause 1') 

Allow students time to investigate setting a condition in their program by changing the different inputs. The orientation is a string type input meaning that only words can be used. The options are 'front', 'back', 'up', 'down', 'left', and 'right'. The orientation options can also be referenced in the Knowledge Base Motion section under wait_for_new_orientation()

Remind students to reference the Knowledge Base as needed to add additional lines of code. Also students should watch the console for error messages.

Explain

(Whole Group, 5 minutes)

Allow students to share their programs and discuss how the programs worked.

Ask students questions like:
• What is a conditional statement in a program?
• What does the “if” do to set up the condition?
• Why are the lines after the “if” indented?
• How does the motion sensor work?
• What are the conditions can you set for the motion sensor?

Elaborate

(Small Groups, 10 minutes)

Challenge students to create a game that includes using the motion sensor to interact with a story.

Ask students to create a short story that has a blank in it. The blank should be filled by an action from the motion sensor that can then show an image and play a sound. Students can use the print() function to include the words of the story in the console.

A simple example written as a pseudocode program is:
• Print(‘One day Jane was walking through the park. She looked up.’).
• Tilt motion sensor up.
• Ghost Image appears.
• Sound plays.
• Print(‘Then she woke up and realized it was a dream’).

Allow students time to create their story games. Students should write their story out in pseudocode first. Ask students to also include code comments in their program to explain the parts of the program.

Student groups should share their stories with other groups. Ask students to reflect on different ways the motion sensor can be used.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• How do conditional statements work?
• How does the motion sensor work?
• What are different ways you can use the motion sensor?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about how to use conditional statements?
• 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.

Hỗ trợ giáo viên

Students will:
• Program the motion sensor.
• Create conditional statements.

• 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-16 Incorporate existing code, media, and libraries into original programs, and give attribution.
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. ©2023 The LEGO Group. All rights reserved. Use of this site signifies your agreement to the terms of use.