Create a program with infinite loops
Questions to investigate
• How can you create loops where the conditions always are true?
• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.
• Ensure students have the Coach model built, which was used in the Dance Loop with Coach lesson.
(Group Discussion, 10 minutes)
Engage students in a conversation about setting conditions.
Prompt students to brainstorm examples of when they need to meet certain conditions. Easy examples can include deciding to eat only when hungry or cleaning your room only when someone tells you to. Have students name the conditions in each case.
Next, challenge students to find an example of a time when a condition is always true. Students should brainstorm ideas of a condition that could be set that would not stopping being true (no false for the condition). An example might include the sun rising each day. Prompt students to write a statement that sets sun rise as a condition. Example: If the sun rises, then I will see light outside. This is true every day because the sun rises every day.
Explain to students that a loop with a condition that is always true is an infinite loop. Infinite loops are while loops that can never become false.
(Small Groups, 45 minutes)
Students will explore programming the Coach model to move using an infinite loop.
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.
Explain to students that Coach wants to take us on a long hike now that we are all warmed up. Discuss with students how we can include a loop for moving the Coach model using a while loop that sets a condition for the movement, but the condition is created in a way that it is always true (cannot be false).
Ask students to review their program from the Setting Condition for Yoga lesson. Ask students to brainstorm ways to set the conditions so that the while loop is always true and therefore does not end.
Sample Program from Setting Condition for Yoga:
from spike import Motor \# initialize motors and sensor. Set variable names motor_b=Motor('B') motor_f=Motor('F') count = 2 \# run motors when count less than 5 while count < 5: motor_f.run_for_seconds(0.3, 40) motor_b.run_for_seconds(0.3, 40) motor_f.run_for_seconds(0.3, -40) motor_b.run_for_seconds(0.3, -40) count=count+1
Allow students time to try several different ideas to ensure the condition is always true. Sample solutions include setting the count at 1 and the while count at greater than 0. This program will continue to run continuously because the condition will continue to be true.
Remind students to watch the console for errors as they are investigating.
Introduce a new line of code to students that will allow them to accomplish the goal to create a condition for the loop that is always true (cannot become false).
Share this line of code with students and ask them where they think it belongs in the program they just created (what it should replace).
Ask students to change their program to include the while True line of code which will allow the program to loop infinitely.
from spike import Motor \# initialize motors and sensor. Set variable names motor_b=Motor('B') motor_f=Motor('F') \# run motors when count less than 5 while True: motor_f.run_for_seconds(0.3, 40) motor_b.run_for_seconds(0.3, 40) motor_f.run_for_seconds(0.3, -40) motor_b.run_for_seconds(0.3, -40)
(Whole Group, 10 minutes)
Discuss with students how the program worked.
Ask students questions like:
• What was difficult about this challenge?
• How is this loop different than a while loop?
• When might you want to use an infinite loop?
(Small Groups, 15 minutes)
Challenge students to add a force sensor to the Coach model.
Ask students to add a force sensor to their model by plugging the sensor into port D. Students do not need to attach the force sensor to the model.
With the sensor attached, ask students to update their programs to include using a sensor in the loop. The loop should continue to always be true, but also include using a force sensor.
from spike import Motor, ForceSensor from spike.control import wait_for_seconds # initialize motors and sensor. Set variable names motor_b=Motor('B') motor_f=Motor('F') force_sensor=ForceSensor('D') # run motors when count less than 5 While True: force_sensor.wait_until_pressed() motor_f.run_for_seconds(0.5, 40) wait_for_seconds(1) motor_f.run_for_seconds(0.5, -40)
Allow students time to test and modify their program. Remind students to watch the console for error messages.
Allows students to share their programs. Discuss the different approaches as a group.
(Group Exercise, 10 minutes)
Discuss the program with students.
Ask students questions like:
• What turns a while loop into an infinite loop?
• What is the most important thing to remember when creating an infinite loop?
• How can sensors be included in using loops?
Have students answer the following in their journals:
• What did you learn today about using infinite 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 student to rate themselves on a scale of 1-3, on their materials (parts) management today.
• Program infinite loops.
• Create a model that includes a force sensor that will provide a condition for the robot to move.
• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Student journals
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.