0

I am looking for knowledge.

I am currently working on a program which, if no one is detected by my sensor for 30 seconds, launches a video. I am currently using time.time() but the concern is that despite trying to reset, the timer continues and does not reset to 0 once the video is finished.

So I tried some new research and I came across a topic about time.clock() and I ended up with this: Python's time.clock() vs time.time()

But I'm not sure I fully understand the role of timeit, time.process_time() or time.perf_counter(), is there a possibility to use these in the way I want? Or is it only used to calculate the execution time of a code as found in several examples?

Precision:

When I reach 30 seconds, I enter the if to launch the video, the launch video, followed by a time.sleep(). The timer does not pause and continues while the video is running. Once the video is finished I try to reset the timer, also once the 50 seconds are reached but I can't do it, I probably don't reset what I need. The timer therefore never stops and my other functions which require a timer (if timer> 7) are launched directly since the condition is fulfilled (whereas they should start again from 0 when entering the other functions)

PS: When a video is running, the other videos are not superimposed on it, they kindly wait their turn

while True:
    if ...:
        try:
            if(200 <= DD): # If we are more than 200 cm away
                # We count the number of seconds between the last checkpoint and now
                face_walking_apparition_time = time.time() - walking_timer # (t+3) - t = 3, it's how u calculate timer
                
                # Then we check if a face has appeared for more than 30 seconds:
                if face_walking_apparition_time > 30: # If the block is detected more than 30 seconds
                    # Display video
                    time.sleep(25)
                    face_motionless_apparition_time = 0 # I try to reset here
                    face_walking_apparition_time = 0 # I try to reset here
                if face_walking_apparition_time >= 50: # After 20 seconds of video, reset
                    face_walking_apparition_time = 0 #I try to reset here
                    face_motionless_apparition_time = 0 # I try to reset here
                    
            elif (50 < DD) and (DD < 200):
                # As we don't see no face, we have to reset our checkpoint to "now"
                face_motionless_apparition_time = 0 # I try to reset here   
                face_walking_apparition_time = 0 # I try to reset here

PS: I'm new to python so my code might be horrible to read

PabloS
  • 27
  • 5
  • 1
    i'm not sure to understand what is your problem, could you be more precise (for exemple which timer is causing problems) ? and could you also isolate the chunk of code that is concerned directly ? – Valentin C Jun 08 '21 at 13:33
  • time is not a timer, time is a library of time related functions. time.time() is the current time now, you can't pause the progression of time, not even with python. – JeffUK Jun 08 '21 at 13:53
  • @JeffUK So is there a way to create a timer that can meet my expectations ? (a new edit has been made) – PabloS Jun 08 '21 at 13:58

1 Answers1

2

I think you may misunderstand how the "timer" works

what we usually do is intitializing a variable with time.time() wich represents the number of seconds elapsed since 01/01/1970 and then when we want to mesure the time like a timer, we substract a second instance of time.time() with the previous one, which give us the mesured time between the two.

Using time.sleep(n) simply waits n seconds before going to the next line, so it won't reset your timer

you need to rethink your code around these notions

Valentin C
  • 176
  • 7