Arduino Uno ultrasonic sensor on a servo

Hello,

I am trying to make an intruder-system. This system contains an ultrasonic sensor (HR-SR04) on top of a Servo (SG90) that continuously rotates. When the sensor detects an object within the range of 30 cm, a led turns on.
Both systems (sensor & servo) work great separate from each other. When i put them together, the servo rotates normal but the sensor only registers distance once every cycle.
I have tried solving this problem by using two different loops, but nothing seems to matter.
To sum up, i would like to have the servo and sensor operate at the same time.
Thank you in advance!

#include <NewPing.h>
#include <Servo.h>
#define trigPin 12
#define echoPin 13
#define MAX_DISTANCE 500
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
int led = 8;
int pos= 90;
int angle = 0;
Servo servo;

void loop() {
  sonarr();
  rotate();
}

void setup() {
  // put your setup code here, to run once:
Serial.begin (115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);  
pinMode(led, OUTPUT);
servo.attach(9);
servo.write(0);
}



void sonarr() {
  // put your main code here, to run repeatedly:
int duration, distance, pos = 0, i;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print(distance);
Serial.print(" cm ");
delay(1000);

if (distance >= 30) {
  digitalWrite(led, HIGH);
  delay(15);
}
else {
  digitalWrite(led, LOW);
  delay(15);

}
} 

void rotate () {
for (angle = 0; angle < 180; angle++) {
  servo.write(angle);
  delay(15);

}
for (angle = 180; angle > 0; angle--) {
servo.write(angle);
delay(15); }
}

You're sweeping the servo, but never calling sonarr() during the sweep

Also, pulseIn returns an unsigned long (uint32_t), not int.

You rotate the servo forward and then back again, which takes around 6 seconds. Then while the servo is at 0 you read the sensor. And then you move the servo again.

Surely what you need to do is read the sensor at various points WHILE the servo is sweeping? How often do you expect to do the read? Doing it every time the servo moves a single degree seems like overkill. Every 15 or 20 degrees might make more sense.

Steve

#include <Servo.h>
#define trigPin 12
#define echoPin 13


// 343 meters per second == 0.002915452 seconds per meter
const float MicrosecondsPerCM = 29.15452;
const float MicrosecondsPerRoundTripCM = MicrosecondsPerCM * 2;  // ~58.3


#define MAX_DISTANCE 500


int led = 8;
int pos = 90;
int angle = 0;
Servo servo;


void setup()
{
  // put your setup code here, to run once:
  Serial.begin (115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  servo.attach(9);
  servo.write(0);
}


void loop()
{
  for (angle = 0; angle < 180; angle++)
  {
    servo.write(angle);
    delay(15);
    sonarr();
  }


  for (angle = 180; angle > 0; angle--)
  {
    servo.write(angle);
    delay(15);
    sonarr();
  }
}


void sonarr()
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);


  unsigned long duration = pulseIn(echoPin, HIGH, MicrosecondsPerRoundTripCM * MAX_DISTANCE);
  unsigned distance = duration / MicrosecondsPerRoundTripCM;


  Serial.print(distance);
  Serial.println(" cm ");


  if (distance < 30 && distance != 0)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}