so the scenario is i am making a bluetooth controlled car(voice control)+obstacle avoidance function.I am using Arduino UNO with L298n motor driver and a HC-05 BT module in the sensors i am using the ultrasonic sensor and a IR sensor for the back and a servo motor is also used to rotate the ultrasonic sensor.so the problem is when i am sending command "turn left" the servo moves left and nothings happens and the other command i.e turn right,move backward,move forward are not working at all.
circuit diagram and code is given if i have done something wrong with the code or else my motor driver or wiring is creating the issue
#include <NewPing.h>
#include<Servo.h>
#define TRIGGER_PIN A1
#define ECHO_PIN A0
#define MAX_DISTANCE 300
#define IR A2
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;
String voice;
void setup() {
Serial.begin(9600);
myservo.attach(8);
myservo.write(90);
pinMode(IR, INPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode( 9, OUTPUT);
}
void loop() {
int distance = sonar.ping_cm();
if (Serial.available() > 0) {
voice = "";
delay(2);
voice = Serial.readString();
delay(2);
Serial.println(voice);
if (voice == "turn left") {
left();
} else if (voice == "left") {
left();
} else if (voice == "turn right") {
} else if (voice == "right") {
}
}
while (voice == "move forward") {
forward();
}
while (voice == "move backward") {
backward();
}
}
void forward() {
int distance = sonar.ping_cm();
if (distance < 10) {
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite( 9, LOW);
voice = "";
} else {
digitalWrite(12, HIGH);
digitalWrite(10, HIGH);
}
}
void backward() {
int IR_Sensor = digitalRead(IR);
if (IR_Sensor == 0) {
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite( 9, LOW);
voice = "";
} else {
digitalWrite(11, HIGH);
digitalWrite( 9, HIGH);
}
}
void left() {
myservo.write(180);
delay(500);
myservo.write(90);
delay(500);
int distance = sonar.ping_cm();
if (distance < 10) {
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite( 9, LOW);
voice = "";
} else {
digitalWrite(12, HIGH);
delay(700);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite( 9, LOW);
}
}
void right() {
myservo.write(0);
delay(500);
myservo.write(90);
delay(500); {
int distance = sonar.ping_cm();
if (distance < 10) {
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite( 9, LOW);
voice = "";
} else {
digitalWrite(10, HIGH);
}
delay(700);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite( 9, LOW);
}
}
void Stop() {
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite( 9, LOW);
}