Stopping my servo sweep when ultrasonic sensor distance is met

Hi Guys

I am trying to make a robotic arm that receives distance information from the HC-SR04 ultrasonic sensor and then uses this information to stop a sweeping servo when the detected distance is
2 mm < distance < 20 mm.

I’ve attached an animation of the robot to give a better idea of what i’m trying to do. As I understand it I can only tell the servo to move to a specified position. I’m just curious what workaround is best to accomplish this task.

As of right now I do not have any code to go off of. Setting up the ultrasonic sensor to control the sweep of the base servo will be my first milestone in this project. I am very new at working with Arduino but I have used C a little bit and I’ve been picking it up pretty well so far.

Link to robot video:

Good start. You have some ideas.
Break it down and try some small steps.

  1. have you tried to hook up a servo and make it move with code?
  2. have you tried to hook up the sensor and print measurements to the console as you move your hand in front of the sensor?

If you have problems with those independent tasks, post a wiring diagram and your code and we can try to help.

If you are successful at both, then try a third project with both pieces of hardware attached and code that makes them interact.

That brings me back to the day:) I had to use that sensor when I was in college for a parallel parking robot.

You seem to be headed in the right direction. I do see one possible issue with your detection distance, but I would much rather have you try it and see what that issue is gonna be.

When I have people work under me take on whole projects, I have them outline what they think they will need to do, then make a plan on how they plan to accomplish it in steps. You have the first milestone, but in my opinion, you can break it down further (like can you get the transducer to make a response in your code).