Home Line Following Competition Arduino Line Follower Programming Guide
Arduino Line Follower Programming Guide PDF Print E-mail
Written by Adam Stambler   
Wednesday, 21 October 2009 12:34

Arduino is a great platform for both experienced programmers and those who have never programmed before.  It provides an easy interfaces that abstracts most of the hard microcontroller specific work.  Instead, you can deal with their easy programming API.

Getting started programming the Arduino

1.      There are very nice step-by-step instructions at http://arduino.cc/en/Guide/HomePage. Go there and follow the instructions for your operating system.

a.      Some notes on the instructions:

                                                             i.      Chances are Windows will be able to automatically install the USB-serial drivers. If it can’t, the instructions on the Arduino site explain how to manually install the drivers.

                                                            ii.      Our board is the “Arduino Duemilanove or Nano w/ ATmega328”.

                                                          iii.      If you have serial Bluetooth devices (possibly a cellphone), it will slow things way down. Cody knows how to fix this.

b.      Notes for Linux

                                                             i.      The Linux install is a little painful.

                                                            ii.      You’ll need to install gcc-avr, avrdude, librxtx-java, avr-libc, binutils-avr

                                                          iii.      Copy /usr/lib/librxtxSerial.so to the lib/ directory in the Arduino IDE’s files

2.      Try the blink example. (File -> Examples -> Digital -> Blink). It is simple and well-commented.


 


a.     Connect the controller to the computer.

b.     Select the right serial port from Tools -> Serial Port

c.     Click on the upload button at the top  .

                                               i.     Serial port ‘COM5’ not found. Did you select the right one from the Tools -> Serial Port menu?

1.     The cable might be unplugged

2.     You might not have the USB-serial driver installed

3.     You might have selected the wrong serial port

                                             ii.     error: <some compiler error>

1.     There’s a problem in the code, and it should highlight the offending line to tell you what it is.

d.     An LED on the blue board should start blinking. The green board is sort of in the way and makes it hard to see.

 

 

Need to Learn more about programming or the Arduino?

Never Programed before?  Start with this Arduino Programming Guide:
Sheep Dog Arduino Programming Tutorials


Reference Guide for Programming The arduiono
Arduino Programming Notebook

 

Test Your Line Following Kit

 Before you start using your line following kit, lets run a test program to make sure that every function on the kit (motors and line sensor) have been properly soldered together and are ready to go.  So download the line follower kit test sketch at the end of this article and upload it to your line follower.  Put the shield on the Arduino, connect the motors, connect the line sensor board, and power up the board.  You should see the motors go forward and then reverse continuously.  

 

Now, fire up the Arduino IDE and connect to the Arduino through through USB.  In the IDE, select Tools from the IDE menu bar and select the correct com under  Serial Port .  Finally, hit serial conection button (the button all the way on the right in the line of icons of the ide).  A text box should come up on the bottom and you should be getting a response from the robot.

 

Line Follower Specifics

The the line follower kit includes functions for controlling the motors and reading the sensor.  These functions are defined at the top of the each of the sketches included at the end of this document. You must include these function definitions at the top of every file in order to use them.  

 There are three functions you will want to use:

  

 
 void initMotorDriver();
void setLeftMotorSpeed(int velocity);  // velocity  = -255 to 255
void setRightMotorSpeed(int velocity);  // velocity =  -255 to 255 
void readLineSensor(int * outputArray); //pass  an array of 4 ints   ie.   int sensors[4];
  

 

Respectively they set up motor control and provide the interface for controlling the motors and reading the sensors.  

 

intMotorDriver() should be placed in the setup() function of your Arduino sketch.  This sets up the Pins to correctly operate the H-bridge and control the motors.


setLeftMotorSpeed and setRightMotorSpeed controls the velocity of each wheel.  The velocity can be -255 to 255.  

 

 

readLineSensor takes an array of four ints as its argument and assigns the current sensor values to the array.  To use this function, you need to do something like this:

 

  

 
 int sensors[4];
  readLineSensor(sensors);
 

 

 

  

 
 int sensors[4];
  readLineSensor(sensors);
 

 

Example Line Following Program

 This is the simplest line following program out there.  It uses only the 2 middle sensors to sense the location of the line.  It assumes that the robot has been placed on the course with the line going through the center of the sensor board.  Once turned on, the robot goes forward until one of the sensors senses the black line.  It then turns until that sensor no longer sees the black line. 

  

  
void setup(){
  Serial.begin(9600);
  initMotorDriver();
  setLeftMotorSpeed(0);
  setRightMotorSpeed(0);
//  initEncoders();

}

void loop()
{

  int sensors[4];

  readLineSensor(sensors);

  Serial.print("Sensor Values : " );

  for (int i =0; i <4; i++)
  {
    Serial.print( sensors[i]);
    Serial.print(" " );
  }

  Serial.println(" ");

  if(( sensors[0] <250) || (sensors[1] <250))  // 250 is a measured threshold between white & black
  {
    goRight();
    delay(100);
  }
   else if(( sensors[2] <250) || (sensors[3] <250))
  {
    goLeft();
    delay(100);
  }
  else
  goForward();
  
  delay(100);
  
  
} 
 
 

 

 

Last Updated ( Wednesday, 21 October 2009 23:36 )
 
Copyright © 2009 Robotics. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.