Home · Academy · Robotics & Coding · Programming with Scratch · Project: Robot Mission Simulation

Project: Robot Mission Simulation

Apply the sense–decide–move loop in a Scratch robot mission simulation.

PROJECT COMPASS

What will you use this page for?

Core idea

In this project I build a robot sprite on screen; it senses colour and touch, makes a decision, avoids obstacles, and reaches a target. This brings the "sense–decide–move" loop from the algorithm module to life in Scratch.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Putting the "if" block outside the forever loop The robot needs to keep watching its surroundings. If you put the "if" block outside forever , the program checks once and forgets. For the check to repeat on every frame, the "if" must sit inside the loop. Choosing the wrong colour In the touching color ...? block,…

Next connection

Python Basics module: We leave the blocks behind and meet written code, starting to build the same algorithmic thinking in a text-based language.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration45–75 min
PrerequisiteProject: Basketball Shooting Game
ContentProject guide · 1,691 words
Last updated

One-sentence summary

In this project I build a robot sprite on screen; it senses colour and touch, makes a decision, avoids obstacles, and reaches a target. This brings the "sense–decide–move" loop from the algorithm module to life in Scratch.

Why it matters

A robot vacuum backs away when it bumps a wall, and a line-following robot corrects its direction when it sees the black stripe. They all work on the same simple idea: sense, decide, move. A sensor measures something, the program decides with a condition, and a motor moves accordingly. Then the loop starts over.

We do not have a real robot yet, but Scratch can imitate this loop on the stage. The sprite's Sensing blocks stand in for the sensor, the Control blocks make the decision, and the Motion blocks move the sprite like a motor. This project is a teaching simulation, not a real device. The goal is to make the logic inside a robot's "head" visible, and the same thinking will help us later when we program a real robot.

Define the mission: the sense–decide–move loop

Before writing any code, let us describe the robot's mission in clear sentences. A vague mission produces a vague program.

Mission description

The robot starts from the bottom-left of the stage. Along the way there are black walls. If the robot touches a wall, it backs up and changes direction; if not, it moves forward. Its goal is the green target at the top right. When it touches the target, it says "Made it!" and stops.

Map the loop to categories

Map the loop to categories table
StepWhat it doesScratch category
SenseAm I touching a wall/target?Sensing
DecideIf I am touching, what should I do?Control ("if ... then")
MoveGo forward or turnMotion
RepeatKeep the loop goingControl ("forever")

Notice that this table is exactly the three building blocks from the algorithm module: sequence (the order of steps), condition (if it is touching), and repetition (forever) here bring a robot to life.

Prepare the stage and the robot

Draw the course

Draw a new backdrop. With the brush tool, draw a few walls out of black lines, leaving a path through. Put a filled green square (the target) in the top-right corner. The colours matter, because the robot will "see" them.

Add the robot sprite

Pick a small sprite from the library (for example a ball, or draw a robot). Shrink its size so it can fit through narrow paths.

Create variables

From the Variables category, make two variables:

Create variables table
VariableWhat it holds
step_countHow many times the robot moved forward
arrivedDid it reach the target? (0 or 1)

Program the robot: the main block sequence

Now let us build the loop. The sequence below is the robot's brain.

Example 1: Basic sense–decide–move

when green flag clicked
go to x: -200 y: -150
point in direction 90
set step_count to 0
set arrived to 0
forever
  if <touching color [black]?> then
    move -15 steps
    turn right 30 degrees
  else
    move 5 steps
    change step_count by 1

On every frame this loop asks one question: "Am I touching black?" If so, the robot backs up 15 steps and turns 30 degrees (decide + move). If not, it moves 5 steps forward and adds one to the counter. This is exactly the sense–decide–move loop.

The touching color [black]? block is in the Sensing category and is hexagon-shaped; its answer is "true" or "false." Click the square on the block and sample the wall colour on the stage with the eyedropper.

Example 2: Sense the target and stop

We do not want the robot to wander forever. We add a second "if" inside the main loop so it stops at the goal:

when green flag clicked
go to x: -200 y: -150
point in direction 90
set step_count to 0
set arrived to 0
forever
  if <touching color [black]?> then
    move -15 steps
    turn right 30 degrees
  else
    move 5 steps
    change step_count by 1
  if <touching color [green]?> then
    say "Made it!"
    set arrived to 1
    stop [this sprite]

