Python Forum
How to add a delay without using the sleep command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add a delay without using the sleep command
#1
I'm working on an assignment in which I am supposed to output a sentence saying how many times the photointerrupter has been interrupted every 4 seconds. This would normally be easy but we are not allowed to use the sleep command. We don't have any restrictions on resources to use so forums such as these are fair game. Anyone have any ideas as to what I could use?
Reply
#2
Here's one way to go about it:

import time
start = time.time ()
while time.time () < start + 4 :
	pass
MaxTyree likes this post
Reply
#3
thank you so much
Reply
#4
This would be impossible using the sleep command unless the photointerrupter is smart enough to maintain a count.
Reply
#5
(Feb-02-2021, 09:41 PM)MaxTyree Wrote: I'm working on an assignment in which I am supposed to output a sentence saying how many times the photointerrupter has been interrupted every 4 seconds. This would normally be easy but we are not allowed to use the sleep command. We don't have any restrictions on resources to use so forums such as these are fair game. Anyone have any ideas as to what I could use?

what exactly would you be adding 4 to. In the code you posted you are essentially saying, "while current time is less than start time (which is always true), add 4." I'm mostly confused as to what you are adding 4 to.
Reply
#6
Read about time.time()

Current time is always greater than start time (time counts up, not down), but current time is only less than start + 4 for four seconds. Personally I prefer end_time = time.time() + 4.
Reply
#7
Quote:what exactly would you be adding 4 to. In the code you posted you are essentially saying, "while current time is less than start time (which is always true), add 4." I'm mostly confused as to what you are adding 4 to.

The four is being added to the 'start' variable which is the first output of time.time (). When you take the current time and add four seconds to it you have a representation of four seconds in the future. Now the while loop sits around doing nothing until time.time () catches up with start (which was four seconds in the future when we started). Got it?

It might be easier for your to see if we went about it like this:
import time
start = time.time ()
finish = start + 4
while time.time () < finish :
    pass
Reply
#8
The four is being added to the 'start' variable which is the first output of time.time (). When you take the current time and add four seconds to it you have a representation of four seconds in the future. Now the while loop sits around doing nothing until time.time () catches up with start (which was four seconds in the future when we started). Got it?

It might be easier for your to see if we went about it like this:
import time
start = time.time ()
finish = start + 4
while time.time () < finish :
    pass
[/quote]

Ohhhh ok yes this makes much more sense. And I would output the interrupt sentence in a while loop where finish is equal to time.time(). Thank you for the follow up.
Reply
#9
You probably want to have the part where you are checking the interrupter status where line 5 is. Also, rather than checking if finish is equal to time.time() as in your comment, you really want to use an inequality like in the code snippet.
Reply
#10
while True:
    end_time = time.time() + 4
    count = 0
    while time.time() < end_time:
        # check photointerrupter
    # report count
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020