Programming Robots: Sensors to Actions
If sensor says X, then do Y
If sensor says X, then do Y
Watch a robot navigate a room using simple rules. It senses walls with a distance sensor, thinks about what to do, and acts by turning or moving forward. The colored arc shows what the sensor detects.
Robot behavior comes from stacking simple if-then rules. Toggle each rule to see how the robot's behavior changes. More rules = smarter behavior.
Different sensors give different information. Click each sensor to see what data it provides to the robot brain.
Sends a sound pulse, measures echo time. Returns distance in cm. Used for wall avoidance. Range: 2-400cm. Blind spot: very close objects.
Measures brightness from 0 (dark) to 1023 (bright). Used for line following (dark line on light surface) and light-seeking behavior.
Returns 0 or 1: pressed or not. The simplest sensor. Used as a backup when distance sensors miss an obstacle. Very reliable.
Returns heading in degrees (0-360). Used for navigation: "face north then drive forward." Enables the robot to maintain direction.
The Mars rovers Curiosity and Perseverance use a sense-think-act loop with a 20-minute communication delay to Earth. They must make navigation decisions autonomously because waiting for human commands would take 40 minutes round trip.
You've written robot logic: read sensors, make decisions, take actions. Every autonomous machine from a Roomba to a Mars rover runs the same sense-think-act loop you just built.
Robots repeat one loop: read sensors (sense), run logic (think), move motors (act). Hundreds of times per second.
The simplest robot brain: "if distance < 20cm, turn right." Stack enough if-then rules and you get surprisingly smart behavior.
A robot can be in states like "exploring", "avoiding", "returning home." Each state has its own behavior rules and conditions to switch to another state.
Real robots combine multiple sensors: distance + light + touch + compass. Combining data gives a fuller picture than any single sensor.
Put your new knowledge into practice!