Minds On

Coding a robot

A robot named Recycle-Bot came with a manual explaining that it can do the following actions:

Movement Symbol
forward Fwd
right quarter turn RT
left quarter turn LT

Learner A wrote code for Recycle-Bot.

Learner B revised the code.

Check out the original code (Learner A) and the revised code (Learner B).

Learner A - Original code Learner B - Revised code

Fwd 2
RT
Fwd 4
RT
Fwd 2
RT
Fwd 4
RT


Repeat 2 [Fwd 2, RT, Fwd 4, RT]

Two columns of code. The first column is entitled ‘Learner A – Original Code’. The code is as follows: ‘Fwd 2, RT, Fwd 4, RT, Fwd 2, RT, Fwd 4, RT’. The second column is entitled ‘Learner B – Revised Code’. The code is as follows: ‘Repeat 2 (Fwd 2, RT, Fwd 4, RT)’.

Pause and Reflect

Pause and reflect

After exploring the original code and the revised code, answer the following questions.

  • What are the similarities between the original and revised code?
  • What are the differences?
  • What might be a reason for Learner B to revise the code?
  • Read the code and make a prediction about the expected outcome.
  • Is the outcome the same for both Learner A and Learner B?

Record your thoughts in a method of your choice. Be sure to explain your thinking.

Press ‘Answer’ to compare your answers!

What are the similarities between the original and revised code?

The similarities between the original code and the revised code are that both have Fwd 2 and RT.

What are the differences?

The differences are that the original code is eight lines long while the revised code is one line of code. Also, the revised code has a Repeat function.

What might be a reason for Learner B to revise the code?

A reason Learner B might have revised their code is to take fewer steps. Their code doesn’t list the events individually, but grouped them.

Read the code and make a prediction about the expected outcome.

I predict that Recycle-Bot will draw a rectangle. It moves forward then turns right four times. This makes me think that it is going to end up back in the same spot it started. It doesn’t always move forward the same amount though. This makes me think it will draw a shorter line, then a longer one. I know a rectangle has four sides, and that opposite sides are equal. It could have two shorter sides and two longer sides.

Is the outcome the same for both Learner A and Learner B?

Since the numbers that Recycle-Bot moves forward are the same and the number of RT are the same, I do think the outcome will be the same for both learners.

Action

Repeat command

A robot picks up litter. A line is drawn behind it to show where it has been.

Recycle-Bot can do the following actions:

Movement Symbol
forward Fwd
right quarter turn RT
left quarter turn LT

Recycle-Bot travels around its community and picks up litter. Recycle-Bot also has a pen attached to it to record where it has been. As Recycle-Bot moves, its pen draws a path, showing its movement.

Starting position

Recycle-Bot starts on the upper left square of the community grid.

A robot is on the top left square of a seven by seven community grid.

Recycle-Bot’s starting position

The following line of code is entered into Recycle-Bot.

Fwd 2, LT, Fwd 4, LT

When events are in sequence, they happen one after another. The code for Recycle-Bot is sequential.

Recycle-Bot follows the line of code and draws a shape on the grid. Recycle-Bot ends up in the following position:

A line shows the robot has moved two squares down and four squares to the right from the upper left of the seven-by-seven community grid.

What instructions would be repeated if Recycle-Bot were to continue and draw two more sides to make a rectangle?

Use the following grid map to move Recycle-Bot and create a line of code that will complete the rectangle. Record what your line of code is in a method of your choice!

What code will make a rectangle?

What line of code would move the robot and compete a rectangle shape?

Select the correct answer, then press ‘Check Answer’ to see how you did.

Using a repeat in code

Instead of writing out the instructions twice, you can use a type of code instruction known as a repeat.

Press ‘Repeat’ to learn more about repeats and why we use them in coding.

A repeat is an instruction to do the same instructions more than once. By including a repeat, you can write the same instructions using fewer lines of code.

Recycle-Bot is placed back onto the same starting position as before.

The following line of code is entered into Recycle-Bot:

Repeat 2 [Fwd 2, LT, Fwd 4, LT]

The robot is put back to the starting position.

The command Repeat 2 caused the commands [Fwd 2, LT, Fwd 4, LT] to be repeated twice.

Your turn

Try to recreate the following image using code. Use repeats whenever possible.

Record your thoughts digitally, orally or in print.

A robot on a seven by seven grid. A line shows the movement of the robot. The robot started at the upper left corner. The robot moved down four squares. The robot then moved right four squares. The robot moved up four squares. The robot moved right two squares. The robot moved down two squares. The robot moved left two squares. The robot’s final position is three down and five right, using the upper left square as the starting position.

What is a possible code to move the robot along the line to the position you explored in the previous image? Press ‘Answer’ to check out a possible solution.

Repeat 3 [Fwd 4, LT], Repeat 2 [RT], Repeat 3 [Fwd 2, RT]

Pause and Reflect

Pause and reflect

How did your code compare to the possible solution?

Record your thoughts using a method of your choice.

Innovation in Canada

This learning activity features emerging technologies, STEM contributions, and Canadian innovations that are making a difference.

Making connections

Plastic litter is polluting the rivers, lakes, and oceans, harming humans, wildlife, and their habitats. The Canadian government has a plan to achieve zero plastic waste by 2030, which includes a ban of single-use plastics.

Did You Know?

Did you know?

Every year, Canadians throw away three million tonnes of plastic waste, the equivalent of throwing away about 570 full garbage bags every minute. Only 9 percent of that waste is recycled, which means the vast majority of plastic waste ends up in landfills and about 29,000 tonnes finds its way into our natural environment annually.

The Innovations Solutions Canada program supports small businesses to develop sustainable solutions that will reduce plastic waste, create jobs, and protect the environment.

Imagine Recycle-Bot from the previous activities was programmed to pick up litter, and that the grid was a community area that was full of litter. As Recycle-Bot travels through the community, it picks up litter.

Let’s explore the following image of a path created by Recycle-Bot.

Consider which code was used to create the following image. Justify your selection. Record your response orally, digitally or in print.

A robot picks up litter around a community and creates a path by moving down to the fifth row, moving right and up to the fourth row, moving up and right to the first row, and finally moving right and down, ending in the eighth row.

Consolidation

Writing code with repeats

The community supervisor has written code for Recycle-Bot to collect each piece of litter.

The goal is to have Recycle-Bot reach each piece of litter while moving the shortest distance. Explore the following image of the path Recycle-Bot followed while picking up litter.

A robot picks up litter around a community and creates a path by moving down to the third row, moving down and right to the fourth row, moving right and down to the fifth row, moving down and right, ending in the seventh row.

Student Success

Think!

The following code has been created for Recycle-Bot. Alter the code so that it includes at least one repeat command. Record your thoughts digitally, orally or in print. Share your ideas with a partner, if possible.

Fwd 2

LT

Fwd 1

RT

Fwd 2

LT

Fwd 1

RT

Fwd 2

LT

Fwd 5

Reflection

As you read through these descriptions, which sentence best describes how you are feeling about your understanding of this learning activity? Press the button that is beside this sentence.

I feel…

Now, record your ideas using a voice recorder, speech-to-text, or writing tool.