The most important ideas are sequence, variables, loops, conditionals, sensors, actuators, and debugging. A robot usually follows the pattern read sensors, process information, choose an action, then control motors or other outputs. Programs often use formulas such as distance = speed x time and error = target value - measured value to control movement.
Good robot code is organized, tested in small steps, and designed to handle unexpected sensor readings.
Key Facts
- A sequence runs commands in order, so command 1 finishes before command 2 begins.
- A variable stores a value that can change, such as speed = 50 or targetDistance = 100 cm.
- A loop repeats commands, such as repeat 4 times: move forward, then turn right.
- A conditional uses logic to choose an action, such as if distance < 20 cm then stop.
- A basic robot control cycle is read sensors, make a decision, run actuators, then repeat.
- The distance formula for constant speed is distance = speed x time.
- A simple correction formula is error = target value - measured value.
- Debugging means finding and fixing problems by testing one part of the robot program at a time.
Vocabulary
- Pseudocode
- Pseudocode is a plain-language plan for a program that describes the steps before writing real code.
- Sensor
- A sensor is an input device that measures something in the environment, such as distance, light, touch, color, or rotation.
- Actuator
- An actuator is an output device that makes the robot do something physical, such as a motor, servo, wheel, or gripper.
- Loop
- A loop is a programming structure that repeats a set of commands while a condition is true or for a set number of times.
- Conditional
- A conditional is a programming structure that runs different commands depending on whether a statement is true or false.
- Debugging
- Debugging is the process of testing a program, finding errors, and changing the code so the robot behaves correctly.
Common Mistakes to Avoid
- Forgetting to update sensor values inside a loop is wrong because the robot keeps using old information instead of reacting to the current environment.
- Using if when a repeated check needs while is wrong because the robot checks the condition only once instead of continuing to respond.
- Setting motor speeds with opposite signs by accident is wrong because the robot may spin instead of driving straight.
- Ignoring units is wrong because mixing seconds, milliseconds, centimeters, or motor rotations can make movements too short, too long, or unsafe.
- Testing the whole program at once is wrong because it makes errors harder to find than testing one sensor, motor, or behavior at a time.
Practice Questions
- 1 A robot drives at 25 cm/s for 4 seconds. Using distance = speed x time, how far does it travel?
- 2 A distance sensor reads 12 cm. Write the action for this rule: if distance < 20 cm then stop, else move forward.
- 3 A robot needs to make a square path. Write pseudocode using a loop that moves forward and turns right 4 times.
- 4 Why should a robot program read sensors repeatedly instead of reading them only once at the start?