0% found this document useful (0 votes)
81 views

Line Following Robot Programming

This document defines the pin connections and functions for controlling a robot with motors and line sensors. It sets the pin modes for the motor direction and speed controls, push button, and 5 line sensors. The main loop reads the sensor values and uses if/else statements to call different motor functions like forward, backward, left, and right depending on the 16 possible line sensor situations.

Uploaded by

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

Line Following Robot Programming

This document defines the pin connections and functions for controlling a robot with motors and line sensors. It sets the pin modes for the motor direction and speed controls, push button, and 5 line sensors. The main loop reads the sensor values and uses if/else statements to call different motor functions like forward, backward, left, and right depending on the 16 possible line sensor situations.

Uploaded by

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

//motor shield pin

int dirR = 12;

int speedR = 10;

int dirL = 13;

int speedL = 11;

int pb = 1; //push button

//line sensor pin

int sensor1 = 12;

int sensor2 = 11;

int sensor3 = 10;

int sensor4 = 9;

int sensor5 = 8;

void setup()

pinMode(dirR, OUTPUT);

pinMode(speedR, OUTPUT);

pinMode(dirL, OUTPUT);

pinMode(speedL, OUTPUT);

pinMode(pb, INPUT);

pinMode(sensor1, INPUT);

pinMode(sensor2, INPUT);

pinMode(sensor3, INPUT);
pinMode(sensor4, INPUT);

pinMode(sensor5, INPUT);

while (digitalRead(pb) == 1)

stop();

void forward(int a, int b) //a=right motor, b=left motor

digitalWrite(dirR, HIGH);

analogWrite(speedR, a);

digitalWrite(dirL, HIGH);

analogWrite(speedL, b);

void backward(int a, int b) //a=right motor, b=left motor

digitalWrite(dirR, LOW);

analogWrite(speedR, a);

digitalWrite(dirL, LOW);

analogWrite(speedL, b);

}
void right(int a, int b) //a=right motor, b=left motor

digitalWrite(dirR, LOW);

analogWrite(speedR, a);

digitalWrite(dirL, HIGH);

analogWrite(speedL, b);

void left(int a, int b) //a=right motor, b=left motor

digitalWrite(dirR, HIGH);

analogWrite(speedR, a);

digitalWrite(dirL, LOW);

analogWrite(speedL, b);

void stop()

analogWrite(speedR, 0);

analogWrite(speedL, 0);

void loop()

int s1 = digitalRead(sensor1);
int s2 = digitalRead(sensor2);

int s3 = digitalRead(sensor3);

int s4 = digitalRead(sensor4);

int s5 = digitalRead(sensor5);

if (s1 == LOW && s2 == LOW && s3 == HIGH && s4 == LOW && s5 == LOW) //12*45 - Situation #1

forward(100, 100); //(right, lrft)

if (s1 == LOW && s2 == LOW && s3 == HIGH && s4 == HIGH && s5 == LOW) //12**5 - Situation #2

right(50, 100); //(right, lrft)

if (s1 == LOW && s2 == HIGH && s3 == HIGH && s4 == LOW && s5 == LOW) //1**45 - Situation #3

left(100, 50); //(right, lrft)

if (s1 == LOW && s2 == LOW && s3 == LOW && s4 == HIGH && s5 == LOW) //123*5 - Situation #4

right(20, 85); //(right, lrft)

if (s1 == LOW && s2 == HIGH && s3 == LOW && s4 == LOW && s5 == LOW) //1*345 - Situation #5

forward(85, 20); //(right, lrft)

}
if (s1 == LOW && s2 == LOW && s3 == LOW && s4 == HIGH && s5 == HIGH) //123** - Situation #6

left(20, 75); //(right, lrft)

if (s1 == HIGH && s2 == HIGH && s3 == LOW && s4 == LOW && s5 == LOW) //**345 - Situation #7

right(75, 20); //(right, lrft)

if (s1 == LOW && s2 == LOW && s3 == LOW && s4 == LOW && s5 == HIGH) //1234* - Situation #8

forward(0, 100); //(right, lrft)

if (s1 == HIGH && s2 == LOW && s3 == LOW && s4 == LOW && s5 == LOW) //*2345 - Situation #9

forward(100, 0); //(right, lrft)

if (s1 == LOW && s2 == LOW && s3 == LOW && s4 == LOW && s5 == LOW) //12345 - Situation #10

forward(0, 0); //(right, lrft)

if (s1 == LOW && s2 == HIGH && s3 == HIGH && s4 == HIGH && s5 == LOW) //1***5 - Situation #11

forward(100, 100); //(right, lrft)

if (s1 == HIGH && s2 == HIGH && s3 == HIGH && s4 == LOW && s5 == LOW) //***45 - Situation #12
{

left(100, 0); //(right, lrft)

if (s1 == LOW && s2 == LOW && s3 == HIGH && s4 == HIGH && s5 == HIGH) //12*** - Situation #13

forward(0, 100); //(right, lrft)

if (s1 == LOW && s2 == HIGH && s3 == HIGH && s4 == HIGH && s5 == HIGH) //1**** - Situation #14

forward(120, 0); //(right, lrft)

if (s1 == HIGH && s2 == HIGH && s3 == HIGH && s4 == HIGH && s5 == LOW) //****5 - Situation #15

forward(0, 120); //(right, lrft)

if (s1 == HIGH && s2 == HIGH && s3 == HIGH && s4 == HIGH && s5 == HIGH) //***** - Situation #16

stop();

You might also like