├── README.md ├── Diagrams ├── Battery-Pack.jpg ├── LFR_Circuit.jpg ├── sensor-count.png ├── LFR_Circuit 2.jpg ├── LFR_Circuit 3.jpg └── Battery-PAck_bms.jpg └── 5-Channel-Array-PID-Line-Follower-Robot └── 5-Channel-Array-PID-Line-Follower-Robot.ino /README.md: -------------------------------------------------------------------------------- 1 | If You Face Any Problem. Feel Free to knock me in my facebook page: https://www.facebook.com/aslamhossain3852 2 | -------------------------------------------------------------------------------- /Diagrams/Battery-Pack.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aslam-Hossain-YT/5-Channel-Array-PID-Line-Follower-Robot/HEAD/Diagrams/Battery-Pack.jpg -------------------------------------------------------------------------------- /Diagrams/LFR_Circuit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aslam-Hossain-YT/5-Channel-Array-PID-Line-Follower-Robot/HEAD/Diagrams/LFR_Circuit.jpg -------------------------------------------------------------------------------- /Diagrams/sensor-count.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aslam-Hossain-YT/5-Channel-Array-PID-Line-Follower-Robot/HEAD/Diagrams/sensor-count.png -------------------------------------------------------------------------------- /Diagrams/LFR_Circuit 2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aslam-Hossain-YT/5-Channel-Array-PID-Line-Follower-Robot/HEAD/Diagrams/LFR_Circuit 2.jpg -------------------------------------------------------------------------------- /Diagrams/LFR_Circuit 3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aslam-Hossain-YT/5-Channel-Array-PID-Line-Follower-Robot/HEAD/Diagrams/LFR_Circuit 3.jpg -------------------------------------------------------------------------------- /Diagrams/Battery-PAck_bms.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aslam-Hossain-YT/5-Channel-Array-PID-Line-Follower-Robot/HEAD/Diagrams/Battery-PAck_bms.jpg -------------------------------------------------------------------------------- /5-Channel-Array-PID-Line-Follower-Robot/5-Channel-Array-PID-Line-Follower-Robot.ino: -------------------------------------------------------------------------------- 1 | //motor pin defines 2 | #define IN1 6 3 | #define IN2 5 4 | #define IN3 4 5 | #define IN4 2 6 | #define enA 9 7 | #define enB 3 8 | 9 | //declaration of necessary variable to line follow 10 | int s[6], total, sensor_position; 11 | int threshold = 531; //set threshold as like i've shown in video 12 | float avg; 13 | int position[6] = { 1, 2, 3, 4, 5 }; 14 | int set_point = 3; 15 | 16 | //push button and led 17 | int button_pin = 12; 18 | int led = 13; 19 | bool button_pin_state; 20 | 21 | void setup() { 22 | //motor driver pins as output 23 | pinMode(IN3, OUTPUT); 24 | pinMode(IN4, OUTPUT); 25 | pinMode(IN1, OUTPUT); 26 | pinMode(IN2, OUTPUT); 27 | //button pin as input 28 | pinMode(button_pin, INPUT_PULLUP); 29 | //led pin as output 30 | pinMode(led, OUTPUT); 31 | Serial.begin(9600); 32 | } 33 | 34 | void loop() { 35 | button_pin_state = digitalRead(button_pin); 36 | display_value(); 37 | 38 | //when button is pressed then the robot will start to follow lines. 39 | while (button_pin_state != 1) { 40 | //blink led 41 | for (int i = 0; i < 3; ++i) { 42 | digitalWrite(led, HIGH); 43 | delay(100); 44 | digitalWrite(led, LOW); 45 | delay(100); 46 | } 47 | PID_LINE_FOLLOW(); //line follow using PID_Value 48 | } 49 | } 50 | 51 | void Sensor_reading() { 52 | sensor_position = 0; 53 | total = 0; 54 | for (byte i = 0; i < 5; i++) { //snesor data read from A0, A1, A2, A6, A7 55 | if (i > 2) { 56 | s[i] = analogRead(i + 3); 57 | } else { 58 | s[i] = analogRead(i); 59 | } 60 | if (s[i] > threshold) s[i] = 1; //analog value to digital conversion 61 | else s[i] = 0; 62 | sensor_position += s[i] * position[i]; 63 | total += s[i]; 64 | } 65 | if (total) avg = sensor_position / total; //average value 66 | } 67 | 68 | void PID_LINE_FOLLOW() { 69 | 70 | int kp = 50, kd = 500, PID_Value, P, D; 71 | float error, previous_error; 72 | int base_speed = 200, left_motor_speed, right_motor_speed, turn_speed = 100; 73 | char t; 74 | 75 | while (1) { 76 | Sensor_reading(); 77 | error = set_point - avg; 78 | D = kd * (error - previous_error); 79 | P = error * kp; 80 | PID_Value = P + D; 81 | previous_error = error; 82 | 83 | //adjusting motor speed to keep the robot in line 84 | right_motor_speed = base_speed - PID_Value; //right motor speed 85 | left_motor_speed = base_speed + PID_Value; //left motor speed 86 | motor(left_motor_speed, right_motor_speed); //when robot in straight position 87 | 88 | Sensor_reading(); 89 | if (total == 0) { 90 | digitalWrite(led, HIGH); 91 | if (t != 's') { 92 | if (t == 'r') motor(turn_speed, -turn_speed); 93 | else motor(-turn_speed, turn_speed); 94 | while (!s[2]) Sensor_reading(); 95 | digitalWrite(led, LOW); 96 | } 97 | } 98 | 99 | if (s[4] == 1 && s[0] == 0) t = 'l'; 100 | if (s[4] == 0 && s[0] == 1) t = 'r'; 101 | 102 | else if (total == 5) { 103 | Sensor_reading(); 104 | if (total == 5) { 105 | motor(0, 0); 106 | while (total == 5) Sensor_reading(); 107 | } else if (total == 0) t = 's'; 108 | } 109 | } 110 | } 111 | 112 | void display_value() { //display the analog value of sensor in serial monitor 113 | for (byte i = 0; i < 5; i++) { 114 | if (i > 2) { 115 | s[i] = analogRead(i + 3); 116 | } else { 117 | s[i] = analogRead(i); 118 | } 119 | Serial.print(String(s[i]) + " "); 120 | } 121 | Serial.println(); 122 | delay(50); 123 | } 124 | 125 | //motor run function 126 | void motor(int a, int b) { 127 | if (a > 0) { 128 | digitalWrite(IN3, 1); 129 | digitalWrite(IN4, 0); 130 | } else { 131 | a = -(a); 132 | digitalWrite(IN3, 0); 133 | digitalWrite(IN4, 1); 134 | } 135 | if (b > 0) { 136 | digitalWrite(IN1, 1); 137 | digitalWrite(IN2, 0); 138 | } else { 139 | b = -(b); 140 | digitalWrite(IN1, 0); 141 | digitalWrite(IN2, 1); 142 | } 143 | 144 | if (a > 200) a = 200; 145 | if (b > 200) b = 200; 146 | 147 | analogWrite(enB, a); 148 | analogWrite(enA, b); 149 | } 150 | --------------------------------------------------------------------------------