Controlling Motion with Tilt
Explore writing conditional statements in Python using the motion sensor
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 motion sensor
• 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 hub import light_matrix, motion_sensor
import runloop
def if_tapped():
# If the hub is tapped, return the TAPPED gesture
return motion_sensor.gesture() == motion_sensor.TAPPED
async def main():
#Wait until the hub is tapped to show a sad face
await runloop.until(if_tapped)
light_matrix.show_image(IMAGE_SAD)
print('Stop Please')
runloop.run(main())
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 options are motion_sensor.TAPPED, motion_sensor.DOUBLE_TAPPED , motion_sensor.SHAKEN, motion_sensor.FALLING, and motion_sensor.UNKNOWN. The gesture options can also be referenced in the Knowledge Base under API Modules, Hub, Motion Sensor.
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.
Note: If the hub is already in the correct orientation specified by the program, the arrow will automatically appear and the sound play.
Sample Code:
from hub import light_matrix, motion_sensor
from app import sound
import runloop
def face_up():
# Return if the hub is oriented USB port up
return motion_sensor.up_face() == motion_sensor.BACK
async def main():
await runloop.until(face_up)
light_matrix.show_image(IMAGE_ARROW_N)
print('I am awake')
await sound.play('Applause 1')
runloop.run(main())
Allow students time to investigate setting a condition in their program by changing the different inputs. The orientation refers to when a particular side of the hub is facing up. The orientation options are:
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.RIGHT
The light matrix side is motion_sensor.TOP
The battery side is motion_sensor.BOTTOM
The orientation options can also be referenced in the Knowledge Base Hub section under Motion Sensor.
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 students to rate themselves on a scale of 1-3, on their materials (parts) management today.
Teacher Support
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.