I know this is probably a very asked question, but I am building a fairly basic radar type thing which sweeps left and right and uses an ultrasonic sensor to determine the distance to an object and a buzzer is set off if something is detected.

The device itself works but I've noticed that because I have used delay() to allow the servo to complete its full movements, it is holding up the rest of the code, and as a result the buzzer and returning of the distance is extremely unresponsive and sluggish.

So far I have looked at simply returning a value whenever the motor reaches a certain step but it doesn't achieve exactly what I'm looking for, and I have looked into using millis() for timing asynchronously, but I'm struggling to wrap my head around it a bit in terms of how it will work for a servo.

My current working but sluggish code:

//Servo Stuff
#include <Servo.h>
int servoPin = 12; // Pin for interacting with servo defined
int servoStart = 0; //Starting position of servo motor
int servoEnd = 90; //End position of servo means it rotates 90 degrees
int waitTime = 1000; // Allows servo to fully rotate before moving again
int servoPos = 20; // For incrementing the position of the servo
int servoStep = 20; //amount to increment servo position by
Servo myServo; // Creates an instance of a servo object

//Ultrasonic stuff
int trigPin = 9;
int echoPin = 10;
long duration;
int distance;
int maxDistance = 329; // The max range of the ultrasonic sensor

//Buzzer stuff
int buzzerPin = 11;
int buzzerWait = 200;

void setup(){

//Attach the servo object to the pin and begin serial monitor with
// polling rate of 9600
  pinMode(buzzerPin, OUTPUT);

    pinMode(trigPin, OUTPUT); // Trig pin set as output
    pinMode(echoPin, INPUT); // echo pin set as i
    void loop()
      // Moving servo 
      myServo.write(servoStart); //Moves servo to the starting pos
      //delay (waitTime); //allows servo to fully move before next move
      //myServo.write(servoEnd); //Moves to end position
      //delay (waitTime); // waits before reverting back to start pos
     // Using ultrasonic sensor
      // Clears the trig pin
      digitalWrite(trigPin, LOW);
    // Sets the trigPin on HIGH state for 10 microseconds
      digitalWrite(trigPin, HIGH);
      digitalWrite(trigPin, LOW);
    // reads echoPin and returns the travel time of the wave in microseconds
      duration = pulseIn(echoPin, HIGH);
    // Calculating distance
      distance = duration*0.0343/2;
    //Prints the distance to the Serial Monitor
      Serial.print("Distance: ");
      if (distance < maxDistance) {
      digitalWrite(buzzerPin, HIGH);
      }else digitalWrite(buzzerPin, LOW);

Any pointers to some good resources for this would be very appreciated, thanks.


From your code it seems like you are doing all the servo movement before you read the distance and report back. A lot of code is commented out, so it's a bit difficult to follow. What I would do is something along the line of:

int servoStart=0;
int servoEnd=90;
int servoPos=0;

void setup() {
    [usual setup stuff like you do it now]
    myServo.write(servoPos);  //start at pos 0
    // Maybe a delay here to make sure we are at pos. 0

void loop() {
    //Switch direction if we reach end
    if (servoPos+servoStep < servoStart || servoPos+servoStep > servoEnd)
        servoStep = -servoStep;    
    servoPos += servoStep;
    //Small delay?
    //Read distance
    //Send distance to seraial
    //Buzz buzzer if needed
  • Thank you for this, works perfectly!
    – SourOddity
    Mar 22 at 17:07

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.