0

I have been working on a project that will allow me to take picture using a switch. So for that purpose i setup the GPIO and all necesseary tools for that code, Python-dev and etc.

When i run the code to control only the LED it works perfectly, i press the button, the LED turns on, and when i let go of the switch the LED turns off.

However the issue starts when i try to add the camera module to the code, when i add all other stuff like take a picture and prompt ls -l, the code does not wait for me to press the switch, instead it takes picture itself over and over again. I am am using rpi2 with latest version of raspian.

FYI I updated and upgraded the raspberry pi2.

What am i doing wrong? here is the code

import picamera
import time
import RPi.GPIO as GPIO
import subprocess
from subprocess import call


#def main():

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #pull down 
GPIO.setup(7, GPIO.OUT) #setup the LED for Processing
GPIO.output(7,0)

def blink():
    GPIO.output(7,1)
    time.sleep(1)
    GPIO.output(7,0)
    time.sleep(1)

def camera():
    subprocess.call(["sudo", "python", "cameratest.py"])    
    call(["ls", "-l"])

try:
    while True:
        if GPIO.input(11) == 1:  #gpio input first button
            blink()
            camera()
        else:
            GPIO.output(7,0) #if button false turn down the$
except KeyboardInterrupt:
        GPIO.cleanup()

1 Answer 1

1

I think i found a solution firstly, I found that I can not use double quote on the subprocess so the format should be

subprocess.call (['sudo', 'nano', 'etc ')

and the answer to my question is I had to wait for the switch to be activate something like this

While True:
  GPIO.wait_for_edge(11, FALLING)
  time.sleep(1)
  if GPIO.input(11):

etc...
1
  • 1
    Quotes are a red herring. Python and Linux should be happy with both single and double quotes. Try print(subprocess.call(["echo", "world", 'a b c', "def", ' g hij']))
    – joan
    Commented Mar 9, 2015 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.