# how to limit pwm on easyc

how to you limit pwm values so a motor will never go full speed. i want my range to be 55-200. i have seen it done on mplab but i can’t figure it out on easyc

``````
if(pwm_value > 200)
pwm_value = 200;
if(pwm_value<55)
pwm_value = 55;
setPWM(port,pwm_value)

``````

The easiest way would be to set up an if statement where if the motor value > 200, set the motor to 200. You would take the value from the joystick (use an OI to PWM block) and instead of setting it to a PWM port, set it to a variable. Then, with an if statement, limit the values to whatever range you want. Finally, send that variable to a setpwm block with that variable set as the speed.

If you want to control the speed of a PWM from a joystick controller then just limiting the final value sent the PWM based on boundaries is a waste. Since 127 is neutral, and you want 200 to be your max, moving your joystick beyond the raw 200 position will do nothing further to the value. You might as well not even limit it.

Instead, try scaling it. Find the difference between your raw value and 127, and divide it by the difference between the the max raw value and 127, then multiply that by the difference between your “new” max and 127.

example: {n = int; d = double}

``````nRaw_Value = GetOIAInput (*yourport*, *youraxis*);
dFinal_Value_Intermediate = (nRaw_Value - 127) / (*maxraw* -127);
dFinal_Value_Intermediate = dFinal_Value_Intermediate * (*yourmax* - 127);
nFinal_Value = dFinal_Value_Intermediate + 127
SetPWM ( *yourPWMport*, nFinal_Value );
``````

With a joystick on port 1 and the y-axis the numbers would go like this:

``````nRaw_Value = GetOIAInput (1, 2);
dFinal_Value_Intermediate = (220 - 127) / (255 - 127);
= 93 / 128;
= 0.7265625;
nFinal_Value_Intermediate = 0.7265625 * (200 -127);
= 0.7265625 * 73;
= 53.0390625;
nFinal_Value = 53 + 127;
= 180;
SetPWM(1, 180);
``````

Of course, there are some casting issues with this example, but you get the idea. I use this technique a lot with my TI-84 Plus calculator graphics.

Implicit in the examples listed above is that you cannot use the tank or arcade motor control functions in easy c to accomplish what you want. You need to code the relationship between joystick and pwm yourself.

well what i want to limit the speed for is not the drivetrain but the arm it moves a bit too fast. So i just want a way to keep the value from passing 50-200.