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();
}