Robotics
How to drive a robot route from a list of moves
To drive a robot along a route without hard-coding it, keep the route as data: an array of leg lengths in encoder ticks. Even legs drive straight; odd legs pivot. A leg counter says which entry the robot is on, the tick count is compared with that entry's length, and when it's reached the robot moves to the next leg and resets the count to zero. Change the list and the same code drives a new route.
The route is a list
int len[] = {4, 2, 4}; // straight 4, pivot 2, straight 4
const int LEGS = 3;
int leg = 0;
int ticks = 0;
Legs count from 0, so leg 0 is straight, leg 1 a pivot, leg 2 straight again. The program doesn't know the route; it reads it.
The leg machine
Each pass of loop(), three steps:
if (now == LOW && last == HIGH) ticks = ticks + 1; // 1. count a tick
last = now;
if (leg < LEGS && ticks >= len[leg]) { // 2. leg done?
leg = leg + 1;
ticks = 0; // start fresh
}
if (leg >= LEGS) { // 3. drive
analogWrite(9, 0); analogWrite(10, 0); // route over
} else if (leg % 2 == 1) {
analogWrite(9, 255); analogWrite(10, 0); // odd: pivot
} else {
analogWrite(9, 255); analogWrite(10, 255); // even: straight
}
leg % 2 is the remainder after dividing by two: 1 for odd legs. The leg < LEGS check comes first so the program never reads past the end of the array.
Live experiment
Edit the route
One program drives whatever list it's given: even legs straight, odd legs a pivot of 18° a tick, stop after the last. Start with 4, 2, 4, add legs, or try 8, 5, 8, 5, 8, 5, 8, 5, a square that closes within about 2 cm. Without the reset at each new leg, every leg after the first ends the moment it starts.
“A list of legs” is lesson 6 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 before it first.
A square from a list
Make the pivots 5 ticks, about a quarter turn each, and the straights 8:
int len[] = {8, 5, 8, 5, 8, 5, 8, 5};
const int LEGS = 8;
Four sides and four corners, and the robot ends within a couple of centimetres of where it started. Nothing in the code changed: only the list, and LEGS.
Code and data
Keeping what to do in data and how to do it in code is one of programming's best habits: the code is tested once, and the data can change every day. 3D printers and CNC machines follow long lists of moves called G-code, and warehouse robots get their routes as lists of waypoints. Neither has the route written into its program.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“A list of legs” is lesson 6 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Ends at once: the count is already past its length. After leg 0 the count is 4, which is already ≥ 2, so leg 1 ends on the same pass. Reset ticks to 0 at every leg change.
- Why not “Takes longer”? The count only grows, so a count already past the leg's length ends it at once, not later.
- Why not “Pivots for ever”? The count is already beyond the pivot's length, so the pivot ends at once rather than never.
- Why not “Is skipped”? The leg does begin, for one pass, but with the count already past its length it ends straight away.
Show the answer
Answer: A pivot of 2 ticks: odd legs pivot. Legs count from 0. Leg 3 is odd, so it pivots, for len[3] = 2 ticks. The code didn't change; only the route did.
- Why not “A stop”? The stop comes after the last leg, leg 4 in this five-leg list.
- Why not “Straight”? Even legs go straight. Leg 3 is odd, so it pivots.
- Why not “The end”? With five entries, legs 0 to 4, leg 3 is the second to last, not the end.
Show the answer
Answer: Legs 1 and 3: the pivots. % 2 is the remainder after dividing by two: 1 for odd numbers. Odd legs pivot.
- Why not “Every leg”? Only odd numbers leave a remainder of 1 when divided by two.
- Why not “No leg”? 1 and 3 leave a remainder of 1, so the test is true for them.
- Why not “Legs 0 and 2”? 0 and 2 are even: % 2 gives 0 for them.
“A list of legs” is lesson 6 of 6 in Encoder Edge, Robotics World 2. Free with an account. Lessons unlock in order, so you’ll do the 12 before it first.
Questions people ask
How do I add left and right turns to the list?
Store what each leg does as well as how long it is: a second array of actions, or a small struct per leg with a type and a length. A signed length works too, positive for one direction and negative for the other. The leg counter and reset stay exactly the same.
Why does my robot skip legs?
The tick count isn't reset when a leg ends. The next leg's test sees a count that's already past its length and ends it on the same pass. Set ticks back to 0 at every leg change.
How long will the route take?
Add up all the leg lengths and multiply by the time between ticks. 4, 2 and 4 is 10 ticks; at a tick every 0.2 s that's 2 seconds, straight until 0.8 s, pivoting until 1.2 s, straight again until 2.0 s.
Related topics
- How to turn a robot 90 degrees with encoder ticksA turn timed with delay() changes with the floor. A turn counted in ticks doesn't.
- How to program a robot as a state machineOne state at a time, each with its own job and its own ways out.
- How a wheel encoder measures distanceCount the notches as the wheel turns, and you know how far the robot went.
- How a robot knows where it is with dead reckoningAdd up every step in the direction you face, and you know where you are, roughly.