Arduino Stepper Line Follower

We’ve got ourselves a solution for soldering the infrared sensors and mounting them on a stepper ChickBot that doesn’t require a mess of string, Blu-Tack or glue. A short piece of dowel fitted between the four leads on the sensor grips them into the right place and you can then use the handy mounting points on the stepper motors to hold the dowel in place.

You then take the three leads from each sensor and wire them into the power side of controller board. The red and black cables go into the auxiliary power at the top of the board and then the right hand sensor lead goes into A1 and the left hand sensor goes in to A2. Left & right is defined by having the ChickBot facing away from you, with the ping pong caster closest to you.

You may need to unscrew the cable grips to get the two power leads in. The colour of the sensor leads is always red & black for power, the analog lead can be any colour that is spare – ChickBot loves recycling and so we use any spare cable that we have to hand.

For a track for the ChickBot to follow, you’ll need to either use paint or a maker pen to colour in a printed 2cm wide track – no tight turns. Testing is the best way here – your printer may be dark enough but earlier tests here indicate that it needs to be very dark, darker than most printers can manage by themselves. If your turns are too tight, just alter the Distance value in the program – reducing it means it will make smaller movements before checking where the line is.

Next, you’ll need some code which you can download here or you can Save As on the starter program and alter it.

First, we need some extra variables in the header:

// Storage for the sensor values
int LSensor = 0;
int RSensor = 0;
// This is the minimum light value that we expect when over white
int Calibrate = 150;
// How far to move between line checks
int Distance = 400;

And because the main loop runs continuously, we add in a WaitForButtonPress and delay at the bottom of the setup section:

// Beep & flash the LED
SoundAndLEDAlert();
// Just as it says, wait for the button to be pressed!
WaitForButtonPress();
// Wait a second for the finger to get out the way
delay( 1000 );

And then the main loop which does all the work:

void loop() {
// put your main code here, to run repeatedly:
// Read the sensor values
RSensor = analogRead(A1);
LSensor = analogRead(A2);
if ((RSensor < Calibrate) && (LSensor < Calibrate)) {
// We've gone right off the grid as both sensors say they can only see dark
SoundAndLEDAlert();
delay(5000);
} else {
for (int c = 0; c < Distance; c++) {
if (LSensor < Calibrate) { // Left over line
// Don't run the left motor
// But run the right motor
RightStep(RStep);
RStep = CalculateNextStep(RStep, Forward);
// so we steer right
} else if (RSensor < Calibrate) { // Right over line
// Run the left motor
LeftStep(LStep);
LStep = CalculateNextStep(LStep, Forward);
// But don't run the right motor
// so we steer left
} else {
// All ahead!
RightStep(RStep);
RStep = CalculateNextStep(RStep, Forward);
LeftStep(LStep);
LStep = CalculateNextStep(LStep, Forward);
}
delay(1);
} // for c
} // if
} // and do it all over again!