SPIKE™ Prime with Python

Make It Physically Safe

Use conditional statements to create a physically protected place to store information

45 min
Principiante
Scuola secondaria di I grado

Questions to investigate

• How can vulnerable information be kept safe in a physical space?

Prepare

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

Engage

(Group Discussion, 5 minutes)

Engage students in a conversation about safes and protecting physical items.

Spark a discussion with students about ways to physically protect important information. Prompt students to think about where this information could be kept and how locks might be used.

Ask students questions like:
• What type of devices can be used to lock something away?
• What makes locks strong?
• What are ways that you can create a lock or a device that locks and unlocks (i.e. using a key, a password, etc.).
• What can make a locked door or box even stronger?

Let students complete research and view images of different types of locks as needed.

Explore

(Small Groups, 20 minutes)

Students will investigate different types of grabbers to consider the best tool for cleaning up trash.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for Safe Deposit Box 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.   
Note: this model is also used for the next lesson Make a Safer Safe. The motor attached will not be used in this lesson, but will be in the next lesson.

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.  

Have students investigate how the safe works to lock and then unlock when the appropriate action is taken. Ask students to type the sample program into their programming canvas and run it.

Sample program:

from spike import PrimeHub, LightMatrix, Button, Motor
from spike.control import wait_for_seconds
from spike.operator import greater_than

hub = PrimeHub() 
lock_motor=Motor('C')
dial_motor=Motor('B')
lock_motor.set_default_speed(50)

hub.speaker.beep(60)
hub.speaker.beep(72)

\#this locks the door
dial_motor.set_stop_action('coast')
dial_motor.set_degrees_counted(0)
hub.light_matrix.show_image('NO')

\#this unlocks the door
hub.left_button.wait_until_pressed()
hub.speaker.beep(72)
lock_motor.run_for_seconds(1)
hub.light_matrix.show_image('YES')
wait_for_seconds(5)

Discuss the program together as a group paying attention to how the safe currently locks. Ask students to think about how protected any information in the safe would currently be. Note that there is only one step of protection using the safe.

Time for an Upgrade
Students will explore a nested loop to create a more secure safe.

Challenge students to change their code to include a nested conditional statement. Explain to students something is nested when one part is put in another part. Loops and conditional statements can be nested within each other to create a more complex program. Ask students to begin by creating a flowchart to show how the program should work. Allow students time to complete their flowcharts and discuss them together as a group.

Allow students time to create their new program that includes the nested loop. Remind students to use code comments in their program to explain what the program should do. Students should also watch the console for error messages.

Sample Program:

from spike import PrimeHub, LightMatrix, Button, Motor
from spike.control import wait_for_seconds, wait_until
from spike.operator import greater_than

hub = PrimeHub()
lock_motor=Motor('C')
dial_motor=Motor('B')
lock_motor.set_default_speed(50)

hub.speaker.beep(60)
hub.speaker.beep(72)

#Make sure the door is closed. This locks the door.
lock_motor.run_for_rotations(-.5)
dial_motor.run_to_position(10)
hub.light_matrix.show_image('NO')

#Step 1 to unlock the door - Turn the dial on the door
while True:
    print('open me')
    wait_until(dial_motor.get_position, greater_than, 180)
    hub.speaker.beep(72)
#Step 2 to unlock the door - press the Left Button on the hub
    if hub.left_button.is_pressed():
        hub.speaker.beep(60)
        lock_motor.run_for_rotations(.5)
        hub.light_matrix.show_image('YES')
        break

    else:
        print('still locked!')

    print('You may enter!')

Allow students time to share their final programs.

Explain

(Whole Group, 5 minutes)

Discuss with students how the program worked.
Ask students questions like:
• How does the safe work to provide security?
• How safe do you think the safe is?
• How does the nested conditional statement work?
• What was difficult about this challenge? Where did you run into programming trouble?
• What does the line of code with “break” do?

Explain to students that the break statement terminates the loop.

Elaborate

(Small Groups, 10 minutes)

Challenge students to change the order of the two step protection and identify the error in the program.

Ask students to consider how they can change the order of the steps for opening the safe. Share the sample program for students to investigate. There are no errors in this program. However, the program is not working to open the safe. Ask students to identify where the problem is and debug the program.

Sample Program:

from spike import PrimeHub, LightMatrix, Button, Motor
from spike.control import wait_for_seconds, wait_until
from spike.operator import greater_than

hub = PrimeHub()
lock_motor=Motor('C')
dial_motor=Motor('B')
lock_motor.set_default_speed(50)

hub.speaker.beep(60)
hub.speaker.beep(72)

\#Make sure the door is closed.This locks the door.
lock_motor.run_for_rotations(-.5)
dial_motor.run_to_position(10)
hub.light_matrix.show_image('NO')

\#Step 1 to unlock the door - press the Left Button on the hub
while True:
    print('open me')
    hub.left_button.wait_until_pressed()
    hub.speaker.beep(72)
\#Step 2 to unlock the door - Turn the dial on the door
    if dial_motor.get_position()>180:
        hub.speaker.beep(60)
        lock_motor.run_for_rotations(.5)
        hub.light_matrix.show_image('YES')
        break
    
    else:
        print('still locked!')

    print('You may enter!')

Allow time for students to re-work the program. Ask groups to share our their final program.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What happened when you created a nested conditional statement?
• How does nesting the conditional statements make the program more complicated and the safe safer?
• What are some errors to watch for when programming compound conditionals?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using nested 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.

Supporto per l'insegnante

Students will:
• Investigate nested conditional statements
• Explore physical security measures

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

CSTA
2-NI-05 Explain how physical and digital security measures protect electronic information.
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-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.

Legal NoticePrivacy PolicyCookies

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.