thomca96
Posts: 9
Joined: Mon Feb 04, 2019 11:11 am

delay for python

Wed Feb 06, 2019 8:34 am

Hi,
I hope someone can help me with following problem. I nedd to control a motor using a raspberry pi zero as shown in following code. Is there an alternative possibility to the sleep comand for a dealy? It would be greate if there would be a possibility to implement a delay which is not freezing the controller?

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
from time import sleep
motor = 12
a=7
b=16
GPIO.setwarnings(False)
GPIO.setup(motor,GPIO.OUT)
GPIO.setup(b,GPIO.OUT)
GPIO.setup(a,GPIO.OUT)
pi_pwm = GPIO.PWM(motor,500)
pi_pwm.start(0)

while True:
for i in range (0,7):
pi_pwm.start(80)
GPIO.output(b,0)
GPIO.output(a,1)
sleep(0.15)
pi_pwm.start(80)
GPIO.output(b,1)
GPIO.output(a,0)
sleep(0.15)
pi_pwm.start(0)
sleep(1.5)

gordon77
Posts: 5075
Joined: Sun Aug 05, 2012 3:12 pm

Re: delay for python

Wed Feb 06, 2019 10:17 am

Code: Select all

start = time.time()
while time.time() - start < 0.15:
     pass

hippy
Posts: 7908
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: delay for python

Wed Feb 06, 2019 10:21 am

An alternative is to move your PWM controlling code into its own thread.

thomca96
Posts: 9
Joined: Mon Feb 04, 2019 11:11 am

Re: delay for python

Wed Feb 06, 2019 1:16 pm

how could i implement it that the PWM code is running in his own threat?
Sorry but I am not the best programmer...

hippy
Posts: 7908
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: delay for python

Wed Feb 06, 2019 2:23 pm

thomca96 wrote:
Wed Feb 06, 2019 1:16 pm
how could i implement it that the PWM code is running in his own threat?
Sorry but I am not the best programmer...
Untested but something like this should be a good starting point. You can do other things in the final 'while True:' loop other than just sleep.

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
from time import sleep
motor = 12	
a=7
b=16	
GPIO.setwarnings(False)			
GPIO.setup(motor,GPIO.OUT)
GPIO.setup(b,GPIO.OUT)
GPIO.setup(a,GPIO.OUT)
pi_pwm = GPIO.PWM(motor,500)	
pi_pwm.start(0)

def MyThread():
  while threadRunning:
	for i in range (0,7):
		pi_pwm.start(80)
		GPIO.output(b,0)
		GPIO.output(a,1)
		sleep(0.15)
		pi_pwm.start(80)
		GPIO.output(b,1)
		GPIO.output(a,0)
		sleep(0.15)
	pi_pwm.start(0)	
	sleep(1.5)

import threading
threadRunning = True
threading.Thread(target=MyThread).start()

while True:
  time.sleep(1)

thomca96
Posts: 9
Joined: Mon Feb 04, 2019 11:11 am

Re: delay for python

Wed Feb 06, 2019 2:30 pm

gordon77 wrote:
Wed Feb 06, 2019 10:17 am

Code: Select all

start = time.time()
while time.time() - start < 0.15:
     pass
Perfect, it is working!!
Thanks a lot

thomca96
Posts: 9
Joined: Mon Feb 04, 2019 11:11 am

Re: delay for python

Wed Feb 06, 2019 2:32 pm

hippy wrote:
Wed Feb 06, 2019 2:23 pm
thomca96 wrote:
Wed Feb 06, 2019 1:16 pm
how could i implement it that the PWM code is running in his own threat?
Sorry but I am not the best programmer...
Untested but something like this should be a good starting point. You can do other things in the final 'while True:' loop other than just sleep.

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
from time import sleep
motor = 12	
a=7
b=16	
GPIO.setwarnings(False)			
GPIO.setup(motor,GPIO.OUT)
GPIO.setup(b,GPIO.OUT)
GPIO.setup(a,GPIO.OUT)
pi_pwm = GPIO.PWM(motor,500)	
pi_pwm.start(0)

def MyThread():
  while threadRunning:
	for i in range (0,7):
		pi_pwm.start(80)
		GPIO.output(b,0)
		GPIO.output(a,1)
		sleep(0.15)
		pi_pwm.start(80)
		GPIO.output(b,1)
		GPIO.output(a,0)
		sleep(0.15)
	pi_pwm.start(0)	
	sleep(1.5)

import threading
threadRunning = True
threading.Thread(target=MyThread).start()

while True:
  time.sleep(1)
Wow, Thanks for your help!!!

Return to “Python”