1

so the scenerio is i am making a bluetooth controlled car(voice control)+obstacle avoidence 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 aslo 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 plz guyz guide me where is the lacking if i have done something wrong with the code or else my motor driver or wiring is creating te issue

CODE : ''' #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); }'''

1
  • start at the begining ... write a sketch that runs one motor in one direction for one second, stops the motor for one second, then repeats
    – jsotola
    Oct 3 '20 at 16:57
1

The code seems to have a few missing lines for logic. Here's a corrected version from my understanding.

#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") {
      right();
    } else if (voice == "right") {
      right();
    } else if(voice == "move forward"){
      forward();
    } else if(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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.