The Limit_Mix function assumes that its input value has had 2000 added in, and thus subtracts it back out before returning.
Back in the Basic Stamp days, there were certain circumstances where it was necessary to avoid negative numbers. This Limit_Mix function is a legacy of those days. By rights, the function should now be:
Code:
unsigned char Limit_Mix (int intermediate_value)
{
static int limited_value;
if (intermediate_value < 0)
{
limited_value = 0;
}
else if (intermediate_value > 254)
{
limited_value = 254;
}
else
{
limited_value = intermediate_value;
}
return (unsigned char) (limited_value);
}
And the calls would be:
Code:
pwm01 = pwm03 = Limit_Mix(PWM_in1 + PWM_in2 - 127); /* LEFT WHEELS */
pwm02 = pwm04 = Limit_Mix(PWM_in2 - PWM_in1 + 127); /* RIGHT WHEELS */