MAKING LINE FOLLOWER.PLS SOMEONE HELP ME! :(
been doing project days , problem line follower robot cannot follow line when takes turn left/right. im using adafruit motor shield, 2 tcrt5000 sensors, , 4wd smart car chassis. don't know anymore been calibrating days , need help.
i made 2 type of program 1 robot following black line , 1 robot following white surface.
line follower reading black line
line follower reading white background
i made 2 type of program 1 robot following black line , 1 robot following white surface.
line follower reading black line
code: [select]
#include <afmotor.h>
af_dcmotor motorr1(1); //motor front-right
af_dcmotor motorr2(2); //motor back-right
af_dcmotor motorl1(3); //motor front-left
af_dcmotor motorl2(4); //motor back-left
/*------ program line follower robot using arduino----- */
/*-------definning inputs------*/
#define ls 53 // left sensor. digitalreading output
#define rs 52 // right sensor. digitalreading output
/*-------definning outputs------*/
int tcrt1pin = a14;// analogreading
int tcrt2pin = a15;// analogreading
void setup()
{
serial.begin(9600);
pinmode(ls, input); // digitalreading left sensor
pinmode(rs, input);// digitalreading right sensor
}
void loop()
{
int tcrt1 = analogread(tcrt1pin); //sensor tcrt1
int tcrt2 = analogread(tcrt2pin); //sensor tcrt2
analogwrite(52,tcrt1/4); //sensor tcrt1
analogwrite(53,tcrt2/4); //sensor tcrt2
serial.println(tcrt1);
serial.println(tcrt2);
if(digitalread(ls) && digitalread(rs)) // move forward
{
forward();//forward direction
}
if(!(digitalread(ls)) && digitalread(rs)) // turn right
{
turnright(); //turn right direction
}
if(digitalread(ls) && !(digitalread(rs))) // turn left
{
turnleft();//turn left direction
}
if(!(digitalread(ls)) && !(digitalread(rs))) // stop
{
motorstop();
}
delay(150);
}
void forward()
{
motorr1.setspeed(100);
motorr2.setspeed(100);
motorl1.setspeed(100);
motorl2.setspeed(100);
motorr1.run(forward);
motorr2.run(forward);
motorl1.run(forward);
motorl2.run(forward);
digitalwrite(51,high);
digitalwrite(31,high);
delay(100);
}
void turnright()
{
motorr1.setspeed(250);
motorr2.setspeed(0);
motorl1.setspeed(0);
motorl2.setspeed(250);
motorr1.run(forward); ////// tcrt1 sensing black color.output high.//////
motorr2.run(release); //////////////////////////////////////////////////////
motorl1.run(release);
motorl2.run(forward);
digitalwrite(31,high);
delay(100);
}
void turnleft()
{
motorr1.setspeed(0);
motorr2.setspeed(250);
motorl1.setspeed(250);
motorl2.setspeed(0);
motorr1.run(release);
motorr2.run(forward);
motorl1.run(forward);
motorl2.run(release);
digitalwrite(51,high);
delay(100);
}
void motorstop()
{
motorr1.run(release);
motorr2.run(release);
motorl1.run(release);
motorl2.run(release);
delay(0);
digitalwrite(51,low);
digitalwrite(31,low);
}
line follower reading white background
code: [select]
#include <afmotor.h>
af_dcmotor motorr1(1); //motor front-right
af_dcmotor motorr2(2); //motor back-right
af_dcmotor motorl1(3); //motor front-left
af_dcmotor motorl2(4); //motor back-left
int tcrt1pin = a14; //analogreading
int tcrt2pin = a15; //analogreading
void setup()
{
delay(2000);
serial.begin(9600);
pinmode(52,input); //sensor tcrt1 output pin. digitalreading
pinmode(53,input); //sensor tcrt2 output pin. digitalreading
pinmode(51,output); //blue led output pin
pinmode(31,output); //red led output pin
}
void loop()
{
int tcrt1 = analogread(tcrt1pin); //sensor tcrt1.analogreading
int tcrt2 = analogread(tcrt2pin); //sensor tcrt2.analogreading
analogwrite(52,tcrt1/4); //sensor tcrt1 .analogreading
analogwrite(53,tcrt2/4); //sensor tcrt2 .analogreading
serial.println(tcrt1);
serial.println(tcrt2);
if ((tcrt1<=500)&&(tcrt2<=500))
{
forward();//forward direction
}
if ((tcrt1>=500)&&(tcrt2<=500))
{
turnright(); //turn right direction
}
if ((tcrt1<=500)&&(tcrt2>=500))
{
turnleft();//turn left direction
}
else if((tcrt1>=500)&&(tcrt2>=500))
{
motorstop();
}
delay(150);
}
void forward()
{
motorr1.setspeed(100);
motorr2.setspeed(100);
motorl1.setspeed(100);
motorl2.setspeed(100);
motorr1.run(forward);
motorr2.run(forward);
motorl1.run(forward);
motorl2.run(forward);
digitalwrite(51,high); //led
digitalwrite(31,high); //led
delay(100);
}
void turnright()
{
motorr1.setspeed(200);
motorr2.setspeed(0);
motorl1.setspeed(0);
motorl2.setspeed(200);
motorr1.run(forward); ////// tcrt1 sensing black color.output high.//////
motorr2.run(release); //////////////////////////////////////////////////////
motorl1.run(release);
motorl2.run(forward);
digitalwrite(31,high); //led
delay(100);
}
void turnleft()
{
motorr1.setspeed(0);
motorr2.setspeed(200);
motorl1.setspeed(200);
motorl2.setspeed(0);
motorr1.run(release);
motorr2.run(forward);
motorl1.run(forward);
motorl2.run(release);
digitalwrite(51,high); //led
delay(100);
}
void motorstop()
{
motorr1.run(release);
motorr2.run(release);
motorl1.run(release);
motorl2.run(release);
delay(0);
digitalwrite(51,low);
digitalwrite(31,low);
}
my line follower robot cannot follow line when takes turn left/right.it helpful if provided more details on problem.
unless code longer forum allow please post using code tags(</> button on toolbar) this:
line follower black line
code: [select]
#include <afmotor.h>
af_dcmotor motorr1(1); //motor front-right
af_dcmotor motorr2(2); //motor back-right
af_dcmotor motorl1(3); //motor front-left
af_dcmotor motorl2(4); //motor back-left
/*------ program line follower robot using arduino----- */
/*-------definning inputs------*/
#define ls 53 // left sensor
#define rs 52 // right sensor
/*-------definning outputs------*/
int tcrt1pin = a14;
int tcrt2pin = a15;
void setup()
{
serial.begin(9600);
pinmode(ls, input);
pinmode(rs, input);
}
void loop()
{
int tcrt1 = analogread(tcrt1pin); //sensor tcrt1
int tcrt2 = analogread(tcrt2pin); //sensor tcrt2
analogwrite(52,tcrt1/4); //sensor tcrt1
analogwrite(53,tcrt2/4); //sensor tcrt2
serial.println(tcrt1);
serial.println(tcrt2);
if(digitalread(ls) && digitalread(rs)) // move forward
{
forward();//forward direction
}
if(!(digitalread(ls)) && digitalread(rs)) // turn right
{
turnright(); //turn right direction
}
if(digitalread(ls) && !(digitalread(rs))) // turn left
{
turnleft();//turn left direction
}
if(!(digitalread(ls)) && !(digitalread(rs))) // stop
{
motorstop();
}
delay(150);
}
void forward()
{
motorr1.setspeed(100);
motorr2.setspeed(100);
motorl1.setspeed(100);
motorl2.setspeed(100);
motorr1.run(forward);
motorr2.run(forward);
motorl1.run(forward);
motorl2.run(forward);
digitalwrite(51,high);
digitalwrite(31,high);
delay(100);
}
void turnright()
{
motorr1.setspeed(250);
motorr2.setspeed(0);
motorl1.setspeed(0);
motorl2.setspeed(250);
motorr1.run(forward); ////// tcrt1 sensing black color.output high.//////
motorr2.run(release); //////////////////////////////////////////////////////
motorl1.run(release);
motorl2.run(forward);
digitalwrite(31,high);
delay(100);
}
void turnleft()
{
motorr1.setspeed(0);
motorr2.setspeed(250);
motorl1.setspeed(250);
motorl2.setspeed(0);
motorr1.run(release);
motorr2.run(forward);
motorl1.run(forward);
motorl2.run(release);
digitalwrite(51,high);
delay(100);
}
void motorstop()
{
motorr1.run(release);
motorr2.run(release);
motorl1.run(release);
motorl2.run(release);
delay(0);
digitalwrite(51,low);
digitalwrite(31,low);
}
what's purpose of this:
code: [select]
analogwrite(52,tcrt1/4); //sensor tcrt1
analogwrite(53,tcrt2/4); //sensor tcrt2
are ir leds on tcrt5000 sensors?
you have code:
code: [select]
#define ls 53 // left sensor
#define rs 52 // right sensor
/*-------definning outputs------*/
int tcrt1pin = a14;
int tcrt2pin = a15;
first off, i'd recommend making code more consistent. #defining pins , making others ints makes code more confusing necessary. i'd use const byte of them. second don't use ls , rs definitions:
code: [select]
analogwrite(52,tcrt1/4); //sensor tcrt1
analogwrite(53,tcrt2/4); //sensor tcrt2
why set ls , rs inputs:
code: [select]
pinmode(ls, input);
pinmode(rs, input);
and use them outputs in code:
code: [select]
analogwrite(52,tcrt1/4); //sensor tcrt1
analogwrite(53,tcrt2/4); //sensor tcrt2
what's on pins 51 , 31:
code: [select]
digitalwrite(51,high);
digitalwrite(31,high);
why didn't set pins outputs?
Arduino Forum > Using Arduino > Project Guidance > MAKING LINE FOLLOWER.PLS SOMEONE HELP ME! :(
arduino
Comments
Post a Comment