├── How to Make Line Follower And Obstacle Avoiding Robot.png └── Line_Follower_And_Obstacle_Avoiding_Robot.ino /How to Make Line Follower And Obstacle Avoiding Robot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/embeddedlab786/Line_Follower_with_Obstacle_Avoiding_Robot/7b1e309ac3ddcd05909993c1dbc7c7c65e61bea9/How to Make Line Follower And Obstacle Avoiding Robot.png -------------------------------------------------------------------------------- /Line_Follower_And_Obstacle_Avoiding_Robot.ino: -------------------------------------------------------------------------------- 1 | 2 | #define enA 10//Enable1 L298 Pin enA 3 | #define in1 9 //Motor1 L298 Pin in1 4 | #define in2 8 //Motor1 L298 Pin in1 5 | #define in3 7 //Motor2 L298 Pin in1 6 | #define in4 6 //Motor2 L298 Pin in1 7 | #define enB 5 //Enable2 L298 Pin enB 8 | 9 | #define L_S A0 //ir sensor Left 10 | #define R_S A1 //ir sensor Right 11 | 12 | #define echo A2 //Echo pin 13 | #define trigger A3 //Trigger pin 14 | 15 | #define servo A5 16 | 17 | int Set=15; 18 | int distance_L, distance_F, distance_R; 19 | 20 | void setup(){ // put your setup code here, to run once 21 | 22 | Serial.begin(9600); // start serial communication at 9600bps 23 | 24 | pinMode(R_S, INPUT); // declare if sensor as input 25 | pinMode(L_S, INPUT); // declare ir sensor as input 26 | 27 | pinMode(echo, INPUT );// declare ultrasonic sensor Echo pin as input 28 | pinMode(trigger, OUTPUT); // declare ultrasonic sensor Trigger pin as Output 29 | 30 | pinMode(enA, OUTPUT); // declare as output for L298 Pin enA 31 | pinMode(in1, OUTPUT); // declare as output for L298 Pin in1 32 | pinMode(in2, OUTPUT); // declare as output for L298 Pin in2 33 | pinMode(in3, OUTPUT); // declare as output for L298 Pin in3 34 | pinMode(in4, OUTPUT); // declare as output for L298 Pin in4 35 | pinMode(enB, OUTPUT); // declare as output for L298 Pin enB 36 | 37 | analogWrite(enA, 200); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed 38 | analogWrite(enB, 200); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed 39 | 40 | pinMode(servo, OUTPUT); 41 | 42 | for (int angle = 70; angle <= 140; angle += 5) { 43 | servoPulse(servo, angle); } 44 | for (int angle = 140; angle >= 0; angle -= 5) { 45 | servoPulse(servo, angle); } 46 | 47 | for (int angle = 0; angle <= 70; angle += 5) { 48 | servoPulse(servo, angle); } 49 | 50 | distance_F = Ultrasonic_read(); 51 | 52 | delay(500); 53 | } 54 | 55 | 56 | void loop(){ 57 | //============================================== 58 | // Line Follower and Obstacle Avoiding 59 | //============================================== 60 | 61 | distance_F = Ultrasonic_read(); 62 | Serial.print("D F=");Serial.println(distance_F); 63 | 64 | 65 | //if Right Sensor and Left Sensor are at White color then it will call forword function 66 | if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){ 67 | if(distance_F > Set){forword();} 68 | else{Check_side();} 69 | } 70 | 71 | //if Right Sensor is Black and Left Sensor is White then it will call turn Right function 72 | else if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){turnRight();} 73 | 74 | //if Right Sensor is White and Left Sensor is Black then it will call turn Left function 75 | else if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){turnLeft();} 76 | 77 | delay(10); 78 | } 79 | 80 | void servoPulse (int pin, int angle){ 81 | int pwm = (angle*11) + 500; // Convert angle to microseconds 82 | digitalWrite(pin, HIGH); 83 | delayMicroseconds(pwm); 84 | digitalWrite(pin, LOW); 85 | delay(50); // Refresh cycle of servo 86 | } 87 | 88 | 89 | //**********************Ultrasonic_read**************************** 90 | long Ultrasonic_read(){ 91 | digitalWrite(trigger, LOW); 92 | delayMicroseconds(2); 93 | digitalWrite(trigger, HIGH); 94 | delayMicroseconds(10); 95 | long time = pulseIn (echo, HIGH); 96 | return time / 29 / 2; 97 | } 98 | 99 | void compareDistance(){ 100 | if(distance_L > distance_R){ 101 | turnLeft(); 102 | delay(500); 103 | forword(); 104 | delay(600); 105 | turnRight(); 106 | delay(500); 107 | forword(); 108 | delay(600); 109 | turnRight(); 110 | delay(400); 111 | } 112 | else{ 113 | turnRight(); 114 | delay(500); 115 | forword(); 116 | delay(600); 117 | turnLeft(); 118 | delay(500); 119 | forword(); 120 | delay(600); 121 | turnLeft(); 122 | delay(400); 123 | } 124 | } 125 | 126 | void Check_side(){ 127 | Stop(); 128 | delay(100); 129 | for (int angle = 70; angle <= 140; angle += 5) { 130 | servoPulse(servo, angle); } 131 | delay(300); 132 | distance_R = Ultrasonic_read(); 133 | Serial.print("D R=");Serial.println(distance_R); 134 | delay(100); 135 | for (int angle = 140; angle >= 0; angle -= 5) { 136 | servoPulse(servo, angle); } 137 | delay(500); 138 | distance_L = Ultrasonic_read(); 139 | Serial.print("D L=");Serial.println(distance_L); 140 | delay(100); 141 | for (int angle = 0; angle <= 70; angle += 5) { 142 | servoPulse(servo, angle); } 143 | delay(300); 144 | compareDistance(); 145 | } 146 | 147 | void forword(){ //forword 148 | digitalWrite(in1, LOW); //Left Motor backword Pin 149 | digitalWrite(in2, HIGH); //Left Motor forword Pin 150 | digitalWrite(in3, HIGH); //Right Motor forword Pin 151 | digitalWrite(in4, LOW); //Right Motor backword Pin 152 | } 153 | 154 | void backword(){ //backword 155 | digitalWrite(in1, HIGH); //Left Motor backword Pin 156 | digitalWrite(in2, LOW); //Left Motor forword Pin 157 | digitalWrite(in3, LOW); //Right Motor forword Pin 158 | digitalWrite(in4, HIGH); //Right Motor backword Pin 159 | } 160 | 161 | void turnRight(){ //turnRight 162 | digitalWrite(in1, LOW); //Left Motor backword Pin 163 | digitalWrite(in2, HIGH); //Left Motor forword Pin 164 | digitalWrite(in3, LOW); //Right Motor forword Pin 165 | digitalWrite(in4, HIGH); //Right Motor backword Pin 166 | } 167 | 168 | void turnLeft(){ //turnLeft 169 | digitalWrite(in1, HIGH); //Left Motor backword Pin 170 | digitalWrite(in2, LOW); //Left Motor forword Pin 171 | digitalWrite(in3, HIGH); //Right Motor forword Pin 172 | digitalWrite(in4, LOW); //Right Motor backword Pin 173 | } 174 | 175 | void Stop(){ //stop 176 | digitalWrite(in1, LOW); //Left Motor backword Pin 177 | digitalWrite(in2, LOW); //Left Motor forword Pin 178 | digitalWrite(in3, LOW); //Right Motor forword Pin 179 | digitalWrite(in4, LOW); //Right Motor backword Pin 180 | } 181 | --------------------------------------------------------------------------------