please read this thread from 2011 - http://forum.arduino.cc/index.php?topic=52583.0 - it discusses the math and an alternative solution. If there are questions thereafter, post them as explicit as you can.