user15144: I am working on an Arduino project for school to power a 12V DC cooling fan.
I want to adjust the speed of the fan. The instructions say to set the motor pin at an equation of (0 to 9) * 10 + 150 to get a range of 150 to 240.
I need to write a mathematical equation but can not figure out what 150 to 240 means.  Is this voltage, resistance...?  Thanks for the help.

user15155: The map() function is the answer. It "rescales" the range of the data, using this formula:
(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

You input these five values:
map(value, fromLow, fromHigh, toLow, toHigh)


value is the X value you want to resize
fromLow and fromHigh is the range that X is currently in
toLow and toHigh is the range that you want to scale X to


user368: They're the value to pass to analogWrite() in order to generate a PWM waveform with an appropriate duty cycle.
But don't bother writing your own algorithm, just use map().

user4143: The analogWrite() function takes a value that is between 0 (fully off) and 255 (fully on).
Your value of 150 to 240 is a value within that range of 0 to 255 and represents a percentage of the "on time" of the PWM signal.

(150 / 255) * 100 = 58.8% on time
(240 / 255) * 100 = 94.1% on time.

You seem to have an "input value" of between 0 and 9 inclusive.  Let's call this value "i".  You want an output value, let's call it "o".
The instructions say "0 to 9 times 10 plus 150 to get a range of 150 to 240". That is simple enough to convert to a formula.  0-9 is i, remember:

o = i × 10 + 150

If i is 0 then o = 0 × 10 + 150 = 0 + 150 = 150.
If i is 9 then o = 9 × 10 + 150 = 90 + 150 = 240.
As Ignacio mentions, the Arduino API has a map function which is meant to be used for this kind of thing;
o = map(i, 0, 9, 150, 240);

However: the map() function is quite heavyweight since it is designed to be a generic mapping function for scaling values. Doi g the calculation manually will always be far more efficient since map will use steps and calculations that are not really part of your needed result. It's a bit like the differende between 1 + 2 and 1+ 5 - 3 + 2 - 4 + 2. The result is the same, but one is far simpler to calculate than the other.

15144: <p>I am working on an Arduino project for school to power a 12V DC cooling fan.</p>

<p>I want to adjust the speed of the fan. The instructions say to set the motor pin at an equation of <code>(0 to 9) * 10 + 150</code> to get a range of 150 to 240.</p>

<p>I need to write a mathematical equation but can not figure out what 150 to 240 means.  Is this voltage, resistance...?  Thanks for the help.</p>
 15155: <p>The <a href="http://labs.arduino.org/map" rel="nofollow">map()</a> function is the answer. It "rescales" the range of the data, using this formula:</p>

<pre><code>(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
</code></pre>

<p>You input these five values:</p>

<pre><code>map(value, fromLow, fromHigh, toLow, toHigh)
</code></pre>

<ul>
<li><code>value</code> is the X value you want to resize</li>
<li><code>fromLow</code> and <code>fromHigh</code> is the range that X is currently in</li>
<li><code>toLow</code> and <code>toHigh</code> is the range that you want to scale X to</li>
</ul>
 368: <p>They're the value to pass to <code>analogWrite()</code> in order to generate a PWM waveform with an appropriate duty cycle.</p>

<p>But don't bother writing your own algorithm, just use <a href="https://www.arduino.cc/en/Reference/Map" rel="nofollow"><code>map()</code></a>.</p>
 4143: <p>The <code>analogWrite()</code> function takes a value that is between 0 (fully off) and 255 (fully on).</p>

<p>Your value of 150 to 240 is a value within that range of 0 to 255 and represents a percentage of the "on time" of the PWM signal.</p>

<ul>
<li>(150 / 255) * 100 = 58.8% on time</li>
<li>(240 / 255) * 100 = 94.1% on time.</li>
</ul>

<p>You seem to have an "input value" of between 0 and 9 inclusive.  Let's call this value "<em>i</em>".  You want an output value, let's call it "<em>o</em>".</p>

<p>The instructions say "0 to 9 times 10 plus 150 to get a range of 150 to 240". That is simple enough to convert to a formula.  0-9 is <em>i</em>, remember:</p>

<ul>
<li><em>o = i × 10 + 150</em></li>
</ul>

<p>If <em>i</em> is 0 then <em>o</em> = <em>0 × 10 + 150</em> = <em>0 + 150</em> = <em>150</em>.</p>

<p>If <em>i</em> is 9 then <em>o</em> = <em>9 × 10 + 150</em> = <em>90 + 150</em> = <em>240</em>.</p>

<p>As Ignacio mentions, the Arduino API has a <code>map</code> function which is meant to be used for this kind of thing;</p>

<pre><code>o = map(i, 0, 9, 150, 240);
</code></pre>

<p><em>However:</em> the <code>map()</code> function is quite heavyweight since it is designed to be a generic mapping function for scaling values. Doi g the calculation manually will always be far more efficient since map will use steps and calculations that are not really part of your needed result. It's a bit like the differende between 1 + 2 and 1+ 5 - 3 + 2 - 4 + 2. The result is the same, but one is far simpler to calculate than the other.</p>
