A mode variable remembers which job the robot is doing; buttons change it, and forever does that job.
A robot lawn mower has modes: mow, go home, pause. You press one button and it keeps doing that job until you choose another.
So far each program did one job. Real robots switch between jobs. The trick is a variable called mode that remembers which job is chosen. It holds a word, such as "follow", and mode = "follow" (the compare block in Logic, with a text in it) asks whether it is that word. Text must match exactly, so "Follow" would not match. The buttons only change the mode; forever looks at the mode every time round and does that job.
followLine is lesson 5's line follower, now in a function so forever stays easy to read.
Make your prediction first. Help opens once you've locked it in.

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.A, function () {
mode = "go"
})
input.onButtonPressed(Button.B, function () {
mode = "follow"
})
input.onButtonPressed(Button.AB, function () {
mode = "stop"
})
let mode = ""
robot.elecfreaksCuteBot.start()
mode = "stop"
basic.forever(function () {
if (mode == "go") {
robot.motorTank(50, 50)
} else if (mode == "follow") {
followLine()
} else {
robot.motorStop()
}
})
Drag the robot onto the line. Press B (follow), A (go straight) and A+B (stop) on the simulator's micro:bit, in any order.

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.A, function () {
mode = "go"
})
input.onButtonPressed(Button.B, function () {
mode = "follow"
})
input.onButtonPressed(Button.AB, function () {
mode = "stop"
})
let mode = ""
robot.elecfreaksCuteBot.start()
mode = "stop"
basic.forever(function () {
if (mode == "go") {
robot.motorTank(50, 50)
} else if (mode == "follow") {
followLine()
} else {
robot.motorStop()
}
})

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.A, function () {
mode = "go"
})
input.onButtonPressed(Button.B, function () {
mode = "follow"
})
input.onButtonPressed(Button.AB, function () {
mode = "stop"
})
let mode = ""
robot.elecfreaksCuteBot.start()
mode = "stop"
basic.forever(function () {
if (mode == "go") {
robot.motorTank(50, 50)
} else if (mode == "follow") {
followLine()
} else {
robot.motorStop()
}
})
Think about what the button did, and what forever does next.
Ruby's program:

input.onButtonPressed(Button.A, function () {
mode = "go"
})
let mode = ""
robot.elecfreaksCuteBot.start()
basic.forever(function () {
mode = "stop"
if (mode == "go") {
robot.motorTank(50, 50)
} else {
robot.motorStop()
}
})
A person needs to know which job the robot is doing. Show a different picture on the micro:bit for each mode, when its button is pressed.
In each button event, under set mode, add a picture.
show arrow north for go, show icon diamond for follow, show icon square for stop.
Add a fourth job: explore, chosen by touching the logo. In explore mode the robot drives and turns away from boxes, like lesson 4.
on logo pressed is in Input. Inside it: set mode to explore.
In forever, click + on the if to add an else if: mode = explore. Inside it, put lesson 4's turn-away if … else.
Show a different picture for each mode when its button is pressed.
This ticks by itself when your blocks do what is asked.
Add an explore mode, chosen with the logo, that turns away from boxes.
This ticks by itself when your blocks do what is asked.
Add a guard mode, like a security robot watching a doorway:
Drag a box towards the robot to test it.
on shake (Input) sets mode to guard. In forever, add an else if for guard.
Inside guard: robot motor stop, then an if: distance < 20 → red lights and a tone (robot play tone); else lights off (black).
A guard mode, chosen by shaking: the robot stays still, and flashes red and beeps when something comes close.
This ticks by itself when your blocks do what is asked.
The same program runs on a real robot. Things to notice:
Students use a mode variable to give one robot several jobs (go, follow, stop); buttons change the mode and forever does the job, then they add explore and guard modes. About 50 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
This is the state pattern (in the curriculum, a variable that keeps track of what the program is doing). A variable called mode holds a word: "go", "follow" or "stop". The buttons only change the mode; forever checks the mode every time round and does that job. So a button press has a lasting effect, even though the button event itself is over in an instant.
The mode values are words (strings) so programs read clearly: if mode = "follow". In blocks, compare words with the = block from Logic and the text block from Text. followLine is lesson 5's program moved into a function, which keeps forever short.
Ruby's robot is the reset-in-loop slip from micro:bit Foundations (Counting): a starting value set inside forever wipes every change. The logo (micro:bit V2 only) appears in the simulator; on a V1 micro:bit, use shake instead. MOVE Motor classes can use the same pattern with Kitronik blocks.
Buttons only change the mode; forever does the job. When a mode never runs, ask them to compare the word in the button with the word in forever.
Stretch: Make one button cycle through the modes: A sets mode to the next one in a list (go, follow, explore, stop). Or count how many times guard mode was triggered.
Support: Give the guard branch's else if with its question; students add the stop, the distance if and the lights.
Next unit: Talk. The buttons move to a second micro:bit, which becomes a remote control. Real-world links: lawn-mower and vacuum modes, a lift's modes (going up, going down, doors open), traffic lights.