Reading servo Motor last angle position

Good day.

I am planning to make a TRASH PICKER:

Concerns:

  1. I want my TRASH PICKER design looks like an excavator that can twist from 0 degree to 180 degrees using a SERVO MOTOR, attached with an Ultrasonic sensor

  2. if the Ultrasonic sensor cannot detect presence of an object, the Servo Motor continues to sweep back and forth (0-180deg, 180-0deg). If the the Ultrasonic sensor can detect object, the SERVO will stop at the last position.

  3. My problem is: I have given the code to sweep the servo, and stop the servo once the sensor detects the object

  4. But, What i want the servo will STOP at the last ANGLE where the Ultrasonic detects the object,

  5. I tried some code, detaching servo from the last angle, but the servo will go back to zero degree.

Any suggesstion please, thru code, how to stop the servo at its at position where the ultrasonic detects object. Thank you

Not really clear to me, maybe I miss something. If you have a code to sweep the servo and stop once the sensor detects the object, don't you already achieved the goal to stop the servo at the proper angle?

Can you post here the full code, and a better description of what it does together (with what you think it doesn't)?
And if possible, also a picture (or, better, a small video uploaded to youtube and linked here) could help much.

You have posted in a forum section which is clearly marked as DO NOT POST. This section exists to catch new forum members who do not read the forum guide before they post, which all new members should do

I will move your topic. In future please consider where you post more carefully and read the sticky post at the top of the forum section to confirm it is a suitable section for your question.

Exactly. You are telling the servo where to be, so you know where it is when you tell it to stop.

Just remember that angle. In the code, that means assign it to a variable.

Later when you want the servo to be at that position, send it to the recorded (remembered) value that sits in that variable.

Cheap servos operated in an open loop, which means they cannot tell you where they are pointing. But as you see, this is no problem as it is your code that make them go anywhere.

a7