#import packages from gpiozero import Button, MotionSensor from picamera import PiCamera from time import sleep from signal import pause #create objects button = Button(2) pir = MotionSensor(4) camera = PiCamera() #start the camera camera.rotation = 180 camera.start_preview() #image image names i = 0 #stop the camera when the momentary switch pressed def stop_camera(): camera.stop_preview() #exit the program exit() #take a picture when motion detected def take_picture(): global i i = i + 1 camera.capture('/home/pi/Desktop/image_%s.jpg' % i) print('A picture has been taken') sleep(10) #assign a function that runs when the momentary switch pressed button.when_pressed = stop_camera #assign a function that runs when motion detected pir.when_motion = take_picture pause()