carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

time.sleep function problem

Mon Feb 18, 2019 11:16 pm

Hello, These day, I am working about 'Playing Video trigger' by using Ultrasonic sensor.
I use Raspberry 3 PI b+ model which is installed with Raspbian OS.

and that is my code

Code: Select all

import RPi.GPIO as GPIO
import time, sys, os
from subprocess import Popen
import subprocess as sp

distance = 400
zone = 1  

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
 
GPIO_TRIGGER = 18
GPIO_ECHO = 24
 
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

movie2 = ('/home/pi/Videos/sample1.mp4')
movie1 = ('/home/pi/Videos/the tide2.mp4')

def distance():
    
    GPIO.output(GPIO_TRIGGER, True)
 
    
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
   
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    TimeElapsed = StopTime - StartTime
    distance = (TimeElapsed * 34300) / 2

    return distance

if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            time.sleep(1)
            
            if (distance() <= 100):
                zone = 1
            if (distance() >= 2000):
                zone = 1
            elif (100 < distance() < 2000):
                zone = 2
        
            if (zone == 1):
                Popen(['/usr/bin/omxplayer', '--display','0', movie1])
                time.sleep(6)
                
            if (zone == 2):
                Popen(['/usr/bin/omxplayer', '--display','0', movie2])
                time.sleep(6)
                
    except KeyboardInterrupt:
      GPIO.cleanup()
Depend on the distance, 'movie1' and 'movie2' are played very well, but problem is that
When the distance is changed, the video is not changed instantly and after 6 sec, the video is played because of time.sleep function.
But if I don't use time.sleep function in this code, this code doesn't work and even my PI is shutdown.
How can I fix this problem? If you have any suggestions, I would greatly appreciate it! Thanks!

User avatar
MrYsLab
Posts: 331
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: time.sleep function problem

Mon Feb 18, 2019 11:30 pm

A few questions:
1. What value do you see for TimeElapsed?
2. Under __main__, in your while True loop, you are calling distance and assigning the return value to dist, but you are never using dist. Instead, the code is calling distance() and then doing a compare. Is this what you intended to do?

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: time.sleep function problem

Mon Feb 18, 2019 11:47 pm

MrYsLab wrote:
Mon Feb 18, 2019 11:30 pm
A few questions:
1. What value do you see for TimeElapsed?
2. Under __main__, in your while True loop, you are calling distance and assigning the return value to dist, but you are never using dist. Instead, the code is calling distance() and then doing a compare. Is this what you intended to do?
Oh so sorry, that's my mistake! I have to fix that
And, videos ('movie1' and 'movie2') have 6 sec, so I need to consider the values.

User avatar
MrYsLab
Posts: 331
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: time.sleep function problem

Mon Feb 18, 2019 11:50 pm

No problem. It just sometimes needs a second set of eyes. Hope things are now working.

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: time.sleep function problem

Mon Feb 18, 2019 11:53 pm

MrYsLab wrote:
Mon Feb 18, 2019 11:50 pm
No problem. It just sometimes needs a second set of eyes. Hope things are now working.
Oh sorry but, my problem is that 'time.sleep' function ignores any other input data, so the videos are not instantly changed.
Can you give any idea how to fix that?

User avatar
MrYsLab
Posts: 331
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: time.sleep function problem

Tue Feb 19, 2019 12:32 am

Sleep is a blocking call within the current thread. Once it starts, nothing else will run until it completes. You probably want to add a second thread to test if the switch should be done and set a flag. You can have a loop in the first thread with a short sleep that will constantly check the flag.

I hope I am understanding your problem.

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: time.sleep function problem

Tue Feb 19, 2019 12:19 pm

MrYsLab wrote:
Tue Feb 19, 2019 12:32 am
Sleep is a blocking call within the current thread. Once it starts, nothing else will run until it completes. You probably want to add a second thread to test if the switch should be done and set a flag. You can have a loop in the first thread with a short sleep that will constantly check the flag.

I hope I am understanding your problem.
Yes, you are right, that's my problem but sorry, I am a beginner, so could you give some brief example code about your idea?

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: time.sleep function problem

Tue Feb 19, 2019 12:47 pm

MrYsLab wrote:
Tue Feb 19, 2019 12:32 am
Sleep is a blocking call within the current thread. Once it starts, nothing else will run until it completes. You probably want to add a second thread to test if the switch should be done and set a flag. You can have a loop in the first thread with a short sleep that will constantly check the flag.

I hope I am understanding your problem.
I just try to put thread on my code, but i am not sure how to put the second thread on my code.

Code: Select all


if __name__ == '__main__':
    try:
        def first():
            
            while True:
                dist = distance()
                time.sleep(1)
            
                if (dist() <= 100):
                    zone = 1
                if (dist() >= 2000):
                    zone = 1
                elif (100 < dist() < 2000):
                    zone = 2
        
                if (zone == 1):
                    Popen(['/usr/bin/omxplayer', '--display','0', movie1])
                    time.sleep(1)
                
                if (zone == 2):
                    Popen(['/usr/bin/omxplayer', '--display','0', movie2])
                    time.sleep(1)
                
        def second():
            count = 1
            while count < 6:
                tmp_thread = Thread(target=first, args=[count])
                tmp_thread.start()
                count +=1
                time.sleep(1)
                
    except KeyboardInterrupt:
      GPIO.cleanup()

User avatar
MrYsLab
Posts: 331
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: time.sleep function problem

Tue Feb 19, 2019 2:12 pm

Here is a very simple example of running 2 separate sleeps - one in each thread.

Code: Select all

import threading
import time


class MyPlayer(threading.Thread):

    def __init__(self):

        # initialize the inherited Thread object
        threading.Thread.__init__(self)
        self.daemon = True
        self.thread1()

    def thread1(self):
        # start the 2nd thread
        # you must start the 2nd thread using the name "start"
        self.start()
        while True:
            print('thread 1')
            time.sleep(1)

    def run(self):
        """
        This the second thread's executable code. It must be called run.
        """
        while True:
            print('thread 2')
            time.sleep(2)

MyPlayer()

If you would like to to learn more about concurrency (running multiple things at once), here is a very good article: https://realpython.com/python-concurrency/

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: time.sleep function problem

Tue Feb 19, 2019 4:15 pm

MrYsLab wrote:
Tue Feb 19, 2019 2:12 pm
Here is a very simple example of running 2 separate sleeps - one in each thread.

Code: Select all

import threading
import time


class MyPlayer(threading.Thread):

    def __init__(self):

        # initialize the inherited Thread object
        threading.Thread.__init__(self)
        self.daemon = True
        self.thread1()

    def thread1(self):
        # start the 2nd thread
        # you must start the 2nd thread using the name "start"
        self.start()
        while True:
            print('thread 1')
            time.sleep(1)

    def run(self):
        """
        This the second thread's executable code. It must be called run.
        """
        while True:
            print('thread 2')
            time.sleep(2)

MyPlayer()

If you would like to to learn more about concurrency (running multiple things at once), here is a very good article: https://realpython.com/python-concurrency/
Thank you so much ! :)

Return to “Python”