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>another version, similar to the version of dsgdfg (based on his/her idea), but without timing lag:</p>

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

def play_LED(value):
    s = ''
    for i in range(int(value*4)):
        s += '*'           
    print s, value

def interpol(first, second, fract):
    return first + (second - first)*fract

def find_borders(list_of_values, total_time, time_per_step):
    len_list = len(list_of_values)
    total_steps = total_time // time_per_step
    fract = (total_time - total_steps * time_per_step) / float(time_per_step)
    index1 = int(total_steps % len_list)
    return [list_of_values[index1], list_of_values[(index1 + 1) % len_list], fract]

def start_program(list_of_values, time_per_step, relax_time):
    total_start = time.time()
    while True:
        last_time = time.time()
        while time.time() - last_time &lt; relax_time:
            pass
        x = find_borders(list_of_values,time.time(),time_per_step)
        play_LED(interpol(x[0],x[1],x[2]))

start_program(list_of_ramp,time_per_step=5,relax_time=0.5)
</code></pre>

python arrays linear-interpolation