2
2
20strannik082018-08-20 15:44:07
Python
20strannik08, 2018-08-20 15:44:07

How to execute loops in parallel in python(2.7)?

I'm trying to learn python. I broke my whole head ... I have python 2.7 on the raspberry pi 3, I'm trying to run two stepper motors at the same time.
There is such a design:

step_timeout = 0.0000000001
impulse_timeout =0.00075

for i in range(0,850):
    GPIO.output(19, GPIO.HIGH)
    time.sleep(impulse_timeout)
    GPIO.output(19, GPIO.LOW)
    time.sleep(step_timeout)

    GPIO.output(13, GPIO.HIGH)
    time.sleep(impulse_timeout)
    GPIO.output(13, GPIO.LOW)
    time.sleep(step_timeout)

    GPIO.output(6, GPIO.HIGH)
    time.sleep(impulse_timeout)
    GPIO.output(6, GPIO.LOW)
    time.sleep(step_timeout)

    GPIO.output(5, GPIO.HIGH)
    time.sleep(impulse_timeout)
    GPIO.output(5, GPIO.LOW)
    time.sleep(step_timeout)

And then there is this design:
step_timeout = 0.002
impulse_timeout =0.001

for i in range(0,800):
    GPIO.output(12, GPIO.HIGH)
    time.sleep(impulse_timeout)
    GPIO.output(12, GPIO.LOW)
    time.sleep(step_timeout)

    GPIO.output(16, GPIO.HIGH)
    time.sleep(impulse_timeout)
    GPIO.output(16, GPIO.LOW)
    time.sleep(step_timeout)

    GPIO.output(20, GPIO.HIGH)
    time.sleep(impulse_timeout)
    GPIO.output(20, GPIO.LOW)
    time.sleep(step_timeout)

    GPIO.output(21, GPIO.HIGH)
    time.sleep(impulse_timeout)
    GPIO.output(21, GPIO.LOW)
    time.sleep(step_timeout)

How can I make these two loops run at the same time?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2018-08-20
@vintello

you need to write asynchronous tasks in asyncio

A
Alexander, 2018-12-19
@Fox_Alex

Make one quick cycle that will decide who and when to twitch. Forget about sleep altogether and instead make loop iteration counters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question