0% found this document useful (0 votes)
79 views2 pages

LFR Code

This document contains code for controlling the motors of a robot to follow a black line. It defines integer variables to represent motor pins and sensor thresholds. The setup function initializes the motor pins as outputs and sensor pins as inputs. Functions are defined to move the robot forward, turn left or right, and turn in a circle. The loop function reads the sensor values, and uses conditional statements to call the appropriate motor control function depending on the sensor values to track the black line.

Uploaded by

Chiranjeev Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views2 pages

LFR Code

This document contains code for controlling the motors of a robot to follow a black line. It defines integer variables to represent motor pins and sensor thresholds. The setup function initializes the motor pins as outputs and sensor pins as inputs. Functions are defined to move the robot forward, turn left or right, and turn in a circle. The loop function reads the sensor values, and uses conditional statements to call the appropriate motor control function depending on the sensor values to track the black line.

Uploaded by

Chiranjeev Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

int lMotFwd=12;

int rMotFwd=6;
int lMotRev=13;
int rMotRev=7;

int onblack= 100; // reading for black line from sensor is less than this
int onWhite= 700; // reading for white line is greater than this

void setup() {

Serial.begin(9600);

// pinMode(A0,INPUT);
pinMode(A1,INPUT); //extreme left sensor
pinMode(A2,INPUT);
pinMode(A3,INPUT); // middle sensor
pinMode(A4,INPUT);
pinMode(A5,INPUT); // extreme right sensor

pinMode(lMotFwd,OUTPUT);
pinMode(rMotFwd,OUTPUT);
pinMode(lMotRev,OUTPUT);
pinMode(rMotRev,OUTPUT);

void moveForward()
{
digitalWrite(lMotFwd,HIGH);
digitalWrite(rMotFwd,HIGH);
}

void turnLeft()
{
digitalWrite(lMotFwd,LOW);
digitalWrite(rMotFwd,HIGH);
}

void turnRight()
{
digitalWrite(lMotFwd,HIGH);
digitalWrite(rMotFwd,LOW);
}

void turnCircle()
{
digitalWrite(lMotFwd,LOW);
digitalWrite(rMotFwd,LOW);
digitalWrite(lMotRev,HIGH);
digitalWrite(rMotFwd,HIGH);
}

void Stop()
{

digitalWrite(lMotFwd,LOW);
digitalWrite(rMotFwd,LOW);
digitalWrite(lMotRev,LOW);
digitalWrite(rMotRev,LOW);

void loop() {
// put your main code here, to run repeatedly:

int sensor1=analogRead(A1);//sensor1
int sensor2=analogRead(A2);//sensor2
int sensor3=analogRead(A3);//sensor3
int sensor4=analogRead(A4);//sensor4
int sensor5=analogRead(A5);//sensor5

Serial.println(sensor1);

if((sensor5<onblack)&&(sensor2<onblack||sensor3<onblack||sensor4<onblack))
{

turnLeft();
}else
if((sensor1>onWhite && sensor5>onWhite)&&(sensor2<onblack||sensor3<onblack||
sensor4<onblack))
{moveForward();
}else
if((sensor5<onblack))
{
turnLeft();

}else
if(sensor1<onblack)
{
turnRight();
}
else
if( sensor1>onWhite && sensor5>onWhite && sensor2<onWhite && sensor2<onWhite &&
sensor4<onWhite)
{
moveForward();
delay(15);

turnCircle();
delay(40);

You might also like