Components and supplies
L293d motor driver shield
Geared motors
Black Tape
Line Sensor Module
Smart Robot Car Chassis Kit
jumper wires for arduino
12V Li-ion Battery
Arduino Uno R3
Apps and platforms
Arduino IDE 2.0 (beta)
Project description
Code
Arduino Line Follower Robot Code
cpp
Upload the code to your Arduino.
1/* 2Library used: Adafruit Motor Shield library V1 version: 1.0.1 3For this code to run as expected: 41.The centre to centre distance between the Line sensors should be 11 to 11.5 cm 52. The width of black tape should be 4.8 to 5 cm 63. The distance of the sensor LED from the flat ground surface should be 2 cm. 7*/ 8#include <AFMotor.h> 9// MACROS for Debug print, while calibrating set its value to 1 else keep it 0 10#define DEBUG_PRINT 0 11// MACROS for Analog Input 12#define LEFT_IR A0 13#define RIGHT_IR A1 14// MACROS to control the Robot 15#define DETECT_LIMIT 300 16#define FORWARD_SPEED 60 17#define TURN_SHARP_SPEED 150 18#define TURN_SLIGHT_SPEED 120 19#define DELAY_AFTER_TURN 140 20#define BEFORE_TURN_DELAY 10 21// BO Motor control related data here 22// Here motors are running using M3 and M4 of the shield and Left Motor is connected to M3 and Right Motor is connected to M4 using IC2 of the shield 23AF_DCMotor motorL(3); // Uses PWM0B pin of Arduino Pin 5 for Enable 24AF_DCMotor motorR(4); // Uses PWM0A pin of Arduino Pin 6 for Enable 25// variables to store the analog values 26int left_value; 27int right_value; 28// Set the last direction to Stop 29char lastDirection = 'S'; 30void setup() { 31#if DEBUG_PRINT 32 Serial.begin(9600); 33#endif 34 // Set the current speed of Left Motor to 0 35 motorL.setSpeed(0); 36 // turn on motor 37 motorL.run(RELEASE); 38 // Set the current speed of Right Motor to 0 39 motorR.setSpeed(0); 40 // turn off motor 41 motorR.run(RELEASE); 42 // To provide starting push to Robot these values are set 43 motorR.run(FORWARD); 44 motorL.run(FORWARD); 45 motorL.setSpeed(255); 46 motorR.setSpeed(255); 47 delay(40); // delay of 40 ms 48} 49void loop() { 50 left_value = analogRead(LEFT_IR); 51 right_value = analogRead(RIGHT_IR); 52#if DEBUG_PRINT 53 // This is for debugging. To check the analog inputs the DETECT_LIMIT MACRO value 300 is set by analysing the debug prints 54 Serial.print(left_value); 55 Serial.print(","); 56 Serial.print(right_value); 57 Serial.print(","); 58 Serial.print(lastDirection); 59 Serial.write(10); 60#endif 61 // Right Sensor detects black line and left does not detect 62 if (right_value >= DETECT_LIMIT && !(left_value >= DETECT_LIMIT)) { 63 turnRight(); 64 } 65 // Left Sensor detects black line and right does not detect 66 else if ((left_value >= DETECT_LIMIT) && !(right_value >= DETECT_LIMIT)) { 67 turnLeft(); 68 } 69 // both sensors doesn't detect black line 70 else if (!(left_value >= DETECT_LIMIT) && !(right_value >= DETECT_LIMIT)) { 71 moveForward(); 72 } 73 // both sensors detect black line 74 else if ((left_value >= DETECT_LIMIT) && (right_value >= DETECT_LIMIT)) { 75 stop(); 76 } 77} 78void moveForward() { 79 if (lastDirection != 'F') { 80 // To provide starting push to Robot when last direction was not forward 81 motorR.run(FORWARD); 82 motorL.run(FORWARD); 83 motorL.setSpeed(255); 84 motorR.setSpeed(255); 85 lastDirection = 'F'; 86 delay(20); 87 } else { 88 // If the last direction was forward 89 motorR.run(FORWARD); 90 motorL.run(FORWARD); 91 motorL.setSpeed(FORWARD_SPEED); 92 motorR.setSpeed(FORWARD_SPEED); 93 } 94} 95void stop() { 96 if (lastDirection != 'S') { 97 // When stop is detected move further one time to check if its actual stop or not, needed when the robot turns 98 motorR.run(FORWARD); 99 motorL.run(FORWARD); 100 motorL.setSpeed(255); 101 motorR.setSpeed(255); 102 lastDirection = 'S'; 103 delay(40); 104 } else { 105 // When stop is detected next time then stop the Robot 106 motorL.setSpeed(0); 107 motorR.setSpeed(0); 108 motorL.run(RELEASE); 109 motorR.run(RELEASE); 110 lastDirection = 'S'; 111 } 112} 113void turnRight(void) { 114 // If first time Right Turn is taken 115 if (lastDirection != 'R') { 116 lastDirection = 'R'; 117 // Stop the motor for some time 118 motorL.setSpeed(0); 119 motorR.setSpeed(0); 120 delay(BEFORE_TURN_DELAY); 121 // take Slight Right turn 122 motorL.run(FORWARD); 123 motorR.run(BACKWARD); 124 motorL.setSpeed(TURN_SLIGHT_SPEED); 125 motorR.setSpeed(TURN_SLIGHT_SPEED); 126 } else { 127 // take sharp Right turn 128 motorL.run(FORWARD); 129 motorR.run(BACKWARD); 130 motorL.setSpeed(TURN_SHARP_SPEED); 131 motorR.setSpeed(TURN_SHARP_SPEED); 132 } 133 delay(DELAY_AFTER_TURN); 134} 135void turnLeft() { 136 // If first time Left Turn is taken 137 if (lastDirection != 'L') { 138 lastDirection = 'L'; 139 // Stop the motor for some time 140 motorL.setSpeed(0); 141 motorR.setSpeed(0); 142 delay(BEFORE_TURN_DELAY); 143 // take slight Left turn 144 motorR.run(FORWARD); 145 motorL.run(BACKWARD); 146 motorL.setSpeed(TURN_SLIGHT_SPEED); 147 motorR.setSpeed(TURN_SLIGHT_SPEED); 148 } else { 149 // take sharp Left turn 150 motorR.run(FORWARD); 151 motorL.run(BACKWARD); 152 motorL.setSpeed(TURN_SHARP_SPEED); 153 motorR.setSpeed(TURN_SHARP_SPEED); 154 } 155 delay(DELAY_AFTER_TURN); 156}
Comments
Only logged in users can leave comments
jrachana
3 Followers
•2 Projects
0