0

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.

So if this is my array...

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

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.

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
  • Divide the difference between the two numbers by the number of times you're going to update the LED. Then add that difference in a loop to the first value each time you send. This is grade school arithmetic. – Barmar Oct 18 '16 at 07:58

2 Answers2

0

do you think about something like that?

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)
am2
  • 380
  • 5
  • 21
  • You need save every value if don't use ABS and WISE. Required compare every value for step_up or step_down. Did you see any variable for saving current value ? – dsgdfg Oct 18 '16 at 08:57
  • Need use `_clk` for communication or micro-controller response time, `_sensitvy` set timing for all processing work without error. – dsgdfg Oct 18 '16 at 09:04
  • @dsgdfg: why (ABS/WISE)? you split Delta into sign and value and the only place you use it is when you add the product to _from. if you wouldnt split it would be the same. – am2 Oct 18 '16 at 09:05
  • @dsgdfg: _clk/_sensitivity: you want to ensure, that there is a lag of at least 40 ms, before you tell the mc to Switch led. Did I understand this correctly? – am2 Oct 18 '16 at 09:09
  • LOL, you touch important point, `step` is `constant output_step_value` but `_clk` is `constant step_timer_value` for every step. – dsgdfg Oct 18 '16 at 09:12
  • @dsgdfg: Sorry, I do not understand your last comment. step is calculated from the positive differenz between _from and _to, why shouldn't it be negative? the only reason for wise is to preserve the sign. _clk is calculated as the time- pendant to step, always positive. Whats the reason for laughing? – am2 Oct 18 '16 at 09:20
  • Maybe all value of `_ramplist` not positive ! `Whats the reason for laughing ?` my flashback ! – dsgdfg Oct 18 '16 at 09:25
  • @dsgdfg: btw, you calculate your value outside the waiting Loop. If you miss one step, at least 1 value will not come in time. Maybe it would be better if you calculate inside the if- Statement of the Loop. so you could take even use the correct time for calculating. – am2 Oct 18 '16 at 09:26
  • @dsgdfg: in my op the function doesn't have to ensure that there are no negative Input values – am2 Oct 18 '16 at 09:29
  • Calculate nothing because `i try write a SCL function`, time is important for related question. `SCL` need 3 argument `start, end, time` but where step ? Step is `time/micro-controller scaning time`. – dsgdfg Oct 18 '16 at 09:31
  • @dsgdfg: And after lots of loops it will be deferred, cause you always restart the timer. Could be better to take a timer outside of the for- loop and to manage it with modulo. (my solution has the same problem of course.) – am2 Oct 18 '16 at 09:34
  • @dsgdfg: what is SCL? – am2 Oct 18 '16 at 09:36
  • SCL Meaning conditional mapping function(so SCALA). You need think all status on a processor if want write a great `ACCELERATION/DECELERATION`. You need bypass every step values if excepted a non-conditional DELAY. Make more exercise about of Scala+map, always raised more error/bug if you have more feedback points(also feedback use a float `clock`). *All computers in the world at the same time, only one process can start.* I mean, we're not real! Because we don't live in REAL TIME ! – dsgdfg Nov 23 '16 at 11:40
0

another version, similar to the version of dsgdfg (based on his/her idea), but without timing lag:

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 < 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)
am2
  • 380
  • 5
  • 21