Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I am going to try to explain this as easily as possible. I am working on a computer program that takes input from a joystick and controls a servo direction and speed. I have the direction working just fine now I am working on speed. To control the speed of rotation on the servo I need to send it so many pulses per second using PWM. The servo that I am using takes arguments for speed between 120-150. 120 is %100 speed and 150 is %0 or stopped. 135 is %50 speed. How would I convert percentage from 0-100 into a number between 120-150 including 1/10ths? I hope this makes sense if you need me to explain further please let me know. I really don't know what tag this falls under either.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

It looks to be a linear relationship: in particular, if your percentage is $p$ (ranging from $0$ to $100$), then the number would be $150-0.3p$.

share|improve this answer
    
Your formula worked perfect thank you so much. I am curious though math has never been a strong point of mine how did you come up with 0.3? –  Yamaha32088 Apr 15 '13 at 23:33
    
Well, you want it to be a line. Two points on the line are (120,100) and (150,0). The general form for a line is $y = mx + b$. Plugging in these values gives you the values of $m$ and $b$. –  Henry Swanson Apr 15 '13 at 23:38
    
@Yamaha32088: Put another way, I noticed that if I started at $0$, the number was $150$. Increasing the percentage by $100$ caused a decrease in the number by $30,$ which would correspond to a slope of $\frac{-30}{100}=-0.3$ if the function were linear. To confirm it, I noted that if it were linear, then the number would decrease by $15$ (half of $30$) if we had instead increased the percentage by $50$ (half of $100$), which was indeed the case. Thus, it seemed that we started at $150$ and lost $0.3$ for every percent point we ticked up--in math-speak $150-0.3p$. –  Cameron Buie Apr 16 '13 at 0:29
add comment

$\text{Percentage speed}=100+\frac {100}{30}(120-\text{input})$

I expressed it that way to show the thought process. We need the $30$ step range of input to correspond to $100$ steps of output, thus the multiply by $\frac {100}{30}$. As the input rises the output falls, so we need a negative sign on the input. A little more thought gets us there. We can then do a little algebra to get

$\text{Percentage speed}=500-\frac {10}3 \text{input}$

To go the other way, $\text{input}=150-0.3 \text{Percentage speed}$

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.