Problems with NullZone

Hey Guys,

we have a problem with a scale function that we wrote in order to have a rectangular NullZone where the joystick is deactivated. Once the Joystick get s out of the Zone (for example p1_x > 147) then we want to scale the new value over the full range of the output (here from 147 - 255 TO 127 - 255) in order to not start with like 20 speed when we get out of the zone.

The function that we came up with works in calculators and other c-Compilers but not with the RC so I hope somebody could help us with this:

Here is the Function:
unsigned char scale ( unsigned char sNum , // Value to Scale
unsigned char sMin , // Old Min Value
unsigned char sMax , // Old Max Value
unsigned char eMin , // New Min Value
unsigned char eMax) // New Max Value
{
unsigned char eNum = (( sNum - sMin ) * ( eMax - eMin ) / ( sMax - sMin ) + eMin);
return eNum;
}

And here where we call it (N_ZONE_X = 40):
(int)scale((unsigned char)p1_x, 127 + (N_ZONE_X / 2), 254, 127, 254);

the value that we get is always either 127 or 254 but never stuff in between so we’re kinda confused : ( …

Thanks,

bye

Felix

In your code you may not be typecasting enough, when you are subtracting the two unsigned characters there may be some problems in that code, try this:

unsigned char eNum = (unsigned char) ((( (signed int)sNum - (signed int)sMin ) * ( (signed int)eMax - (signed int)eMin )) / ( (signed int)sMax - (signed int)sMin ) + (signed int)eMin);

Ok I’ll try that tomorrow, thanks for your help … C Typecasting makes me just crazy ^^, I’ll be so happy when I’ll finish our Teams Homepage in PHP after Shipping Date you can’t imagine : ) …