The L298n motor works well when the program is no code with severo motor but the motor doesn't move while the severo motor code is in the program
#include <Ultrasonic.h>
#include <Servo.h> //
Servo myservo; // ???
//IRsencor
int DigitalPin =7;
int AnalogPin =A1;
//uira
const int trigPin = 5; //Trig Pin
const int echoPin = 6; //Echo Pin
long duration, cm, inches;
int BackwardSpeed = 50;
//Ultrasonic ultrasonic(8,9); // (Trig PIN,Echo PIN)//
//motor
int m1p1 =8;
int m1p2 =9;
int m2p1 =12;
int m2p2 =13;
const int pwm1 =10;
const int pwm2 =11;
void motorStop(){
digitalWrite( m1p1 , LOW);
digitalWrite( m1p2 , LOW);
digitalWrite( m2p1 , LOW);
digitalWrite( m2p2 , LOW);
analogWrite(10, 0);
analogWrite(11, 0);
}
void Backward(){
digitalWrite( m1p1 , LOW);
digitalWrite( m1p2 , HIGH);
digitalWrite( m2p1 , HIGH);
digitalWrite( m2p2 , LOW);
analogWrite(10, 200);
analogWrite(11, 200);
}
void forward(){
digitalWrite( m1p1 , HIGH);
digitalWrite( m1p2 , LOW);
digitalWrite( m2p1 , LOW);
digitalWrite( m2p2 , HIGH);
analogWrite(10, 150);
analogWrite(11, 200);
}
void Fforward(){
digitalWrite( m1p1 , HIGH);
digitalWrite( m1p2 , LOW);
digitalWrite( m2p1 , LOW);
digitalWrite( m2p2 , HIGH);
analogWrite(10, 200);
analogWrite(11, 250);
}
void left()
{
digitalWrite( m1p1 , HIGH);
digitalWrite( m1p2 , LOW);
digitalWrite( m2p1 , LOW);
digitalWrite( m2p2 , LOW);
analogWrite(10, 150);
analogWrite(11, 0);
}
void right()
{
digitalWrite( m1p1 , LOW);
digitalWrite( m1p2 , LOW);
digitalWrite( m2p1 , LOW);
digitalWrite( m2p2 , HIGH);
analogWrite(10, 0);
analogWrite(11, 150);
}
void setup() {
delay(500);
myservo.attach(3); //
pinMode(m1p1, OUTPUT);
pinMode(m1p2, OUTPUT);
pinMode(m2p1, OUTPUT);
pinMode(m2p2, OUTPUT);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(DigitalPin, INPUT); //
pinMode(AnalogPin, INPUT); //
pinMode(trigPin, OUTPUT); //
pinMode(echoPin, INPUT);
//
digitalWrite( m1p1 , HIGH);
digitalWrite( m2p2 , HIGH);
digitalWrite( m1p2 , LOW);
digitalWrite( m2p1 , LOW);
analogWrite(10, 170);
analogWrite(11, 200);
delay (4000);
digitalWrite( m1p1 , LOW);
digitalWrite( m1p2 , LOW);
digitalWrite( m2p1 , LOW);
digitalWrite( m2p2 , LOW);
analogWrite(10, 0);
analogWrite(11, 0);
myservo.write(180); //
delay (1000);
}
void loop() {
//IR
boolean D_val = digitalRead(DigitalPin); //
int A_val = analogRead(AnalogPin);
//if(A_val >400 )//
//ultra
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); //
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT); //
duration = pulseIn(echoPin, HIGH); //
cm = (duration/2) / 29.1; //
Serial.println(cm);
//
if (cm>30 && A_val>400)
{
forward ();
}
else if (10<cm<30 &&A_val>400)
{
Fforward ();
}
else if (cm<10 &&A_val>400)
{
//myservo.write(120);
Fforward ();
delay(2000);
}
else if (A_val<400 && cm>30)
{
motorStop();
delay (10);
Backward();
delay(1000);
left();
delay (2000);
forward();
delay (1000);
}
else if (A_val<400 && cm<30)
{
motorStop();
delay (10);
Backward();
delay(1000);
left();
delay (2000);
forward();
delay (1000);
}
else
{
motorStop();
}
}