Ball locate 

To locate the ball precisely, the X, Y coordinates for centroid (geometric
center) ar calculated, using formulas Cx = ∑CixAi / ∑Ai and
Cy = ∑CiyAi / ∑Ai, where Cx, Cy are X, Y coordinates and A is the
value of every pixel. As the background is predominantly black before this
step, Cx, Cy will be in the center of the ball. Then the whole picture is
moved so that the centroid is at coordinates 110,110, which is at the
center of the frame. The center is marked with 2x2 red pixels. Next, the averade diameter is measured, calculating the average pixel value for different diametres. The highest drop of average value (relative to the previous one) is considered as the ball diameter. Then, the background (every pixel which is outside of the diameter) is set to "white", or, more specifically, green (value 63). Those two steps takes 38 ms. This is only the small microcontroller, so it is the good speed for 220*220 matrix. One part of credit for that goes to assembly language, and the other one to lookup tables which are used instead of trigonometric calculations. 