Raspberry Pi button interrupt realizes camera photo

Source: Internet
Author: User

Install the Picamera module first using the Python interrupt function add_event_detect and define the callback function Call_back ().
    • Add_event_detect (channel, GPIO.) RISING, Callback=test_callback, bouncetime=200)
    • Rising edge detection, associated callbacks, bouncetime for keypad software stabilization
Calling the Picamera method
def catpure_img():    camera = PiCamera()    camera.resolution = (1024,768)    camera.start_preview() #预览2秒    # Camera warm-up time 2s,beacause it need 2‘s to ***    GPIO.output(22,GPIO.HIGH)     sleep(2)    camera.capture(‘img_catpure/two.jpg‘) #捕捉图片    camera.stop_preview() # 关闭预览    camera.close() #要关闭,不然第二次中断响应会报错    GPIO.output(22, GPIO.LOW)
Total generation?
#!coding:utf-8from Time Import sleepfrom picamera import picameraimport rpi.gpio as Gpiobtnpin = 11Gpin = 12Rpin = 13d EF catpure_img (): Camera = Picamera () camera.resolution = (1024,768) camera.start_preview () # Camera warm-up T IME 2s,beacause It need 2 ' to * * * gpio.output (22,gpio. High) Sleep (2) camera.capture (' img_catpure/two.jpg ') Camera.stop_preview () Camera.close () Gpio.output (G PIO. Low) def setup (): Gpio.setmode (GPIO). BOARD) # Numbers GPIOs by physical location Gpio.setup (Gpin, GPIO. # Set Green Led Pin mode to Output Gpio.setup (Rpin, GPIO). # Set Red Led Pin mode to Output Gpio.setup (GPIO). Out) Gpio.setup (Btnpin, GPIO. In, Pull_up_down=gpio. PUD_UP) # Set Btnpin ' s mode is input, and the pull-up-to-level (3.3V) Gpio.add_event_detect (Btnpin, GPIO.        BOTH, Callback=detect, bouncetime=200) def Led (x): if x = = 0:gpio.output (Rpin, 1) gpio.output (gpin, 0)      Catpure_img () if x = = 1:  Gpio.output (Rpin, 0) gpio.output (gpin, 1) def Print (x): if x = = 0:print (' *********************** ')   Print (' * Button pressed!  * ') print (' *********************** ') def Detect (CHN): Led (Gpio.input (Btnpin)) print (Gpio.input (btnpin)) def Loop (): While True:passdef destroy (): Gpio.output (Gpin, GPIO. High) # Green led off Gpio.output (Rpin, GPIO.  High) # Red led off Gpio.cleanup () # Release Resourceif __name__ = = ' __main__ ': # program Start from here Setup () Try:loop () except Keyboardinterrupt: # when ' CTRL + C ' is pressed, the child prog        Ram destroy () would be executed. Destroy ()

Raspberry Pi button interrupt for camera capture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.