# EasyC Help

Is there any way or command to keep your motor values between 0 and 255? In my code it is possible to do this and the only way I know to fix it is to have many many if commands. Any ideas would be helpful.

The motor’s won’t take a value larger than 255 or smaller than 0. I’m not sure what you’re asking for here…

When my motors get a value of over 255, through the algorithms I created for my holonomic bot, the motor begins to go in the opposite direction than it is supposed to. I want to know if there is any other way, than using lots of if commands to keep the values under 255.

Ok I see what you mean, what you need to do is have a test to see if the value is over 255. If it is then the value you send to the motor should be 255. So something like

if(x >255)
motoron(255)
else
motoron(x)

The way I’ve dealt with this is to create SetMotorSpeed(x) functions that take a signed integer as an argument. 0 means stopped, negative numbers mean reverse and positive numbers mean forward.

You put the “if” logic inside this routine so that positive values above 128 get limited to 128, and negative values below -127 get limited to -127. This way you put all the speed-limiting logic in one place and ignore it in your algorithms (as long as they keep the values between -32768 and 32767 it’ll work).

Taking it a step further, I write one routine for each motor that automatically corrects for left-vs-right so that positive values always mean “forward” and negative values always mean “reverse”. The routines look something like this:

``````void SetLeftMotorSpeed(int x)
{
if ( x < -127 )
x = -127 ;  // clamp negative values to -127
else
if ( c > 128 )
x = 128 ;  // clamp positive values to 128
x = x + 127 ; // convert range from -127..0..128 to 0..127..255
SetPWM ( LEFT_MOTOR , x ) ;

}

void SetRightMotorSpeed( int x )
{
if ( x < -127 )
x = -127 ;  // clamp negative values to -127
else
if ( c > 128 )
x = 128 ;  // clamp positive values to 128
x = 128 - x ; // convert range from -127..0..128 to 255..127..0
SetPWM ( LEFT_MOTOR , x ) ;

}``````

Hope that helps,

• Dean