Obstacle Avoiding Car Servo Question

Hi there, I am working on an obstacle avoiding car. I am modifying some code I found online. I have gotten the car working with the motor shield V2 that I have but I would like to program the servo that moves the ultrasonic sensor to constantly sweep back and forth so that obstacles can be detected more reliably. As it currently is, the servo only looks left to right when an obstacle is encountered.

I have tried to add a few new lines of code to get the servo to constantly look left to right but haven't been able to do so without breaking the rest of the code. I've just gone ahead and pasted all of the code below. So basically, I'd just like to know how I can add on to this so that the servo is constantly moving without causing the rest of the code to not work. I can provide more code and or information if needed. Thank you so much!

#include <Adafruit_MotorShield.h>
#include <NewPing.h>
#include <Servo.h> 

#define TRIG_PIN 9 
#define ECHO_PIN 8 
#define MAX_DISTANCE 200 
#define MAX_SPEED 100 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *Motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *Motor4 = AFMS.getMotor(4);
Servo myservo;   

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

  myservo.attach(10);  
  myservo.write(115); 
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  Serial.begin(9600);
  Serial.println("Riverbot DC Motor test!");

  //start the motor shield
  if (!AFMS.begin()) {
    Serial.println("Could not find Motor Shield. Check wiring");
    while (1);
  }
  Serial.println("Motor Shield found");
}

void loop() {
 int distanceR = 0;
 int distanceL =  0;
 delay(40);

 if(distance<=20)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);

  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
 }
 distance = readPing();
}

int lookRight()
{
    myservo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
}

int lookLeft()
{
    myservo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
    delay(100);
}

int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {
  Motor1->run(RELEASE); 
  Motor2->run(RELEASE);
  Motor3->run(RELEASE);
  Motor4->run(RELEASE);
  } 
  
void moveForward() {

 if(!goesForward)
  {
    goesForward=true;
    Motor1->run(FORWARD);      
    Motor2->run(FORWARD);
    Motor3->run(FORWARD); 
    Motor4->run(FORWARD);     
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
   {
    Motor1->setSpeed(speedSet);
    Motor2->setSpeed(speedSet);
    Motor3->setSpeed(speedSet);
    Motor4->setSpeed(speedSet);
    delay(5);
   }
  }
}

void moveBackward() {
    goesForward=false;
    Motor1->run(BACKWARD);      
    Motor2->run(BACKWARD);
    Motor3->run(BACKWARD);
    Motor4->run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    Motor1->setSpeed(speedSet);
    Motor2->setSpeed(speedSet);
    Motor3->setSpeed(speedSet);
    Motor4->setSpeed(speedSet);
    delay(5);
  }
}  

void turnRight() {
  Motor1->run(FORWARD);
  Motor2->run(FORWARD);
  Motor3->run(BACKWARD);
  Motor4->run(BACKWARD);     
  delay(500);
  Motor1->run(FORWARD);      
  Motor2->run(FORWARD);
  Motor3->run(FORWARD);
  Motor4->run(FORWARD);      
} 
 
void turnLeft() {
  Motor1->run(BACKWARD);     
  Motor2->run(BACKWARD);  
  Motor3->run(FORWARD);
  Motor4->run(FORWARD);   
  delay(500);
  Motor1->run(FORWARD);     
  Motor2->run(FORWARD);
  Motor3->run(FORWARD);
  Motor4->run(FORWARD);
}  

Welcome to the forum

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

If your REAL object is reliable detection, you can't move the sensor until after the echo has been received.

I see. The car does avoid obstacles currently but if the obstacle is at too much of an angle the sensor does not detect it and the car continues forward into the obstacle. Is there another way to mitigate this behavior?

Do you drive your car that way? See an obstacle, then look away to avoid it?

Untested with Arduino: Mount four sensors at 90° separation. Ping each in order.

Right now this code is just 'consequential'... if this then that... what you are talking about would require several things, one would be code to control the sensor and read the values, then those values have to be mapped to something that can be read and understood by some looping code in a way that allows it to navigate... I think what I am trying to say is that the modification you are talking about is many orders of magnitude more complex than this example.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.