stromecek555
Posts: 32
Joined: Fri Apr 02, 2021 10:01 am

button rpi zero 2 not work why ?

Sun Mar 26, 2023 1:00 pm

Hi,i need help in my code need button but not work ,why ? thank you i using zero 2 rpi

Code: Select all

import picamera 
from time import sleep
from gpiozero import Button

with picamera.Picamera() as camera:
button = Button(14)



def take_photo():
    camera.start_preview()
    sleep(0.3)
    camera.capture('/home/strom/image{0:04d}.jpg')
    camera.stop_preview()
    
button.when_pressed = take_photo
Attachments
Snímek obrazovky (947).png
Snímek obrazovky (947).png (228.57 KiB) Viewed 314 times

danjperron
Posts: 4391
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: button rpi zero 2 not work why ?

Sun Mar 26, 2023 1:11 pm

Your python code exit at the end.

You didn't put any loop to prevent the code to exit!

add the function pause() at the end

Code: Select all

button.when_pressed = take_photo
pause()
or make a loop

Code: Select all

try:
    while True:
        time.sleep(0.010)
except KeyboardInterrupt:
    pass

User avatar
rpiMike
Posts: 2396
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: button rpi zero 2 not work why ?

Sun Mar 26, 2023 1:14 pm

You'll need the following at the start:

Code: Select all

from signal import pause

stromecek555
Posts: 32
Joined: Fri Apr 02, 2021 10:01 am

Re: button rpi zero 2 not work why ?

Sun Mar 26, 2023 1:15 pm

thank you, but I still want to ask about line 6, is there an error and is there a button, what about it?

User avatar
rpiMike
Posts: 2396
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: button rpi zero 2 not work why ?

Sun Mar 26, 2023 1:21 pm

Your 'with picamera...' is wrong.

Code: Select all

import picamera 
from time import sleep
from gpiozero import Button
from signal import pause

from picamera import PiCamera
camera = PiCamera()

button = Button(14)

def take_photo():
    print('take_photo')
    camera.start_preview()
    sleep(0.3)
    camera.capture('/home/strom/image{0:04d}.jpg')
    camera.stop_preview()
    
button.when_pressed = take_photo

pause()

stromecek555
Posts: 32
Joined: Fri Apr 02, 2021 10:01 am

Re: button rpi zero 2 not work why ?

Sun Mar 26, 2023 3:11 pm

thank you, it works, I still want to ask how to make it take another photo instead and not overwrite the old one? Thank you

User avatar
rpiMike
Posts: 2396
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: button rpi zero 2 not work why ?

Sun Mar 26, 2023 4:55 pm

You could add date and time to the image name:

Code: Select all

import datetime

camera.capture('/home/strom/image'+datetime.datetime.now().strftime("%Y%m%d-%H%M%S")+'.jpg')

Return to “Python”