Interpolate between elements in an array of floats
<p>I'm getting a list of 5 floats which I would like to use as values to send pwm to an LED. I want to ramp smoothly in a variable amount of milliseconds between the elements in the array.</p>

<p>So if this is my array...</p>

<pre><code>list = [1.222, 3.111, 0.456, 9.222, 22.333]
</code></pre>

<p>I want to ramp from 1.222 to 3.111 over say 3000 milliseconds, then from 3.111 to 0.456 over the same amount of time, and when it gets to the end of the list I want the 5th element of the list to ramp to the 1st element of the list and continue indefinitely.</p>

<p>Like this :</p>

<pre><code>import time

_ramplist = [1.222, 3.111, 0.456, 9.222, 22.333]

def set_pwm(_ramplist,_time,_sensitivy):
    for i in range(len(_ramplist)) :
        if i + 1 != len(_ramplist) :
            init_time = time.time()
            _from = _ramplist[i]
            _to   = _ramplist[i+1]
            step = float(abs(_from - _to )/_sensitivy)
            wise = -1 if _from &gt; _to else +1
            _clk =float( (_time/1000.0) /_sensitivy )
            count = 1
            while time.time() &lt; (init_time + float(_time/1000)) :
                if time.time() &gt; init_time + (count * _clk) :
                    count += 1
                    print (step*wise*count) + _from


set_pwm(_ramplist, 3000, 40)
</code></pre>

<p><strong>Missing points :</strong>
<code>step</code> is important because time object include infinity elements. This example is a <code>RTC</code>, so never see a <code>time.sleep()</code> function. <em>Check variable names if you want more information !</em></p>

python arrays linear-interpolation