User avatar
scott_pdp
Posts: 15
Joined: Thu Apr 30, 2015 1:12 pm
Location: Port-de-Paix, Haiti

GPIO button press not working

Wed Mar 23, 2016 2:30 pm

I have some code for a camera project that has worked great in the past. I had some trouble with a solder joint on my RTC and fixed that and then reimaged my card with lastest Jessie (3-18) and put my camera script back on it. RTC is working fine which means I2C is working fine. GPIO 25 which turns on a 'ready LED' is also working fine. Can anyone have a look at this and see if there is any glaring problem? If I get rid of the 'if (GPIO.input(23) is False):' line, the camera takes pictures indefinitely, so the camera code all works fine. I just can't get it to respond to the button press and none of the button wiring or other hardware has changed.

Thank you in advance for take a few minutes.

Code: Select all

#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
import picamera
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
CAMLED = 32
READYLED = 25
GPIO.setup(CAMLED, GPIO.OUT, initial=False)
GPIO.setup(READYLED, GPIO.OUT, initial=True)
GPIO.output(CAMLED, False)
GPIO.setup(23, GPIO.IN)
GPIO.setup(25, GPIO.OUT)
TIMEOUT = 480  # 8 minutes (60 secs * 8 min)

start = time.time()
now = time.time()
if now < start:
    start = time.time()
    now = time.time()
while now - start < TIMEOUT:
    now = time.time()

## while True: Used before adding the 'sleep-timer' functionality
if (GPIO.input(23) is False):
    start = time.time()
    with picamera.PiCamera() as camera:
        ##camera.resolution = (1944, 1944)'6 x 6 square format
        camera.resolution = (2592, 1944)  # 4:3 landscape format
        camera.iso = 300
        camera.contrast = 40
        camera.brightness = 53
        camera.color_effects = (128, 128)
        camera.meter_mode = 'matrix'
        camera.led = False
        camera.vflip = True
        camera.hflip = True
        ##Set 'Ready' LED to False
        GPIO.output(READYLED, False)
        ##Camera warm-up time
        time.sleep(.600)
        camera.capture("/mnt/usb/" + time.strftime
                      ("%y%m%d_%H-%M-%S") + ".jpg")
        ##camera.close
        time.sleep(0.1)
        GPIO.output(READYLED, True)

path = 'sudo shutdown -h now'
os.system(path

DirkS
Posts: 7751
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: GPIO button press not working

Wed Mar 23, 2016 3:08 pm

How is it supposed to work?
Now it is a 'one shot' script: you wait for 8 minutes, then hope a button is pressed at just the right moment to make a photo...

User avatar
scott_pdp
Posts: 15
Joined: Thu Apr 30, 2015 1:12 pm
Location: Port-de-Paix, Haiti

Re: GPIO button press not working

Wed Mar 23, 2016 5:32 pm

Thanks for having a look. Sorry, I should have been way more clear about its normal use. The script runs at startup. The 'ready LED' comes on. If you press the button attached to GPIO 23, the script will capture the image and save the file. If the button is not pressed for 8 minutes, it performs a shutdown. Any time the button is pressed, we reset the now variable so that our 8 minute timer is reset.

Does that help?

User avatar
cheez
Posts: 13
Joined: Wed Mar 23, 2016 5:27 pm
Location: Iowa

Re: GPIO button press not working

Wed Mar 23, 2016 7:09 pm

It is testing for the button press AFTER the timeout, not during.
And even if you were pressing the button right at the 8 minute mark, it would take the picture, but it would not reset the timer because the "while" loop is already past.
You need to indent your "if" statement to be inside the "while" loop.
It looks like you have a similar problem with the other "if" statement that checks if the now is less than the start. You set the two variables, test them in the if, and then move on. Nothing is nested.

Stupidav
Posts: 10
Joined: Wed Mar 16, 2016 10:02 pm

Re: GPIO button press not working

Wed Mar 23, 2016 9:12 pm

Perhaps something like this, although I haven't tested it but it looks like it might be the correct:

Code: Select all

#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
import picamera
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
CAMLED = 32
READYLED = 25
TakePictureBtn = 23
GPIO.setup(CAMLED, GPIO.OUT, initial=False)
GPIO.setup(READYLED, GPIO.OUT, initial=True)
GPIO.output(CAMLED, False) 
GPIO.setup(TakePictureBtn, GPIO.IN)
TIMEOUT = 480*1000  # 8 minutes (60 secs * 8 min)

while True:
     # wait for up to 480 seconds for a rising edge (timeout is in milliseconds)
     channel = GPIO.wait_for_edge(TakePictureBtn, GPIO_RISING, timeout=TIMEOUT)
     if channel is None:
         path = 'sudo shutdown -h now'
         os.system(path)

     else:
         with picamera.PiCamera() as camera:
             ##camera.resolution = (1944, 1944)'6 x 6 square format
             camera.resolution = (2592, 1944)  # 4:3 landscape format
             camera.iso = 300
             camera.contrast = 40
             camera.brightness = 53
             camera.color_effects = (128, 128)
             camera.meter_mode = 'matrix'
             camera.led = False
             camera.vflip = True
             camera.hflip = True
             ##Set 'Ready' LED to False
             GPIO.output(READYLED, False)
             ##Camera warm-up time
             time.sleep(.600)
             camera.capture("/mnt/usb/" + time.strftime("%y%m%d_%H-%M-%S") + ".jpg")
             ##camera.close
             time.sleep(0.1)
             GPIO.output(READYLED, True)
Here is some info on the wait_for_edge function: https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/

Return to “Python”

Who is online

Users browsing this forum: No registered users and 4 guests