This is a more advanced version of Mr. Wallplate’s Eye Illusion Robot https://www.hackster.io/tony3/mr-wallplate-s-eye-illusion-dd077f An ultrasonic sensor allows Mr. Wallplate’s head to track you as you walk in front of him.
The process may be summarized as follows. The sensor first turns counterclockwise (left) 60 degrees, and then turns right while scanning for an object closer than 3 feet. If it doesn’t detect anything before it reaches 60 degrees right, it repeats the turning left and then scanning until it does detect an object. Then the head turns to face it, the sensor turns left to the left limit (-60 degrees), and scans again to the right. This head-turning and scanning continues until the object moves back farther than 3 feet or goes too far left or right. A more detailed summary of the program logic is in step #6.
The project is described in detail on the following website: