SPIKE™ Prime with Python

Claw Machine

Create a robotic hand to move bricks using conditional statements

45 min
Beginner
Years 7-9 or Key Stage 3

Questions to investigate

• How can we create conditional statement without using an if statement?

Prepare

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

Engage

(Group Discussion, 5 minutes)

Engage students in thinking about how robotic arms move. Share several images and videos of different types of robotics arms. Examples may include welding, manufacturing, shipping, picking, sorting.

Ignite a discussion with students on how the robotic arms move and work. Have students point out how robotic arms are designed.

Shift the discussion to ask students if they have ever played the arcade game that uses a claw to grab a prize. Have students compare how this is similar and different to the robotic arms they studied. Discuss their ideas.

Explore

(Small Groups, 20 minutes)

Students will explore working with conditional statements to program a robotic hand to grab objects.
Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for Robotic Hand. Ask students to build the model. The building instructions are also available at https://education.lego.com/en-us/support/spike-prime/building-instructions listed as Pass the Brick.

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.

Use the Robotic Hand
Discuss ideas for making the Robotic Hand pick up objects like the claw machine. Think about ways to program the hand to open and close to pick up objects. Students should realize that they need to set a condition for what to do for the hand to be open or closed.

Share this sample program with students. Review the program as a group to ensure students understand how it will work. The program sets the condition to close the hand by pressing and holding the left button to close the hand. When released, the hand will open.

Students will need to type this program into the programming canvas. Students should run the program.

from hub import port, button
import runloop
import motor

def pressed():
    # When the left button is pressed, it returns to open
    return button.pressed(button.LEFT)

def released():
    # When the left button is released, it returns to close
    return not button.pressed(button.LEFT)

async def main():
    await runloop.until(pressed)
    motor.run(port.F, -750)
    await runloop.until(released)
    motor.run(port.F, 750)

runloop.run(main())

After students see how the program will work, ask students to remove all the 2x4 bricks from their set. Practice using the Robotic Hand as if it is the claw machine to pick up the bricks and move them. Note, the program is set to only work one time.

Explain

(Whole Group, 5 minutes)

Discuss with students how the program worked.
Ask students questions like:
• How does the program work?
• How is a conditional statement used in the program? What are the conditions?
• What are the true and false indicating in the program?

Point out to students that this is a conditional statement even though we do not use the if statement. Discuss why this is still considered a conditional statement. The program is using a condition to run the motor based on the button being pushed and released.

Elaborate

(Small Groups, 10 minutes)

Challenge students to play a relay game.

Have two groups work together to pass a brick from one Robot Hand to another. Group 1 should grab the brick, then turn to Group 2 and try to pass the brick to them. Then Group 2 can try to pass the brick back. Challenge students to see how many times they can pass the brick back and forth.

Discuss this challenge with students. Ask them if they need to change the program in any way. Students should recognize that they will need to be able to have the hand open and close more than one time. Prompt students to think about including a while loop in their program.

Allow students time to modify their program and run it. Remind students to watch the console for error messages.

Sample Program:

from hub import port, button
import runloop
import motor

def pressed():
    # When the left button is pressed, it returns to open
    return button.pressed(button.LEFT)

def released():
    # When the left button is released, it returns to close
    return not button.pressed(button.LEFT)

async def main():
    while True:
        await runloop.until(pressed)
        motor.run(port.F, -750)
        await runloop.until(released)
        motor.run(port.F, 750)

runloop.run(main())

Allow students time to practice passing the bricks back and forth. Consider having the whole class try to pass one brick or a series of bricks through all groups as a final challenge.

Discuss the experience as a group. Ask students to define what a conditional statement is and different ways that you can create them. Students should recognize that a conditional statement is about setting condition in the program that can be true or false. In this example, the condition is set by “while True” indicating that the loop will continue as long as the condition is true. If the condition is not true, then the action will not happen.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• How did the Robotic Hand react to the conditional statement?
• What are different ways to create conditional statements?
• How can you create a program that allows you to manually indicate if the condition is met or not?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about conditional statements and how to use them with loops?
• 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 basic conditional statement.
• Program a grabber model based on set conditions.

• 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-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.