View Single Post
  #1   Spotlight this post!  
Unread 03-03-2005, 05:07
yruhere yruhere is offline
Registered User
None #0769
 
Join Date: Mar 2003
Rookie Year: 2002
Location: _
Posts: 7
yruhere is an unknown quantity at this point
A strange occurence..

I'm a student at VCU and near the shipping deadline I went out to a school in the Richmond area that my friend mentors to help them with their programming issues. Apparently the compiler is as wonderful as last year.. and here's a solution for a problem they had...

The original code went something to the tune of:
Code:
if(calib) {
  offset01 = p1_y - 128;
  calib=0;
}
if(p1_y - offset01 > 254)
  pwm01 = 254;
else if(p1_y - offset01 < 0)
  pwm01 = 0;
else
  pwm01 = p1_y - offset01;
It would seem the compiler has issues with signed and unsigned variables and doing the math 'right' without you being very explicit. A bit of casting is all you need...

This should be the fix:
Code:
if(calib) {
  offset01 = p1_y - 128;
  calib=0;
}
if( ((signed int) p1_y) - offset01 > 254)
  pwm01 = 254;
else if( ((signed int) p1_y) - offset01 < 0)
  pwm01 = 0;
else
  pwm01 = ((signed int) p1_y) - offset01;