BartRasp
Posts: 1
Joined: Mon Feb 18, 2019 7:38 pm

Stopping the Camera preview with ctrl-c ánd push-button?

Mon Feb 18, 2019 8:05 pm

Hi,

I am just trying to get my hands round the Raspberry Pi and especially the camera module. For this I was using the tutorial from the website:
https://projects.raspberrypi.org/en/pro ... p-motion/7

Everything works fine, but when using the code that is provided in the tutorial I encounter something that I just don't understand. I can't stop the program from running with ctrl+c alone. After pressing ctrl-c I also have to press once more the button (connected to the GPIO) before the camera preview stops. This is of course not a major problem, but I am very eager to understand why? Because where in the code is it mentioned that it should wait for another push on the button before stopping?

The code is this:

Code: Select all

from picamera import PiCamera, Color
from time import sleep
from gpiozero import Button

button = Button(17)
camera = PiCamera()
camera.rotation =180

frame = 1

while True:
    try:
        camera.start_preview(alpha=150)
        button.wait_for_press()
        camera.capture('/home/pi/animation/frame%03d.jpg' % frame)
        frame += 1
    except KeyboardInterrupt:
        camera.stop_preview()
        camera.close()
        break
Has anyone a idea? As you must now I am not an experienced prgrammer, so maybe it something that seems very obvious for a more experienced person.

I really appreciate your help!

Return to “Python”