I have build up a line follower but a problem has risen up where the l298n dc motor driver is not driving the wheel.It is a single dual shaft tt motor attached to in3, in4 and enaB.
All other components are working including the IR sensor and servo to control the direction except the wheel is not rotating.
I would appreciate your comments and directions ...if any error is done.
#include <Servo.h>
#define leftIr 2
#define rightIr 3
#define MotorSpeed 8
#define MotorP 4
#define MotorN 5
int potValue;
#define leftultrasonicTrigger 11
#define leftultrasonicEcho 12
#define rightultrasonicTrigger 6
#define rightultrasonicEcho 7
Servo myservo;
// motors speed (0 - 255)
const float FORWARD_SPEED = 100;
void setup() {
Serial.begin(115200);
myservo.attach(13);
pinMode(leftIr, INPUT);
pinMode(rightIr, INPUT);
pinMode(MotorSpeed, OUTPUT);
pinMode(MotorP, OUTPUT);
pinMode(MotorN, OUTPUT);
pinMode(leftultrasonicTrigger, OUTPUT);
pinMode(leftultrasonicEcho, INPUT);
pinMode(rightultrasonicTrigger, OUTPUT);
pinMode(rightultrasonicEcho, INPUT);
}
void loop() {
long leftduration, leftdistance;
digitalWrite(leftultrasonicTrigger, HIGH);
delayMicroseconds(1000);
digitalWrite(leftultrasonicTrigger, LOW);
leftduration = pulseIn(leftultrasonicEcho, HIGH);
leftdistance = (leftduration / 2) / 29.1;
Serial.print(leftdistance);
Serial.println("CM");
delay(10);
potValue = leftdistance;
potValue = map(potValue, 0, 1023, 0, 180);
long rightduration, rightdistance;
digitalWrite(rightultrasonicTrigger, HIGH);
delayMicroseconds(1000);
digitalWrite(rightultrasonicTrigger, LOW);
rightduration = pulseIn(rightultrasonicEcho, HIGH);
rightdistance = (rightduration / 2) / 29.1;
Serial.print(rightdistance);
Serial.println("CM");
delay(10);
potValue = rightdistance;
potValue = map(potValue, 0, 1023, 0, 180);
drive();
moveForward();
moveLeft();
moveRight();
stopAircraft();
if ((leftdistance <= 50) && (rightdistance>=50))
{
stopAircraft();
myservo.write(132);
delay(5000);
myservo.write(87);
delay(10000);
myservo.write(42);
delay(5000);
myservo.write(87);
delay(5000);
drive();
Serial.println('leftdistance');
}
else if ((rightdistance <= 50) && (leftdistance>=50))
{
stopAircraft();
myservo.write(42);
delay(5000);
myservo.write(87);
delay(10000);
myservo.write(132);
delay(5000);
myservo.write(87);
delay(5000);
drive();
Serial.println('rightdistance');
}
if ((leftdistance<=50) && (rightdistance<= 50))
{
stopAircraft();
myservo.write(87);
}
}
void drive() {
int leftIrValue = digitalRead(leftIr) ;
int rightIrValue = digitalRead(rightIr) ;
if (leftIrValue && rightIrValue )
moveForward();
else if (leftIrValue && !(rightIrValue ))
moveLeft();
else if (!(leftIrValue) && rightIrValue )
moveRight();
else if (!(leftIrValue) && !(rightIrValue))
stopAircraft();
// Runs an action during just 125ms to make sure it doesn't cross the line
delay(125);
stopAircraft();
delay(75);
}
void moveForward() {
// PWM
analogWrite(MotorSpeed, FORWARD_SPEED);
// Direction
digitalWrite(MotorP, HIGH);
digitalWrite(MotorN, LOW);
myservo.write(87);
}
void moveLeft() {
// PWM
analogWrite(MotorSpeed,FORWARD_SPEED);
// Direction
digitalWrite(MotorP, HIGH);
digitalWrite(MotorN, LOW);
myservo.write(132);
}
void moveRight() {
// PWM
analogWrite(MotorSpeed, FORWARD_SPEED);
// Direction
digitalWrite(MotorP, HIGH);
digitalWrite(MotorN, LOW);
myservo.write(42);
}
void stopAircraft() {
// PWM
analogWrite(MotorSpeed, 0);
// Direction
digitalWrite(MotorP, LOW);
digitalWrite(MotorN, LOW);
myservo.write(87);
}