Programming Robots: Sensors to Actions

If sensor says X, then do Y

1

The Sense-Think-Act Loop

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.

while(true) { sensor = read(); decision = think(sensor); motor(decision); }
This loop runs hundreds of times per second. The robot never stops sensing and responding. Even "doing nothing" is an active decision.
2

Writing Rules

Robot behavior comes from stacking simple if-then rules. Toggle each rule to see how the robot's behavior changes. More rules = smarter behavior.

3

Sensor Types

Different sensors give different information. Click each sensor to see what data it provides to the robot brain.

Distance (Ultrasonic)

Sends a sound pulse, measures echo time. Returns distance in cm. Used for wall avoidance. Range: 2-400cm. Blind spot: very close objects.

Light (Photoresistor)

Measures brightness from 0 (dark) to 1023 (bright). Used for line following (dark line on light surface) and light-seeking behavior.

Touch (Bumper)

Returns 0 or 1: pressed or not. The simplest sensor. Used as a backup when distance sensors miss an obstacle. Very reliable.

Compass (Magnetometer)

Returns heading in degrees (0-360). Used for navigation: "face north then drive forward." Enables the robot to maintain direction.

Fun Fact

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.

Robot Programmer!

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.

0
Rules Created
0
Time Exploring

Sense-Think-Act

Robots repeat one loop: read sensors (sense), run logic (think), move motors (act). Hundreds of times per second.

If-Then Rules

The simplest robot brain: "if distance < 20cm, turn right." Stack enough if-then rules and you get surprisingly smart behavior.

State Machines

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.

Sensor Fusion

Real robots combine multiple sensors: distance + light + touch + compass. Combining data gives a fuller picture than any single sensor.

Ready to Create?

Put your new knowledge into practice!

More Discoveries

Suggest a Correction