> For the complete documentation index, see [llms.txt](https://docs.idew.org/code-robotics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.idew.org/code-robotics/references/navigation-modes/line-following-counting-navigation.md).

# Line Following + Counting Navigation

When using line following + counting navigation, the robot follow a line while counting other lines it crosses, and then turns at a specific line number to start driving on a new line. The robot's path is programmed as an ordered sequence of specific line counts and turns.

* **ADVANTAGE:**  You can create complex line patterns with straight paths, curved paths, and loops. Even if the robot's turns aren't perfect, the robot will usually self-correct its direction as it starts to follow its new line path.
* **DISADVANTAGE:**  The robot can only stop or turn at a line intersection. You have to create a continuous line for each path.

Line following + counting navigation is similar to the directions that a person might give you to get to a destination in the country (such as "Follow this road as it curves around. At the second stop sign, turn right...").

## Example Task Scenario

In this task scenario, a restaurant robot will deliver a food order from the kitchen to Table 2 (red rectangles are cardboard boxes representing a wall), drive around the table (delivering each person's order), and then return to the "Start" line marker in the kitchen.

For the purposes of the demonstration, the distances traveled are much shorter than what would be required in an actual restaurant environment.

![](/files/-L_TlHrX8Q-pxowsFAbx)

## Example Code

Here is a possible way to code a custom function to perform this task scenario:

```cpp
void task1() {
  // Example of Line Following + Counting Navigation

  // drive out of kitchen from Start towards Table 2
  followCountLine(2); // 2nd line will be path to Table 2
  pivotAngle(-90); // turn left
  followCountLine(1); // next line is circle around table
  doubleBeep(); // alert guests that food has arrived
  pivotAngle(-90); // turn left
  followCountLine(1); // drive once around table

  // turn and return to kitchen
  pivotAngle(-90); // turn left
  followCountLine(1); // next line is main path
  pivotAngle(-90); // turn left
  followCountLine(1); // drive back into kitchen, next line is Start
  tripleBeep(); // alert staff that robot is ready for next order

  // at end of this task, reset for next task
  started = false;
  nextTask = 2;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.idew.org/code-robotics/references/navigation-modes/line-following-counting-navigation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
