Information
Step 1
What is causing inconsistent sensor readings?

Although there are versions of the sensor which have its own IR emitter- the sensors we are using actually rely on the ambient IR light in the room This mean some areas of the board will be lighter or darker than others, so readings will be higher or lower for the same material.

Planning
Step 2
How to handle this problem

We need to have the sensor perform a calibration routine when it is turned on.

You'll turn on your robot and then slide it around to show it the lightest and darkest colors it will encounter on the track.

It will record a minimum and maximum value for each sensor. Then we can say anything more than 25% of the way between these two values is the line Step 3
Step 4
Write a calibrate function

Write a function which finds the minimum and maximum sensor values after a few seconds, by taking several readings

```void calibrateSensors() {
//PseudoCode
repeat 200 times

IF [any sensor value is smaller than the
minimum value for that sensor]
THEN
[set the minimum value to the current sensor value]

[same thing for the maximum value]
}```
Step 5
Check if you function works properly

Display your calibrated minimum and maximum sensor values for each sensor via the serial monitor.

Ensure the values your sensor is picking up mostly fall between these values.

Example serial output:

```   Min: 100   |   Min: 87   |   Min: 65
Max: 987   |   Max: 830   |   Max: 753

Left: 137   |   Mid: 120   |   Right: 87
Left: 231   |   Mid: 214   |   Right: 194
Left: 254   |   Mid: 220   |   Right: 203
...
...
Left: 854   |   Mid: 842   |   Right: 800```

(It most likely won't be perfect but it should be close)

Planning
Step 6
One more problem

Right now our sensors are giving us numeric values. The problem with this is that our sensors give us different number values for the same amount of reflection A percentage value would tell us how much percent of the way between the min and max values we are

And these values will agree with each other Information
Step 7
How to take a percentage

How much percent of the blue bar has been filled in with red?

Simply divide the amount of red by the total size of the bar Percentages are a number between 0.00 and 1.00.

50% is denoted by 0.50
27% is denoted by 0.27
100% is 1.00
etc

Information
Step 8
What percent of our sensor reading falls between the minimum and maximum calibrated values?

We need to first subtract the minimum sensor value from the sensor reading.

Then we can take a percentage of the remaining value. Now we can calculate the percentage of the way between the min and max values.

Step 9

Say we are looking at sensorValue.

We need to find how much percent of the way `sensorValue` falls between `sensorMin` and `sensorMax`

The previous steps outlines a procedure for calculating this

Challenge
Step 10

Here is the pseudocode for this function

```//call readSensor()

//for each sensor
//find the percentage between the min and max value
//multiply that percentage by 1000
//use the constrain function to prevent any values being over 1000 or below 0```

How to use constrain()

This way all of your sensors will output a value between 0 and 1000, and they should all get about the same reading for the same refelction

Planning
Step 11
We're about to use the functions you've just created to build a simple line follower

First you will need to build a robot chassis. Here is an example chassis used for a line follower.

Remember what the sensor manual had to say about the optimal distance it must be from the ground! Challenge
Step 12
Make a rudimentary line follower

Here is an example of the first line follower we will be making.

In the next lesson we will be making a much more complicated and smooth line follower, but we need to start from the basics

Here is the pseudocode for such a robot

``````If the middle sensor sees black
move forward
else
if the left sensor sees black
turn left
else
turn right
``````

### Try one of these lessons next: PID control

Account

MVCode

Created By jamie

Course:

Feedback Control Systems: Line follower

Access Level

public

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.