A little help with python

Caporegime
Joined
18 Oct 2002
Posts
25,287
Location
Lake District
I'm trying to wrap my brains around getting the following to work:

A timer is set for 5 minutes which then begins to count down, if a GPIO pin goes high, a command is executed then the timer is reset back to 5 minutes, however if it goes low, the timer continues to run out, when the timer reaches zero, a command is executed.

I think I'm going to have to use threading to do this? I'm using a callback from the GPIO.add_event_detect but I can't get my head around the logic required to make this work.
 
This is what I have ended up with and it appears to be working:

Code:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
global timer
timer = 300 # 5 mins
subprocess.call("sudo /home/pi/rpi-hdmi.sh on", shell=True)
try :
    print "Screen Timer (CTRL+C to exit)"
    time.sleep(5)
    print "Ready..."
    while 1:
        time.sleep(0.985)
        print timer
        # Test PIR_PIN condition
        current_state = GPIO.input(PIR_PIN)
        if timer > 0:
            timer -= 1
            if current_state: #is true
                # Reset timer
                timer = 300
        else:
            if current_state: #is true
                subprocess.call("sudo /home/pi/rpi-hdmi.sh on", shell=True)
                # Reset timer
                timer = 300
            else:
                # Wait for edge
                channel = GPIO.wait_for_edge(PIR_PIN, GPIO.RISING)
                if channel is None:
                        print('Timeout occurred')
                else:
                        subprocess.call("sudo /home/pi/rpi-hdmi.sh on", shell=True)
                        # Reset timer
                        timer = 300
except KeyboardInterrupt:
    print "Quit"
    GPIO.cleanup()
 
Actually that didn't work and it got stuck waiting for edge, revised code is now below, but having issues with false positives on the PIR.

Code:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
global timer
time_limit = 300 #5 mins
timer = time_limit
subprocess.call("sudo /home/pi/rpi-hdmi.sh off", shell=True)
try :
    print "Screen Timer (CTRL+C to exit)"
    time.sleep(5)
    print "Ready..."
    while 1:
        time.sleep(0.985)
        # Test PIR_PIN condition
        current_state = GPIO.input(PIR_PIN)
        if timer > 0:
            timer -= 1
        if current_state: #is true
                subprocess.call("sudo /home/pi/rpi-hdmi.sh on", shell=True)
                # Reset timer
                timer = time_limit
        elif timer == 0:
                # turn off hdmi as timer expired
                subprocess.call("sudo /home/pi/rpi-hdmi.sh off", shell=True)
                # Wait for edge
                channel = GPIO.wait_for_edge(PIR_PIN, GPIO.RISING)
                if channel == PIR_PIN:
                        # turn hdmi back on
                        subprocess.call("sudo /home/pi/rpi-hdmi.sh on", shell=True)
                        # Reset timer
                        timer = time_limit
except KeyboardInterrupt:
    print "Quit"
 
Last edited:
Back
Top Bottom