Quick update: I need to play some wit the calculate speed function:
I updated the function to also output secsPerDeg to help debug... This except should give a few clues:
void calculate_speed( int p_position, int p_interval )
if ( p_position > lastMotorPos )
degreesTraveled = p_position - lastMotorPos;
degreesTraveled = p_position - lastMotorPos + 360;
lastMotorPos = p_position;
secsPerDeg = ( p_interval / 1000.0/*milliseconds*/ ) / degreesTraveled;
secondsPerDegree = ( 0.9 * secondsPerDegree ) + ( 0.1 * secsPerDeg );
Darn new Arduino when I unplug USB cable it still shows the data, but it is grayed out which is fine, but you can not copy to clipboard... So will describe:
Suppose you call twice with p_position = 0 degrees.
In the code the line if ( p_position > lastMotorPos ) will be false as they are equal, so it will calculate that:
we went: 360 degrees (0-0+360)
Where the calculation went off was Call with 360 degrees followed by 0 degrees.
It will calculate: degreesTraveled = (0 - 360) + 360 or 0 degrees.
At which point secsPerDegree we will get a divide by zero.
My guess is that 360 should have been 0...