Ultrasonic with servo motor help

Hello, i'm working on a robotics project where I have an HC-SR04 with buzzer (horn) attached to a servo motor where I want to trigger the device when it detects something within its proximity (led light will activate + the horn), as it moves 180 deg back and forth. So far with this code I can only get the servo motor to move 180 deg back and forth but does not trigger the ultrasonic sensor when I put my hand in front of the HC-SR04. I want to try to incorporate both devices to work. I tested both the servo and ultrasonic devices separately beforehand and they both work, here i'm trying to incorporate both devices to correlate together. Could there be a problem with how i'm structuring the program? I made some comments to show what's part of the ultrasonic sensor and servo.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {
  // put your setup code here, to run once:
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);

// pin for the servo motor
 myservo.attach(8);  // attaches the servo on pin 9 to the servo object


void loop() {
  // put your main code here, to run repeatedly:
// Clears the trigPin
digitalWrite(trigPin, LOW);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);

// servo postioning 
 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position

Your servo code is unconditional

Put the code for reading the rangefinder inside the for loops so that the rangefinder is read each time through the for loop (one movement of the servo). The angle of sensitivity of the rangefinder is about 30° so you don't need to read it every 1°. To read every 15° or 30°:]
for (pos = 0; pos <= 180; pos += 15) //or for (pos = 0; pos <= 180; pos += 30).

1 Like

thank you, if its not too troubling could you show an example for what you mean, I don't think i'm understanding too well

if (this) {
} else {
 doSomethingElse ();

Like that.