One of classic tasks in the world of robotics is edge detection. Edge detection being the method you would use to follow a line. This is a perfect task for the reflection mode of the colour sensor.

Image for post
Image for post

Edge detection as in following a line. Here’s a short script to illustrate the point.

What does it do. We define a colour sensor and link two motors into a drive pair. We than declare a target light reflection value and a fudge value.

We put everything into a endless loop and start scanning. But wait what are the targetLV and fudge values. We get the targetLV by placing our light sensor on the edge of the black line, so half on half off, and reading the value we get back. We get the fudge from trial and error. The higher the fudge value the wider the corrections will be. The bigger the curves the line the bigger the fudge needs to be.

Moving ahead, and yes I confess more hardware than you get in the standard set. You can do something very similar with two colour sensors, here is another example.

There are two target values because no two colour sensors seem return the same values. Note this script only follows straight lines.

You set it up the same way as did the previous example, with the sensors placed across the backline. Across as in both are half on and half off. Note this time I used a very small fudge value.

Let have some more fun with this one. Change the if statement and add a few more variables, so that when it comes off the end of the line it does a 180 and comes back, a sentry bot.

Note I used the angle command here cause I wanted the script execution to pause everything until it completed its turn.

Copy and paste this and see it works for you. It may not, cause you’re almost certainly going to have to change the variable called skip to match the value for your floor and even the angle value if your bot doesn’t have the same build as mine.

Of course you know where we heading with all this edge following. Solving mazes. You may have have seen videos of robots chasing their way around mazes. Let’s try and get our bot to do just that.

You have the basics with the previous tutorial, with the sentry bot. But there is snag. The logic behind the turns is too simple. To follow a maze you only turn around by 180 degrees when there is no other path to take.

I say maze, in this example we going follow a very simple maze, a line with 90 degree turns left and right in it. Here is the logic then.

You have the same code to detect the end of the line, but beyond that we made some changes.

Having come off the end, you move forward a little to get your bot centered over the end itself. [the sensors stick out a way in front]. We then look for the black line as we turn, in previous logic we simply swung around to face the other way.

But wait it isn’t quite that simple. We want to look for the lines that are at 90 degrees, and only turn by 180 degrees if we don’t find one on either side. That is why we have two while loops, the first looks to the right. If it finds nothing it swings to the left, only turning around of course if there is nothing.

Note too we tried not to use magic numbers here, introducing two more variables. Variables that make the functionality of the code more obvious.

We left a few print statements in too to help you understand what was happening as your running your robot.

Watching my bot doing the track it occured to me that it surely was possible to do this with a single colour sensor, along with a gyro this time. Here the code.

As before I left a few print statements in here to give you an idea of what is happening.

It does the same thing as the previous script, but this time we only turn by 95 degrees in each direction, the goal to follow a line that never dead ends in a dead end.

Written by

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store