I made a arduino line following robot.
for the line sensor i used a 5 sensor polou ir sensor array
for the servo power I used 6v
and for the arduino a 9v
the servo power is connected with a switch so i can control when they can move. (allows programming easily)
i used a motor shield because i want to expand on this project.
the parts list is:
Acrylic base
Two continuous rotation servos
One arduino duemilanove
One motor shield
One switch
One polou 8 sensor array (split into 6)
Two wheels
A servo mount
Battery pack
9v battery
4 rechargable triple a's
Lots of screws
A 13 year old
The pictures are high quality so i cant upload them or them
[url]http://aklem.com/pics%20of%20robot/HPIM2562.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2567.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2568.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2569.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2572.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2573.JPG[/url]
The code is this
[CODE=rich]
#include <Servo.h> //allows control of servos using a easy to use library
Servo Leftservo; //creats left servo object
Servo Rightservo; //creats right servo object
int raw1, raw2, raw3, raw4, raw5, raw6; //creates all the variables for storing the raw values
boolean sen1, sen2, sen3, sen4, sen5, sen6; //creates all the variables for storing the calibrated values (true or false)
int lastline = -5;
int lineplace;
int line;
void setup() //setups all the values, attaches the servos and begins serial communication
{
Leftservo.attach(9);
Rightservo.attach(10);
Serial.begin(9600);
raw1 = analogRead(1);
raw2 = analogRead(2);
raw3 = analogRead(3);
raw4 = analogRead(4);
raw5 = analogRead(5);
raw6 = analogRead(0);
}
//these are all the functions that you can call to move your robot
void stopmoving()
{
Leftservo.write(90);
Rightservo.write(95);
}
void spinleft()
{
Leftservo.write(0);
Rightservo.write(0);
}
void spinright()
{
Leftservo.write(180);
Rightservo.write(180);
}
void arcleft()
{
Leftservo.write(115);
Rightservo.write(0);
}
void arcright()
{
Leftservo.write(180);
Rightservo.write(75);
}
void moveleft()
{
Leftservo.write(110);
Rightservo.write(0);
}
void moveright()
{
Leftservo.write(180);
Rightservo.write(80);
}
void sharpleft()
{
Leftservo.write(102);
Rightservo.write(0);
}
void sharpright()
{
Leftservo.write(180);
Rightservo.write(87);
}
void moveforward()
{
Leftservo.write(180);
Rightservo.write(0);
}
void movebackward()
{
Leftservo.write(0);
Rightservo.write(180);
}
int getline() //returns line position
{
raw1 = analogRead(5);
raw2 = analogRead(4);
raw3 = analogRead(3);
raw4 = analogRead(2);
raw5 = analogRead(1);
raw6 = analogRead(0);
if (raw1 > 500)
{
sen1 = 1;
}
if (raw1 < 500)
{
sen1 = 0;
}
if (raw2 > 500)
{
sen2 = 1;
}
if (raw2 < 500)
{
sen2 = 0;
}
if (raw3 > 500)
{
sen3 = 1;
}
if (raw3 < 500)
{
sen3 = 0;
}
if (raw4 > 500)
{
sen4 = 1;
}
if (raw4 < 500)
{
sen4 = 0;
}
if (raw5 > 500)
{
sen5 = 1;
}
if (raw5 < 500)
{
sen5 = 0;
}
if (raw6 > 500)
{
sen6 = 1;
}
if (raw6 < 500)
{
sen6 = 0;
}
if (sen1 == 1)
{
lineplace = -5;
}
if (sen1 == 1 && sen2 == 1)
{
lineplace = -4;
}
if (sen2 == 1 && sen1 == 0)
{
lineplace = -3;
}
if (sen2 == 1 && sen3 == 1)
{
lineplace = -2;
}
if (sen3 == 1 && sen2 == 0)
{
lineplace = -1;
}
if (sen3 == 1 && sen4 == 1)
{
lineplace = 0;
}
if (sen4 == 1 && sen3 == 0)
{
lineplace = 1;
}
if (sen4 == 1 && sen5 == 1)
{
lineplace = 2;
}
if (sen5 == 1 && sen4 == 0)
{
lineplace = 3;
}
if (sen5 == 1 && sen6 == 1)
{
lineplace = 4;
}
if (sen6 == 1 & sen5 == 0)
{
lineplace = 5;
}
if (sen1 == 0 && sen2 == 0 && sen3 == 0 && sen4 == 0 && sen5 == 0 && sen6 == 0)
{
lineplace = -10;
}
if (sen1 == 1 && sen2 == 1 && sen3 == 1 && sen4 == 1 && sen5 == 1 && sen6 == 1)
{
lineplace = 10;
}
return lineplace;
}
void loop()
{
line = getline();
while (line == -10)
{
line = getline();
Serial.print("No line detected (spin to where it was last found):");
Serial.println(line, DEC);
if (lastline == -5)
{
spinleft();
Serial.println("spinning left ");
}
if (lastline == 5)
{
spinright();
Serial.println("spinning right ");
}
}
while (line == 10)
{
stopmoving();
line = getline();
Serial.print("END (or picked up)(stop)");
Serial.println(line, DEC);
}
while (line == -5 || line == -4)
{
sharpleft();
line = getline();
Serial.print("sharp left: ");
Serial.println(line, DEC);
lastline = -5;
}
while (line == -3 || line == -2)
{
moveleft();
line = getline();
Serial.print("move left: ");
Serial.println(line, DEC);
}
while (line == -1)
{
arcleft();
line = getline();
Serial.print("arc left: ");
Serial.println(line, DEC);
}
while (line == 0)
{
moveforward();
line = getline();
Serial.print("move forward: ");
Serial.println(line, DEC);
}
while (line == 1)
{
arcright();
line = getline();
Serial.print("arc right: ");
Serial.println(line, DEC);
}
while (line == 2 || line == 3)
{
moveright();
line = getline();
Serial.print("move right: ");
Serial.println(line, DEC);
}
while (line == 4 || line == 5)
{
sharpright();
line = getline();
Serial.print("sharp right: ");
Serial.println(line, DEC);
lastline = 5;
}
stopmoving();
Serial.println(line, DEC);
}
[/CODE]
for the line sensor i used a 5 sensor polou ir sensor array
for the servo power I used 6v
and for the arduino a 9v
the servo power is connected with a switch so i can control when they can move. (allows programming easily)
i used a motor shield because i want to expand on this project.
the parts list is:
Acrylic base
Two continuous rotation servos
One arduino duemilanove
One motor shield
One switch
One polou 8 sensor array (split into 6)
Two wheels
A servo mount
Battery pack
9v battery
4 rechargable triple a's
Lots of screws
A 13 year old
The pictures are high quality so i cant upload them or them
[url]http://aklem.com/pics%20of%20robot/HPIM2562.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2567.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2568.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2569.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2572.JPG[/url]
[url]http://aklem.com/pics%20of%20robot/HPIM2573.JPG[/url]
The code is this
[CODE=rich]
#include <Servo.h> //allows control of servos using a easy to use library
Servo Leftservo; //creats left servo object
Servo Rightservo; //creats right servo object
int raw1, raw2, raw3, raw4, raw5, raw6; //creates all the variables for storing the raw values
boolean sen1, sen2, sen3, sen4, sen5, sen6; //creates all the variables for storing the calibrated values (true or false)
int lastline = -5;
int lineplace;
int line;
void setup() //setups all the values, attaches the servos and begins serial communication
{
Leftservo.attach(9);
Rightservo.attach(10);
Serial.begin(9600);
raw1 = analogRead(1);
raw2 = analogRead(2);
raw3 = analogRead(3);
raw4 = analogRead(4);
raw5 = analogRead(5);
raw6 = analogRead(0);
}
//these are all the functions that you can call to move your robot
void stopmoving()
{
Leftservo.write(90);
Rightservo.write(95);
}
void spinleft()
{
Leftservo.write(0);
Rightservo.write(0);
}
void spinright()
{
Leftservo.write(180);
Rightservo.write(180);
}
void arcleft()
{
Leftservo.write(115);
Rightservo.write(0);
}
void arcright()
{
Leftservo.write(180);
Rightservo.write(75);
}
void moveleft()
{
Leftservo.write(110);
Rightservo.write(0);
}
void moveright()
{
Leftservo.write(180);
Rightservo.write(80);
}
void sharpleft()
{
Leftservo.write(102);
Rightservo.write(0);
}
void sharpright()
{
Leftservo.write(180);
Rightservo.write(87);
}
void moveforward()
{
Leftservo.write(180);
Rightservo.write(0);
}
void movebackward()
{
Leftservo.write(0);
Rightservo.write(180);
}
int getline() //returns line position
{
raw1 = analogRead(5);
raw2 = analogRead(4);
raw3 = analogRead(3);
raw4 = analogRead(2);
raw5 = analogRead(1);
raw6 = analogRead(0);
if (raw1 > 500)
{
sen1 = 1;
}
if (raw1 < 500)
{
sen1 = 0;
}
if (raw2 > 500)
{
sen2 = 1;
}
if (raw2 < 500)
{
sen2 = 0;
}
if (raw3 > 500)
{
sen3 = 1;
}
if (raw3 < 500)
{
sen3 = 0;
}
if (raw4 > 500)
{
sen4 = 1;
}
if (raw4 < 500)
{
sen4 = 0;
}
if (raw5 > 500)
{
sen5 = 1;
}
if (raw5 < 500)
{
sen5 = 0;
}
if (raw6 > 500)
{
sen6 = 1;
}
if (raw6 < 500)
{
sen6 = 0;
}
if (sen1 == 1)
{
lineplace = -5;
}
if (sen1 == 1 && sen2 == 1)
{
lineplace = -4;
}
if (sen2 == 1 && sen1 == 0)
{
lineplace = -3;
}
if (sen2 == 1 && sen3 == 1)
{
lineplace = -2;
}
if (sen3 == 1 && sen2 == 0)
{
lineplace = -1;
}
if (sen3 == 1 && sen4 == 1)
{
lineplace = 0;
}
if (sen4 == 1 && sen3 == 0)
{
lineplace = 1;
}
if (sen4 == 1 && sen5 == 1)
{
lineplace = 2;
}
if (sen5 == 1 && sen4 == 0)
{
lineplace = 3;
}
if (sen5 == 1 && sen6 == 1)
{
lineplace = 4;
}
if (sen6 == 1 & sen5 == 0)
{
lineplace = 5;
}
if (sen1 == 0 && sen2 == 0 && sen3 == 0 && sen4 == 0 && sen5 == 0 && sen6 == 0)
{
lineplace = -10;
}
if (sen1 == 1 && sen2 == 1 && sen3 == 1 && sen4 == 1 && sen5 == 1 && sen6 == 1)
{
lineplace = 10;
}
return lineplace;
}
void loop()
{
line = getline();
while (line == -10)
{
line = getline();
Serial.print("No line detected (spin to where it was last found):");
Serial.println(line, DEC);
if (lastline == -5)
{
spinleft();
Serial.println("spinning left ");
}
if (lastline == 5)
{
spinright();
Serial.println("spinning right ");
}
}
while (line == 10)
{
stopmoving();
line = getline();
Serial.print("END (or picked up)(stop)");
Serial.println(line, DEC);
}
while (line == -5 || line == -4)
{
sharpleft();
line = getline();
Serial.print("sharp left: ");
Serial.println(line, DEC);
lastline = -5;
}
while (line == -3 || line == -2)
{
moveleft();
line = getline();
Serial.print("move left: ");
Serial.println(line, DEC);
}
while (line == -1)
{
arcleft();
line = getline();
Serial.print("arc left: ");
Serial.println(line, DEC);
}
while (line == 0)
{
moveforward();
line = getline();
Serial.print("move forward: ");
Serial.println(line, DEC);
}
while (line == 1)
{
arcright();
line = getline();
Serial.print("arc right: ");
Serial.println(line, DEC);
}
while (line == 2 || line == 3)
{
moveright();
line = getline();
Serial.print("move right: ");
Serial.println(line, DEC);
}
while (line == 4 || line == 5)
{
sharpright();
line = getline();
Serial.print("sharp right: ");
Serial.println(line, DEC);
lastline = 5;
}
stopmoving();
Serial.println(line, DEC);
}
[/CODE]