SPIKE™ Prime with Python

Make It Physically Safe

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

45 min
Beginner
Years 7-9 or Key Stage 3

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 some 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 hub import port, sound, light_matrix, button
import motor
import runloop

def pressed():
    return button.pressed(button.LEFT)

async def main():
    await sound.beep(262, 200)
    await sound.beep(523, 200)

    #this locks the safe
    motor.stop(port.B)
    motor.reset_relative_position(port.B, 0)
    await motor.run_to_absolute_position(port.C, 270, 500)
    light_matrix.show_image(light_matrix.IMAGE_NO)

    #this unlocks the safe
    await runloop.until(pressed)
    await sound.beep(523, 200)
    await motor.run_for_time(port.C, 1000, 500)
    light_matrix.show_image(light_matrix.IMAGE_YES)
    await runloop.sleep_ms(5000)

runloop.run(main())

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 hub import port, sound, light_matrix, button
import runloop
import motor

def pressed():
    return button.pressed(button.LEFT)

async def main():
    await sound.beep(262, 200)
    await sound.beep(523, 200)

    #this locks the safe
    motor.stop(port.B)
    motor.reset_relative_position(port.B, 0)
    await motor.run_to_absolute_position(port.C, 270, 500)
    light_matrix.show_image(light_matrix.IMAGE_NO)

    #this unlocks the safe
    while True:
        print('Open Me')
        if motor.relative_position(port.B) > 180:
            await runloop.until(pressed)
            await sound.beep(523, 200)
            await motor.run_for_time(port.C, 1000, 500)
            light_matrix.show_image(light_matrix.IMAGE_YES)
            await runloop.sleep_ms(5000)
            break
        else:
            print('Still Locked!')
            await runloop.sleep_ms(1000)

    print('You may enter!')
runloop.run(main())

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 hub import port, sound, light_matrix, button
import runloop
import motor

def pressed():
    return button.pressed(button.LEFT)

async def main():
    await sound.beep(262, 200)
    await sound.beep(523, 200)

    #this locks the safe
    motor.stop(port.B)
    motor.reset_relative_position(port.B, 0)
    await motor.run_to_absolute_position(port.C, 270, 500)
    light_matrix.show_image(light_matrix.IMAGE_NO)

    #this unlocks the safe
    while True:
        print('Open Me')
        await runloop.until(pressed)
        if motor.relative_position(port.B) > 180:
            await sound.beep(523, 200)
            await motor.run_for_time(port.C, 1000, 500)
            light_matrix.show_image(light_matrix.IMAGE_YES)
            await runloop.sleep_ms(5000)
            break
        else:
            print('Still Locked!')
            await runloop.sleep_ms(1000)

    print('You may enter!')
runloop.run(main())

Ask students how this program is different?
Note: You must press both buttons at the same time to get the safe to open.
Allow time for students to re-work the program. Ask groups to share 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 students to rate themselves on a scale of 1-3, on their materials (parts) management today.

Teacher Support

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.