user3391890
user3391890

Reputation: 13

Increasing and decreasing with different steps loop

I want to do some simple loop which will increasing and decreasing different values with different step size until some time expired.

For example:

(1) First variable in range from 10 to 20 with step 1

(2) Second variable in range from 20 to 30 with step 2

(3) Third variable in range from 30 to 40 with step 3

(4) Fourth variable in range from 10 to 50 with step 5

(1) 10 11 ... 20 19 ... 10 ...

(2) 20 22 ... 30 28 ... 20 ...

(3) 30 33 ... 39 36 ... 30 ...

(4) 10 15 ... 50 45 ... 10 ...

The problem is in elif line but I have no idea how to rebuild this code. Can anyone help me?

My code:

for i in range(4):
    aHO.setBlock((i + 1), startValue[i])

while time.time() < end:
    for i in range (4):
        if actualValue[i] <= stopValue[i] - stepSize[i]:
            actualValue[i] = actualValue[i] + stepSize[i]
            aHO.setBlock((i + 1), actualValue[i])
        elif actualValue[i] > stopValue[i] - stepSize[i]:
            actualValue[i] = actualValue[i] - stepSize[i]
            aHO.setBlock((i + 1), actualValue[i])

time.sleep(float(timeInterval) / 1000)

Everything is going right until values are approaching to the stopvalue then values are decreasing but only once and then increasing also only once and so on. Eg.

1 2 3 ... 9 10 9 10 9 10 ...

Upvotes: 0

Views: 2028

Answers (4)

schacki
schacki

Reputation: 9533

Based on warwaruk's approach this should do the job:

from itertools import chain
def get_next(start, stop, step)
    while True:
        for j in list(chain(xrange(start, stop, step), xrange(stop, start-1, -step))):
            yield j

while time.time() < end:
    for actualValues in zip(
        get_next(10,20,1),
        get_next(20,30,2),
        get_next(30,40,3),
        get_next(10,50,5)
    ):
    for i,actualValue in enumerate(values):
        aHO.setBlock((i + 1), actualValue)

Upvotes: 0

warvariuc
warvariuc

Reputation: 59664

You can use this to generate needed values:

>>> from itertools import chain
>>> list(chain(xrange(10, 20, 1), xrange(20, 9, -1)))
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
>>> list(chain(xrange(20, 30, 2), xrange(30, 19, -2)))
[20, 22, 24, 26, 28, 30, 28, 26, 24, 22, 20]
>>> list(chain(xrange(30, 40, 3), xrange(39, 29, -3)))
[30, 33, 36, 39, 39, 36, 33, 30]
>>> list(chain(xrange(10, 50, 5), xrange(50, 9, -5)))
[10, 15, 20, 25, 30, 35, 40, 45, 50, 45, 40, 35, 30, 25, 20, 15, 10]
>>> 

Upvotes: 0

Corley Brigman
Corley Brigman

Reputation: 12401

You have it set to increment if it's less than StopValue (- one step size), and decrement if it's more than StopValue (+ one step size). This tends to keep it right at StopValue, as you have strong negative feedback from wandering away.

You need to change your algorithm to 'skid' - this is just a sketch of the algorithm, but you want to basically set your positive & negative limits, and add a current direction. As long as you are in the range you want i.e. -n*step_size <= offset <= n*step_size, where n is how many steps you allow from the center, you don't modify your direction. You don't modify your direction until you walk outside this range.

Upvotes: 0

Tom Ron
Tom Ron

Reputation: 6181

Try using range in the following syntax -

range(start, end, step_size)

Upvotes: 2

Related Questions