CookieGamez2018
Posts: 6
Joined: Sun Dec 09, 2018 9:22 pm

Have a certain time to do something

Sat Dec 15, 2018 10:45 pm

Hi,
i am building a basic alarm system. what i want is when the motion sensor goes off you have 15 seconds to hit a push button but i cant find out how to do that.
how can i make it so you have a set amount of time to hit the button but if you don't in that time period the alarm goes off.

all help is appreciated, thanks

User avatar
paddyg
Posts: 2561
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Have a certain time to do something

Sun Dec 16, 2018 8:52 am

in python you can import time module and then time.time() will give you the time in seconds. You will need to keep track of when your movement detector was first triggered then loop until more than 15s have elapsed. You will need variables to keep track of which state you're in - button or movement etc. This is the kind of thing much better done with functions in different threads, probably using the gpiozero module. If any of that sounds alien or doesn't make sense then I would strongly advise taking a break from your project and spending a few days doing a 'learn python' course; lots of good ones around.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Idahowalker
Posts: 534
Joined: Wed Jan 03, 2018 5:43 pm

Re: Have a certain time to do something

Mon Dec 17, 2018 12:12 pm

import time

Code: Select all

class cRuntime():
    def __init__(self):
        self.iStartTime = int( time.time() )
        self.iRunTimeDays = 0
        self.iRunTimeHours = 0
        self.iRunTimeMinutes = 0
        self.iRunTimeSeconds = 0

    def fDoRunTime( self ):
        self.iRunTimeDays = divmod( (int(time.time()) - self.iStartTime), 86400 )
        self.iRunTimeHours = divmod( self.iRunTimeDays[1], 3600 )
        self.iRunTimeMinutes = divmod( self.iRunTimeHours[1], 60 )
        self.iRunTimeSeconds = self.iRunTimeMinutes[1]
        return "%03d:%02d:%02d:%02d" %( self.iRunTimeDays[0], self.iRunTimeHours[0], self.iRunTimeMinutes[0], self.iRunTimeSeconds);
The above takes 2 times (start time and now) subtracts and returns the difference. Your start time can be in the future. In your case you'll be looking for a count down to zero or close to zero. I'm sure you can adapt the above code to your requirements.
Without knowing why you are deleting my postings, I will not know how...

Return to “Python”