Sonic sensor to servo help

Please understand i am new and may not understand technical answers.

Two sonic sensors at 30 degrees facing away from each other.
Right sensor detects object 4 servos move right
Left sensor detects object 4 servos move left
No object stay center
both detect, left two move right and right two move left

What happens in simulation: servos move right 90 degrees and stop. serial monitor says object detected from right sensor and repeats

Thanks


// Define pins for the ultrasonic sensors
const int rightSensorTrigPin = 2;
const int rightSensorEchoPin = 3;
const int leftSensorTrigPin = 4;
const int leftSensorEchoPin = 5;

// Define servo objects
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

// Define pins for the servo motors
const int servo1Pin = 6;
const int servo2Pin = 7;
const int servo3Pin = 8;
const int servo4Pin = 9;



// Declare center function
void centerServos() {
  // Set all servos to 45 degrees
  servo1.write(45);
  servo2.write(45);
  servo3.write(45);
  servo4.write(45);

  // Delay for 1 second to allow servos to reach position
  delay(1000);
}

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  while (!Serial) {}  // Wait for the serial connection to be established
  
  // Attach servo objects to the corresponding pins
  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);
  servo3.attach(servo3Pin);
  servo4.attach(servo4Pin);

  // Initialize ultrasonic sensors
  pinMode(rightSensorTrigPin, OUTPUT);
  pinMode(rightSensorEchoPin, INPUT);
  pinMode(leftSensorTrigPin, OUTPUT);
  pinMode(leftSensorEchoPin, INPUT);
}


  void loop() {
  // Read distance measurements from the ultrasonic sensors
  int rightDistance = readDistance(rightSensorTrigPin, rightSensorEchoPin);
  int leftDistance = readDistance(leftSensorTrigPin, leftSensorEchoPin);

  // Determine the direction to turn the servo motors based on the sensor readings
  if (rightDistance < 20 && leftDistance < 20) {
    Serial.println("Both sensors detected objects, turning right and left");
    turnRightAndLeft();
  } else if (rightDistance < 20) {
    Serial.println("Right sensor detected an object, turning right");
    turnRight();
  } else if (leftDistance < 20) {
    Serial.println("Left sensor detected an object, turning left");
    turnLeft();
  } else {
    Serial.println("No objects detected, centering");
    centerServos();
  }
}





int readDistance(int trigPin, int echoPin) {
  // Send a 10us pulse to the ultrasonic sensor to trigger a measurement
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the time it takes for the echo to return
  int duration = pulseIn(echoPin, HIGH);

  // Convert the duration to distance in centimeters
  int distance = duration / 58;

  return distance;
}

void turnRight() {
  servo1.write(90);
  servo2.write(90);
  servo3.write(90);
  servo4.write(90);
  delay(1000);
}

void turnLeft() {
  servo1.write(0);
  servo2.write(0);
  servo3.write(0);
  servo4.write(0);
  delay(1000);
}

void turnRightAndLeft() {
  servo1.write(0);
  servo2.write(0);
  servo3.write(90);
  servo4.write(90);
  delay(500);
  servo1.write(90);
  servo2.write(90);
  servo3.write(0);
  servo4.write(0);
  delay(500);
}

type or paste code here

When reading one ultrasonic sensor right after another, the second sensor will likely pick up echoes from the first. It should help to add a 30-millisecond delay between the two.

Hi, @MakerMidway
Welcome to the forum.

Do you have control of both of the detector signals to the UNO in your simulation.
If you don't then the simulation has failed, not your project.

You must be able to control the ultrasonic sensors in the simulation.
Read the manual for the sim and find out.

Otherwise build it in the real world, where you can control the ultrasonic signals.

What simulator are you using?

Thanks... Tom.. :smiley: :+1: :coffee: :australia:

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