Hi,
Can anyone point me in the direction of a project with code to use a tilt switch or an accelerometer to trigger a USB or pi camera to take a photo?
Code: Select all
#!/usr/bin/env python3
import RPi.GPIO as GPIO
from picamera import PiCamera
from time import sleep
# initialize the camera
camera = PiCamera()
TiltPin = 11
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(TiltPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def take_photo(x):
camera.resolution = (1024, 768)
camera.start_preview()
# Camera warm-up time
sleep(2)
camera.capture('foo.jpg')
camera.stop_preview()
def loop():
GPIO.add_event_detect(TiltPin, GPIO.FALLING, callback=take_photo, bouncetime=100) # wait for falling
while True:
pass # Don't do anything
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()