Now the robot has two senses: it flees when it sees black and stops when it sees green. The stop [this sprite] block (Control) ends the forever loop. In the end step_count shows how many steps the robot took, a nice measure for comparing different paths.

Test course and debugging

Writing a program is half the work; testing is the other half. Try the robot in different situations:

Test course and debugging table
#TestExpected result
1Start on an open pathThe robot moves straight
2Put a wall in frontIt backs up and turns on touch
3Drive onto the green targetIt says "Made it!" and stops

One bug and its fix

On my first try the robot passed straight through the walls and never turned back. Expected: back up when it touches black. What happened: it sensed nothing.

Goal: The robot backs up when it touches a black wall
Problem: The robot goes through the walls
Check: The colour in "touching color?" is slightly off from the real black
Fix: Click the colour square and sample the exact wall colour with the eyedropper

When a colour is set by hand, it may not match the real colour on screen, so the block is never "true." Sampling with the eyedropper fixed it.

A second common bug: the robot shakes and gets stuck against a wall. This happens when the back-up distance or turn angle is too small. Increasing move -15 steps and turn 30 degrees a little frees the robot from the corner.

Next step: moving to a real robot

The logic in this simulation is the same in real robots. Later, the touching color? block is replaced by a line sensor or a distance sensor, and the move steps block by a motor command. The deciding "if ... then" part barely changes. So the brain you build on screen today looks a lot like the brain of a real robot tomorrow.

Mini practice

Grow the project with your own idea. Try one of these:

  1. Add a second wall colour (for example red) and make the robot turn at a different angle when it touches red.
  2. Add a time variable and measure how many seconds the robot takes to reach the goal.
  3. Make the robot reach the target entirely on its own decisions; make the course harder and tune the turn angle.

Write your change in one sentence: what does the robot do now, and what does it do better?

Common mistakes

Putting the "if" block outside the forever loop

The robot needs to keep watching its surroundings. If you put the "if" block outside forever, the program checks once and forgets. For the check to repeat on every frame, the "if" must sit inside the loop.

Choosing the wrong colour

In the touching color ...? block, always sample the colour from the real wall with the eyedropper. A colour set by hand that only looks similar may never be "true," and the robot will not see the obstacles.

Not resetting the starting position

If you do not send the robot back to the start with a go to block when the green flag is clicked, it starts where the last run left off. Reset the position and variables at the start so every test is fair.

Forgetting the condition that stops the loop

If you only write "turn when touching a wall" and never check the target, the robot wanders forever. Remember to add the second condition that says stop [this sprite] when it touches the goal.

Safety note

This project runs entirely on screen, so there is no physical danger. Still, stand up every 20 minutes and rest your eyes. When you share your project on the Scratch website, do not type personal details like your full name, school, or address; anyone can see your shared projects. If you later move this simulation to a real robot, any step involving batteries, motors, sharp tools, or hot surfaces must be done with an adult; never work with mains electricity.

Lesson summary

Check questions

  1. What are the three steps in a robot's core working loop?
  2. In Scratch, which block category does the sensor's job?
  3. What is the correct way to choose the colour in the touching color [black]? block?
  4. Why do we put the "if" block inside the forever loop?
  5. If we move this simulation to a real robot, what replaces the touching color? block?

Answers

  1. Sense, decide, move. The robot measures its surroundings, decides with a condition, moves accordingly, and the loop repeats.
  2. The Sensing category. Hexagon blocks like touching color? are the program's eyes and ears.
  3. Click the colour square on the block and sample the exact wall colour with the eyedropper. A hand-set colour may not match the screen.
  4. On its own, "if" checks the condition once and forgets. Inside forever, the program rechecks every frame, so the robot senses continuously.
  5. A sensor (for example a line or distance sensor). Motors replace the motion blocks, while the deciding "if ... then" part stays almost the same.

Source and verification note

For “Project: Robot Mission Simulation”, verification focuses on whether the relationship between Define the mission: the sense–decide–move loop and Map the loop to categories remains consistent across examples. Block names are kept consistent with the current core Scratch categories. Project behaviour should be tested separately for start-up, normal play, errors and restarting.

Next lesson

Python Basics module: We leave the blocks behind and meet written code, starting to build the same algorithmic thinking in a text-based language.

Start QuizBack to Programming with Scratch
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.