First the servo motor start rotating from left to right and vice versa , at the same time, our arduino program start calculating the distance using ultrasonic sensor , then the arduino send the data that contain the angle and the distance of detected object as follows : "angle ; distance " , this semi-colon ";" help to split this data in python program so we can easily extract angle and distance value.

Our python program starts listening to the port where the arduino is connected , and then turn this data into coordinate using simple trigonometric equation , so we can draw it in the window , but before that we need to transform this coordinate to window coordinate ( Note : the origin of window in pygame is the top left corner ) because in our case the origin is the center of window , then we draw a set of circles with radius = 50*n ( you can change this value in python code ) .

Finally , we draw a green line where its angle with (x'x) axis is equal to servo angle , and start updating the screen , so it appear like scanning the window .

Note : In my arduino code , i decrease the rotating speed of servo using delay in for loop ,so that the data that python receive is the same of arduino in the same time (because when the speed is normal , python was too late ) . Download the source code from github .

Hope you enjoy it !!