9. Case 06: The Line-following Car#

9.1. Introduction#

Make Cutebotdrive along the black line.

9.2. Programming Preparation#

Please refer to: Preparing Programming Environment

9.3. Sample code#

```from cutebot import *

#Create a sample for Cutebot category
cutebot = Cutebot()

# While true, detect the status of the line-tracking sensor
# when both detect the black line, the Cutebot is moving forward at 50% speed.
# When the right probe detects that it is off from the black line, set the speed of the left wheel to 0% and the speed of the right wheel to 100%
# When the left probe detects that it is off from the black line, set the left wheel speed to 100% and the right wheel speed to 0%
while True:
tracing = cutebot.get_tracking()
if tracing == '11':
cutebot.set_speed(50,50)
if tracing == '10':
cutebot.set_speed(0,100)
if tracing == '01':
cutebot.set_speed(100,0)
```

Code details#

1. Import the modules that we need for the program：`cutebot` module contains classes and functions that operate on the Cutebot smart car.

```from cutebot import *
```
1. Create a sample for Cutebot category.

```cutebot = Cutebot()
```
1. While true, detect the status of the line-tracking sensor, when both detect the black line, the Cutebot is moving forward at 50% speed; when the right probe detects that it is off from the black line, set the speed of the left wheel to 0% and the speed of the right wheel to 100%; when the left probe detects that it is off from the black line, set the left wheel speed to 100% and the right wheel speed to 0%.

```while True:
tracing = cutebot.get_tracking()
if tracing == 11:
cutebot.set_speed(50,50)
if tracing == 10:
cutebot.set_speed(0,100)
if tracing == 01:
cutebot.set_speed(100,0)
```

9.4. Results#

After turning on the power, the Cutebot smart car drives along the black line.

9.5. Exploration#

Is it possible to use the Cutebot to make a cart that can prevent falls?