Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using the GPIO pins on my Raspberry Pi with a PIR sensor to detect motion. When the sensor detects motion I want to then move the software onto other functions.

At the moment, to detect motion I have my program constantly running in a loop while it is waiting for motion to be detected. While this works at the moment, for use in the future this will be incredibly inefficient and am hoping to improve on this by assigning it to an event.

Is there any way to bind my GPIO input to an event that is detected by the program without manually running a loop.

Here is my current loop for detection motion:

var=1
counter = 0
while var == 1:
    if GPIO.input(7):
        counter += 1
    time.sleep(0.5)

    else:
        counter = 0
        time.sleep(1)

    if counter >= 3:
        print "Movement!"
        captureImage()
        time.sleep(20)

The counter and detecting motion multiple times is used to reduce the number of false positives that the sensor picks up.

share

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.