Servo and DC Motor are not working at the same time Arduino UNO

I'm making a robot which works on voice commands so what happens here is whenever the servo.attach() method gets called the right side of the motor stops working i tried servo.detach() but it's not working I also went through many forum where i came to know that PWM pin 9 & 10 wont work so i didn't used them then too I'm going through the same problem .I'm using L298N motor driver for the DC Motors plz suggest me with the solution.

#include <SoftwareSerial.h>
#include<Servo.h>

SoftwareSerial BT(0, 1);

Servo servo;
int pos;

String readvoice;
int In1=4;
int In2=5;
int In3=6;
int In4=7;
int ENA1=2;
int ENA2=3;
int SPEED=255;


void setup() 
{
  
  
  BT.begin(9600);
  Serial.begin(9600);
  
  pinMode(In1,OUTPUT);
  pinMode(In2,OUTPUT);
  pinMode(In3,OUTPUT);
  pinMode(In4,OUTPUT);
  pinMode(ENA1,OUTPUT);
  pinMode(ENA2,OUTPUT);
}
void loop()
{
  
  analogWrite(ENA1,SPEED);
 analogWrite(ENA2,SPEED);
  while (BT.available())
  {  
  delay(10); //Delay added to make thing stable  
  char c = BT.read(); //Conduct a serial read
  readvoice += c; //build the string- "forward", "reverse", "left" and "right"
  }
  
  if (readvoice.length() > 0) 
  {
    Serial.println(readvoice);
    if(readvoice == "*go#")
    {
      digitalWrite(In1,HIGH);
      digitalWrite(In2,LOW);
      digitalWrite(In3,HIGH);
      digitalWrite(In4,LOW);
      delay(100);
    }
  
  else if (readvoice == "*left#")
  {
    serveleft();
    
    delay(1000);
    digitalWrite (In1, LOW);
    digitalWrite (In2, HIGH);
    digitalWrite (In3, HIGH);
    digitalWrite (In4, LOW);
    delay (1100);
    digitalWrite (In2, LOW);
    digitalWrite (In3, LOW);
    delay (100);
    
    }
  
  else if (readvoice == "*right#")
  {
    serveright();
    delay(1000);
    digitalWrite (In1, HIGH);
    digitalWrite (In2, LOW);
    digitalWrite (In3, LOW);
    digitalWrite (In4, HIGH);
    delay (1100);
    digitalWrite (In1, LOW);
    digitalWrite (In4, LOW);
    delay (100);
   
  }
  else if (readvoice == "*stop#")
  {
    
    digitalWrite (In1, LOW);
    digitalWrite (In2, LOW);
    digitalWrite (In3, LOW);
    digitalWrite (In4, LOW);
    delay (100);
  }
    readvoice="";
  }
}
void serveleft()
{
  servo.attach(A0);
  servo.write(190);
  delay(1000);
  servo.write(90);
    delay(1000);
    servo.detach();
}
void serveright()
{
  servo.attach(A0);
  servo.write(0);
  delay(1000);
  servo.write(90);
    delay(1000);
    servo.detach();
}
SoftwareSerial BT(0, 1);

?

Servo.h and SoftwareSerial.h do not work well together. They both use the same timer. You could try using ServerTimer2.h instead of Servo.h but read the documentation...the commands for the two are not quite the same.

And you shouldn't be using SoftwareSerial on the hardware serial pins 1 and 0 anyway and you definitely can't use both hardware Serial.begin() and software serial BT.begin() when they both use the same pins.

Steve

Well that Software serial is for the Bluetooth HC-05 which has to be connected on the Tx and Rx pin theres no issue in it but ill just try changing to timer2

that Software serial is for the Bluetooth HC-05 which has to be connected on the Tx and Rx pin

They are the very pins that should not be used for SoftwareSerial because they are used by the hardware serial interface

So where to connect tx and Rx pin ???

SaurabhKawli:
So where to connect tx and Rx pin ???

You can use any pair of pins you like for SoftwareSerial Rx and Tx. The purpose is to create a second serial interface in addition to HardwareSerial on pins 0 and 1

...R

SaurabhKawli:
So where to connect tx and Rx pin ???

Any other 2 pins that are not being used by something else

No problem , Thank you everyone i solved the error what I did was i just changed the servo pin from Analog A0 to PWM pin 10 and it worked. and i removed the enable pins. Ty for helping everyone :slight_smile:

So do you still have SoftSerial on pins 0 and 1 ?