I had an idea. Basically the grey magnet is set to a random position. Using variables the grey magnet is able to detect the location of the stronger magnet (red and blue magnet)