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>do you think about something like that?</p>

<pre><code>import time
l = [1.222, 3.111, 0.456, 9.222, 22.333]

def play_led(value):
    #here should be the led- code
    print value

def calc_ramp(given_list, interval_count):
    new_list = []
    len_list = len(given_list)
    for i in range(len_list):
        first = given_list[i]
        second = given_list[(i+1) % len_list]
        delta = (second - first) / interval_count
        for j in range(interval_count):
            new_list.append(first + j * delta)
    return new_list

def endless_play_led(ramp_list,count):
    endless = count == 0
    count = abs(count)

    while endless or count!=0:
        for i in range(len(ramp_list)):
            play_led(ramp_list[i])
            #time.sleep(1)
        if not endless:
            count -= 1
        print '##############',count


endless_play_led(calc_ramp(l, 3),2)
endless_play_led(calc_ramp(l, 3),-2)
endless_play_led(calc_ramp(l, 3),0)
</code></pre>

python arrays linear-interpolation