a1tD0000007eJM2IAM

Course: Introduction to Programming with NAO
3. Loops and Infinite Loops

  • 6-9 grade
  • Beginner

Lesson Description:

Lesson Description:
This lesson introduces the concept of loops. Students will add a repetitive part in their story using loops. 

Objectives

  1. Convert a repetitive series of actions into a single loop
  2. Make NAO repeat the same action over and over again
  3. Use the Counter box control and its main components (initial value, incremental value, final value) to make the robot count

image description

Lesson Modules


Teaching Tips:

You can download the code for this lesson here

This lesson covers the concept of loops. The students will create a program that makes the robot repeats something at least twice. 

For the observe module, run the program which makes the NAO say " Hello" 5 times. 2 times with a series of boxes and 3 times using a loop.

We want to spark the discussion about repetition and the ways to have a program do the same things multiple times without having to have to do it all these times. 

The CLASS VIEW shows one way to program the robot to say the same thing multiple times. 

If they created a story in previous classes you can open their files and take up from where they left off. 

Advantages of loops :

Simplicity and ease when writing a program.

Reduces the need for duplicate code which makes the code more readable.

How would you make Nao say thank you in choregraphe? Answer step-by-step.

How many times did the robot say the same thing?
  • 1
  • 2
  • 5
  • 6



Teaching Tips:

The goal of this module is to get the students familiarized with the counter box in Choregraphe. The counter box is the most basic block to create a program that includes loop and counts. 

Students are invited to open their Choregraphe and learn hands-on how the counter box work. In the next module, they will use the box to create a program that makes the robot repeats something multiple times with loops. 

A detail explanation of how the counter box work is described here and can be shown to the students with CLASS VIEW. We recommend to let the students explore by themselves before showing them the steps and the detailed explanation on CLASS VIEW.

---------------------------------------------------------------------------------------------------------------------------

All the answers below are valid only in case of the loop counter running from lower to a higher number.
Only when Initial value is lower than the Final value.
Loop counter running backward from higher number to a lower number is not discussed here.

If the Initial value is set to 0, and Step value is set to 1, the loop will run (Final value + 1) cycles.

The loop will run 4 cycles with the following setup:
Initial value=0, Step value=1, Final value =3.
Here are the values of the current counter with each looping cycle.
Total 4 times:
0, 1, 2, 3


If the Initial value is set to 1, and Step value is set to 1, the loop will run (Final value) cycles.

The loop will run 3 cycles with the following setup:
Initial value=1, Step value=1, Final value =3.
Here are the values of the current counter with each looping cycle.
Total 3 times:
1, 2, 3


Each loop cycle the current counter will be the sum of previous counter value plus Step value.
If the counter current value is higher than the Final value, it will signal the output port and move to the next connected box wihtout executing the loop one more cycle. 

The loop will run 3 cycles with the following setup:
Initial value=0, Step value=2, Final value =5.
Here are the values of the current counter with each looping cycle.
Total 3 times:
0, 2, 4


A case where the loop is being started again after it has finished all its cycles is called Infinite Loop.
The loop will be executed endless times.

An infinite loop can be also created when the termination condition is absent.
This situation can be created simply by connecting two boxes to each other.
When function A calls function B that calls function A.

Connecting an Animated Say box to itself will cause an infinite loop.
Function A calls itself within its execution.


Having Step value set to 0 will cause the loop to run endlessly because the Final value will never be reached.


Another example of a terminating condition that can never be met is when the Step value is negative while the Initial value is lower than the Final value
Every loop cycle the current counter value is getting lower than the previous cycle so it will never reach the Final value.


Another way to cause the loop to start over infinitely is to signal its input port from Inside the Loop box.

Resetting the loop is used when there is a need to start over in the middle of another loop execution.

Open Choregraphe 

How to Loop

In Choregraphe, the Counter box can control the number of times a set of boxes will run.
It can be found under the Flow Control folder.

The wrench icon on the bottom corner of the Counter block opens its settings.
The decisions on how to count, when to start, when to stop, how many per counts, are respectively called: Initial value, final value and step value.

Drag and drop the counter box in your workspace and explore the box by running the program. 

Modify the parameters ( initial value, step value, final value) to understand how they work.

With initial value of 1 and step of 1 what is the final value to count to 5?
  • 4
  • 6
  • 5

With initial value of 0 and step of 1 what is the final value to count to 3?
  • 4
  • 3
  • 2



Teaching Tips:

This module will have the students use the counter box and a say box or animated say or say text to make the robot say the same thing in loops. They have two challenges :

  1. Make the robot say the same thing multiple times. 
  2. Make the robot count the number of times you go through a loop.  


In the CLASS VIEW module, the steps described will answer task number 2. Which is to make the robot counts the number of times it goes through a loop.

With Choregraphe create a program that : 

  1. Make the robot say the same thing multiple times. (If you have a program from previous lessons you can use it and add the loop part to it).
  2. Make the robot count the number of times you go through a loop.  



Teaching Tips:



AFTER CLASS

Store Student Files
Put the student files on a thumb drive or store them in some way so that the students can continue working on their project in the next lesson. 

Pack Up Hardware
Pack the robots and computers according to the packing instructions.

Question 1: Why are loops useful in programming?

Question 2: In which scenario would it be useful to use a loop?
  • I want NAO to count 1-10
  • I want NAO to repeat a series of movements to make a dance
  • I want NAO to check for a face to detect once per minute
  • All of the above