Odd math issues

Our current robot has the following function in it which adjusts the desired orientation of one of our joints based upon the joystick:

void Waist(void) {
    long delta;
    if(p1_wheel>147) {
    if(p1_wheel<107) {
    waist_theta-= delta;


All the variables in this code are declared as longs, except for p1_wheel (obviously). waist_theta is only read in other functions, not written.

Despite this, somehow the value of waist_theta is sporadically jumping instantly to a large negative number. Any ideas? I’m suspecting some unusual type conversion issue.

I would tend to guess your problem is in an interrupt routine. Are you saving all the appropriate sections?

Read this post for more details: http://www.chiefdelphi.com/forums/showthread.php?t=44046

We’ll check that, but I’m pretty sure we’re already doing that.

We simplified the routine and it seems to be working now, however. I’ll dig into this more when we get back from the CT scrimmage.