← Robotics challenges

Tripwire

Two robots work together. One stands still as a tripwire and, when the other passes, cues it to do a trick.

Factories put sensors beside conveyor belts: when a box passes, the next machine is told to act. Shops use beams at the door to tell a speaker to chime. One device notices, another acts.

An optional challenge: two robots working as a team. One robot stands beside the track as a tripwire: its distance sensor watches the line. The other robot patrols. When the patrol robot passes in front of the tripwire, the tripwire sends a cue by radio, and the patrol robot does a trick.

Both robots run the same program. Press A+B on a micro:bit to make its robot the tripwire, then drag that robot beside the line, facing it. The simulator's distance sensor sees other robots as well as boxes.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
tripwire.ts
function followLine () {
    if (robot.detectLine(RobotLineDetector.Left) && robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 40)
    } else if (robot.detectLine(RobotLineDetector.Left)) {
        robot.motorTank(10, 40)
    } else if (robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 10)
    } else {
        robot.motorStop()
    }
}
input.onButtonPressed(Button.AB, function () {
    role = "tripwire"
    robot.motorStop()
    basic.showIcon(IconNames.Target)
})
let role = ""
robot.elecfreaksCuteBot.start()
radio.setGroup(1)
role = "robot"
basic.forever(function () {
    if (role == "tripwire") {
        if (robot.obstacleDistance() < 20) {
            basic.showIcon(IconNames.Surprised)
        } else {
            basic.showIcon(IconNames.Target)
        }
    } else {
        followLine()
    }
})
The second robot is the tripwire, beside the line and facing it. What does the tripwire's micro:bit show as the first robot passes?

RunRun it and compare the result with your prediction.

Run

Press A+B on the second micro:bit. Drag its robot next to the line, pointing at it, about a hand's width away in the simulator. Watch its micro:bit as the first robot goes past.

The program as blocks
Show as code
tripwire.ts
function followLine () {
    if (robot.detectLine(RobotLineDetector.Left) && robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 40)
    } else if (robot.detectLine(RobotLineDetector.Left)) {
        robot.motorTank(10, 40)
    } else if (robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 10)
    } else {
        robot.motorStop()
    }
}
input.onButtonPressed(Button.AB, function () {
    role = "tripwire"
    robot.motorStop()
    basic.showIcon(IconNames.Target)
})
let role = ""
robot.elecfreaksCuteBot.start()
radio.setGroup(1)
role = "robot"
basic.forever(function () {
    if (role == "tripwire") {
        if (robot.obstacleDistance() < 20) {
            basic.showIcon(IconNames.Surprised)
        } else {
            basic.showIcon(IconNames.Target)
        }
    } else {
        followLine()
    }
})
Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

The program as blocks
Show as code
tripwire.ts
function followLine () {
    if (robot.detectLine(RobotLineDetector.Left) && robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 40)
    } else if (robot.detectLine(RobotLineDetector.Left)) {
        robot.motorTank(10, 40)
    } else if (robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 10)
    } else {
        robot.motorStop()
    }
}
input.onButtonPressed(Button.AB, function () {
    role = "tripwire"
    robot.motorStop()
    basic.showIcon(IconNames.Target)
})
let role = ""
robot.elecfreaksCuteBot.start()
radio.setGroup(1)
role = "robot"
basic.forever(function () {
    if (role == "tripwire") {
        if (robot.obstacleDistance() < 20) {
            basic.showIcon(IconNames.Surprised)
        } else {
            basic.showIcon(IconNames.Target)
        }
    } else {
        followLine()
    }
})

Ivy's tripwire, in forever:

The program as blocks
Show as code
basic.forever(function () {
    if (role == "tripwire") {
        if (robot.obstacleDistance() < 20) {
            radio.sendString("trick")
        }
    }
})

Think about what forever is doing while the message arrives.

Ivy's tripwire sends 'trick' whenever something is closer than 20 cm. Her patrol robot starts its trick again and again as it passes. Why?

When the patrol robot hears 'trick', why should the handler only set mode to trick, instead of doing the moves itself?

ModifyChange the code on purpose and check the result.

Milestone 1: arm and fire

Turn the tripwire into a one-shot trigger:

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.
Need a hint?

Make a variable armed. On button A: if role = tripwire, set armed to true and show a tick.

Another hint

In the tripwire's part of forever: if armed and distance < 20, set armed to false, send the word trick, and show a cross.

A arms the tripwire; when the robot passes, it sends 'trick' once and disarms.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Milestone 2: the trick

Make the patrol robot do a trick when it is cued: lights on, a quick wiggle, lights off, then back to patrolling. Keep the trick short and finish facing the same way, so the robot can find the line again.

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.
Need a hint?

Make a variable mode, set to patrol in on start. on radio received: if role = robot and the word is trick, set mode to trick.

Another hint

In the robot's part of forever: if mode = trick, do the trick (lights and a wiggle), then set mode back to patrol; else follow the line.

When the patrol robot hears 'trick', it does a trick with its lights, then carries on patrolling.

This ticks by itself when your blocks do what is asked.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Milestone 3: a self-resetting tripwire

Make the team run by itself:

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.
Need a hint?

After the tripwire fires: count the pass (a variable passes, change by 1), show it, pause 5 seconds, then arm again.

Another hint

Add a sound to the trick with play tone … in background, so the robot keeps moving.

The tripwire counts the passes, shows the count and re-arms itself; the trick plays a sound.

This ticks by itself when your blocks do what is asked.

Info

On a real robot

With real robots, the tripwire can be a robot or just a micro:bit with an ultrasonic sensor. Place it a short distance from the track, facing across it. Real readings jump about, so you may need a range (for example between 5 and 20 cm) to ignore false readings near zero.

For teachers

An optional challenge for students who finish early. Two robots work as a team; one stands still as a tripwire and, armed with a true/false variable, cues the patrolling robot once per pass; the patrol robot does a trick through its mode, then carries on. About 50 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Optional: the high ceiling of the course, not needed for Challenge complete. It is based on a common sensor pattern: one device notices, another acts. The tripwire is a robot that has been switched (A+B) to watch instead of drive; its distance sensor sees the other robot (the simulator treats robots as obstacles). Students drag the tripwire robot beside the line, facing it.

Two ideas from earlier come back: fire once (armed, like alerted in lesson 9) and handlers set the mode, forever does the job (lessons 6 and 11). The trick is a wiggle so the robot finishes facing along the line and can find it again; a full spin may leave it off the line.

Real ultrasonic sensors give false readings near zero, so a real tripwire often checks a range (for example 5 to 20 cm).

Running the lesson

  1. Predict and Run: placing the tripwire is fiddly. Show it once on the projector.
  2. Investigate: Ivy's tripwire is lesson 9's repeat-alert slip; the second question is the mode rule from Patrol and alert.
  3. Milestone 1: arm with A, fire once, disarm.
  4. Milestone 2: the trick lives in forever, chosen by mode.
  5. Milestone 3: re-arming after a pause makes it work lap after lap.

The hard part (on purpose)

Firing once per pass with arm and disarm, and the trick in forever. Ask when the tripwire is armed and where the trick's moves are.

When students get stuck

Stretch and support

Stretch: Two tripwires at different places on the track, each sending a different word, and a different trick for each.

Support: Give Milestone 1 as a worked example to read; students build Milestones 2 and 3.

Where it leads

Real-world links: conveyor-belt sensors, shop door chimes, lap timers at races, automatic doors.

What each step is for

Now you can
You reached the endBack to Robotics challenges