Computer Science
Grade 7
20 min
Lesson 7: Programming with Sensors: Responding to Environmental Changes
Learn how to program robots to respond to changes in their environment based on sensor input.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Identify different types of robot sensors and the data they collect.
Explain how a sensor provides input to a program.
Write a program that uses an 'If-Then-Else' conditional statement to make a decision based on sensor data.
Define and use a 'threshold' to trigger a robot's action.
Create a program that uses a loop to continuously check a sensor's value.
Design and code a simple autonomous behavior, such as avoiding an obstacle or following a line.
How does an automatic door know when to open, or a self-driving car know when to stop? 🤖 It's all about sensors!
In this lesson, we'll learn how to give our robots 'senses' using sensors. We will write code that reads information from the robot's environ...
2
Key Concepts & Vocabulary
TermDefinitionExample
SensorA device that detects or measures a physical property (like light, distance, or sound) and sends that information to a computer or microprocessor.An ultrasonic sensor measures distance by sending out a sound wave and timing how long it takes to bounce back.
InputData that a computer program receives from an external source, like a sensor.The number '15' (representing 15 centimeters) read from a distance sensor is an input to the robot's program.
OutputAn action or signal that a computer program sends out, often to control a device like a motor or a light.Telling the robot's wheels to turn is an output.
Conditional Statement (If-Then-Else)A block of code that performs different actions depending on whether a condition is true or false.IF the...
3
Core Syntax & Patterns
The If-Then-Else Pattern
if (sensor_value < threshold) {
// Action A
} else {
// Action B
}
Use this to make a choice. The program checks if a condition is true. If it is, it runs Action A. If it's not, it runs Action B. This is the core of making a robot 'decide' what to do.
The Continuous Sensing Loop
forever {
// Read sensor value
// Use If-Then-Else to decide on an action
}
A robot needs to constantly check its surroundings. Placing your sensor-reading and conditional logic inside a 'forever' loop ensures the robot is always aware and ready to react to changes.
Comparison Operators
< (less than), > (greater than), == (equal to)
These are the symbols you use inside your 'if' statement to compare the sensor's...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You want to program a robot to stay inside a large black circle on a white floor. The robot has one light sensor on its front. What logic should the robot follow?
A.If it sees white, move forward. If it sees black, stop.
B.If it sees black, move forward. If it sees white, back up and turn around.
C.If it sees black, turn in a random direction. If it sees white, move forward.
D.If it sees white, move forward. If it sees black, also move forward.
Challenging
A robot has a forward distance sensor and a downward light sensor. How could you program it to drive across a room until it finds a black mat, and then stop on the mat?
A.Use an 'If-Then-Else' to check the distance sensor, and if it's clear, check the light sensor.
B.In a 'forever' loop, check the light sensor. If it sees black, stop. Otherwise, move forward.
C.In a 'forever' loop, move forward. Then, have a separate 'forever' loop to check the light sensor.
D.Check the light sensor once. If it's not black, drive forward for 10 seconds.
Challenging
A friend's code for an obstacle avoider is: `forever { if (distance < 20) { backup_for_1_sec() } else { turn_right() } }`. What is the major flaw in this robot's behavior?
A.The robot will get stuck in a loop of backing up and turning right, never moving forward.
B.The threshold of 20 is too large and the robot will be too timid.
C.The robot will not detect obstacles correctly because it is always turning.
D.Backing up for 1 second is too long and will drain the battery.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing FreeMore from Chapter 5: Robotics: Building and Programming Autonomous Machines
Lesson 1: What is a Robot? Defining Autonomous Machines
Lesson 2: Robot Components: Sensors, Actuators, and Controllers
Lesson 3: Types of Robots: Exploring Different Designs and Applications
Lesson 4: Introduction to Robot Programming: Giving Robots Instructions
Lesson 5: Basic Robot Movements: Controlling Motors and Actuators