Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Robots can seem smart because they react to what they sense around them. A simple way robots make choices is with if then logic. This means the robot checks a condition, such as whether an obstacle is close, and then follows an instruction.

This idea is important because it helps robots move safely, avoid crashes, and repeat tasks on their own.

A wheeled robot with an ultrasonic distance sensor can measure how far away a wall or block is. The robot might follow the rule, if the obstacle is closer than 20 cm, then stop and turn. If the path is clear, then the robot drives forward.

This decision repeats in a loop, so the robot keeps reading the sensor, checking the condition, and choosing the next action.

Understanding If Then Logic in Robots

Inside a program, a condition becomes one of two values, true or false. The robot does not understand a wall in the human sense. It receives a number from a sensor and compares that number with a chosen limit.

That limit is called a threshold. A good threshold depends on the robot's speed, turning space, sensor accuracy, and the surface around it. A fast robot needs to react earlier than a slow one.

A robot carrying a heavy object may need even more room before turning. Choosing a threshold is therefore an engineering decision, not a random number.

Sensor readings are not always perfect. An ultrasonic sensor can give changing values when sound reflects from soft fabric, angled surfaces, or narrow objects. A light sensor can change because of shadows or room lighting.

If one reading sits near the threshold, the robot may rapidly switch between actions. This can make it shake, stop repeatedly, or turn back and forth. Programmers reduce this problem by checking several readings before deciding.

They may use an average reading. They can use two limits instead of one.

For example, the robot can begin avoiding an obstacle at one distance and return to forward motion only when the obstacle is clearly farther away. This creates a calmer response.

The order of instructions matters. A robot usually needs time to complete an action such as stopping, reversing, or turning. If the program reads the sensor again immediately after starting a turn, it may change its mind before the turn finishes.

Many robot programs include a short wait or use a state variable. A state records what the robot is currently doing, such as moving forward, backing up, turning left, or waiting. The program can then apply different rules in each state.

This method is useful for line-following robots, automatic doors, warehouse carts, and traffic light systems. Larger robot programs use many conditions arranged in a clear order so that safety actions take priority over ordinary movement.

When building or debugging a robot, test one part at a time. First display or record the raw sensor values. This shows whether the sensor is connected correctly and whether its readings make sense.

Next test the decision using a slow motor speed. Watch which branch of the program runs when an object moves closer or farther away. A flowchart helps because each decision path should lead to a clear next step.

Pay attention to boundary cases, including a reading exactly at the chosen limit, a missing sensor reading, or an obstacle appearing while the robot turns. Real environments are messy. Careful tests make simple conditional logic reliable enough for a robot to act safely.

Key Facts

  • If then logic follows the pattern: if condition is true, then do an action.
  • A robot sensor collects information, such as distance, light, sound, or touch.
  • Example rule: if distance < 20 cm, then stop and turn.
  • Example rule: if distance >= 20 cm, then drive forward.
  • A flowchart diamond shows a decision, such as obstacle detected?
  • A loop lets a robot repeat steps: read sensor, decide, act, then read sensor again.

Vocabulary

If then logic
A decision rule where a robot checks a condition and performs an action when the condition is true.
Condition
A statement that can be true or false, such as distance is less than 20 cm.
Sensor
A device that detects information from the environment and sends it to the robot controller.
Ultrasonic sensor
A sensor that uses sound waves to measure the distance to nearby objects.
Loop
A set of instructions that repeats until the program tells it to stop.

Common Mistakes to Avoid

  • Forgetting to read the sensor again, which is wrong because the robot would keep using old information instead of reacting to new obstacles.
  • Using the wrong comparison sign, which is wrong because distance < 20 cm means close, while distance > 20 cm means farther away.
  • Putting the action inside the decision diamond, which is wrong because the diamond should contain the condition and the action should go in the next step.
  • Leaving out the no path in a flowchart, which is wrong because a robot needs instructions for both true and false results.

Practice Questions

  1. 1 A robot is programmed with the rule if distance < 15 cm, then stop. The sensor reads 12 cm. What action should the robot take?
  2. 2 A robot drives forward if distance >= 25 cm and turns if distance < 25 cm. For sensor readings of 40 cm, 25 cm, 18 cm, and 7 cm, list the robot action for each reading.
  3. 3 A robot gets stuck turning in place even when no obstacle is nearby. Explain one possible if then logic mistake in its program and how to fix it.