mercredi 7 novembre 2018

How can I ask the device wait for bit to see if the button is pressed or not, and do different action?

I'm building a button-camera with python on raspberry pi there is a shutter in front of the camera.

I tried to build:

The shutter will open automatically, and camera will take photo automatically, too.

however, when I pressed the button, the camera will stop taking photo, and the shutter will close too.

simple illustration, see this photo

this is my current python script.

from time import sleep
from picamera import PiCamera
from picamera import PiCamera
import RPi.GPIO as GPIO
import datetime
import time

camera = PiCamera()
sleep(WAIT_TIME)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.OUT)

p15 = GPIO.PWM(15, 50) #P15 is shutter motor
p15.start(0)

GPIO.setup(3, GPIO.IN) #P3 is button

def take_photo():
    for i in range(2):
    date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M_%S")
    timeCaptured = '/home/pi/timelapse/TEST_%s.jpg' %date
    camera.capture(timeCaptured)
    time.sleep(1)

def open_shutter():
    print('move')
    p15.ChangeDutyCycle(3)
    time.sleep(1)
    p15.ChangeDutyCycle(0)
    time.sleep(0.5) 


def close_shutter():
    p15.ChangeDutyCycle(6.2)
    time.sleep(1)
    p15.ChangeDutyCycle(0)
    time.sleep(1)

It is working. It wait for 1 min. However, I have to always press the button, otherwise it will always taking photo. How can I ask the device wait for bit to see if the button is pressed or not, and do different action?

while True:
try:
    GPIO.wait_for_edge(3, GPIO.RISING)
    time.sleep(60)
    if GPIO.input(3) == GPIO.LOW:
        print('Button pressed')
        close_shutter()
        time.sleep(1)

    elif GPIO.input(3) == GPIO.HIGH:
        open_shutter()
        take_photo()
        close_shutter()
        time.sleep(1)
    exit()

except(KeyboardInterrupt):
    print('AN Error was raised in sequence')
    close_shutter()
    p15.stop()
    GPIO.cleanup()

Aucun commentaire:

Enregistrer un commentaire