2

I have this piece of code:

#!/usr/bin/env python
#coding: utf8 
import RPi.GPIO as GPIO
from picamera import PiCamera
from time import sleep

camera = PiCamera()
# pin-count
GPIO.setmode(GPIO.BOARD)
# Pin 18 (GPIO 24) as input
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# loop
while 1:
# read input
    if GPIO.input(18) == GPIO.HIGH:
# if input = high start recording
        camera.start_recording('video.h264')
# PIN 16 (GPIO23) as input
GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# loop
while 1:
# read input
    if GPIO.input(16) == GPIO.HIGH:
# if input = high stop recording
        camera.stop_recording

If pin 18 is 1 (1.3+V) the recording should start but it just won't. I tried the exact same code with

camera.start_preview / camera.stop_preview

as code for pin 18 / 16 and it worked like a charm. I know that error comes up because I'm looping the "camera.start_recording" part but I don't now how to do this without the loop. If I try this without loopin it just starts recording as soon as I start the script. But I need it to start as soon as the pin counts as "high". Any Idea how I could do that?

  • Why not create a start and stop recording function and call it from your loop. Then you can combine the two loops with a conditional on the second (stop recording) conditional that checks if the recording has been started. – Steve Robillard Sep 8 '16 at 10:05
1

Create a flag that you will use to indicate that recording has started (e.g. isRecording = FALSE)

When you begin recording, set that flag (e.g. isRecording = TRUE).

Now, you can change your test for GPIO18 being HIGH to :

if ((GPIO.input(18) == GPIO.HIGH) and (isRecording == FALSE)):

Make sure you also reset the flag (to FALSE) when you stop recording.

EDIT - After further clarification

Sounds like there is no need for GPIO16 - you state you want to start recording when GPIO18 goes HIGH (the button is pressed) and keep recording until it then goes LOW again (you stop pressing the button)..

Here is some code that should do:

#!/usr/bin/env python
#coding: utf8 
import RPi.GPIO as GPIO
from picamera import PiCamera
from time import sleep

camera = PiCamera()
# pin-layout
GPIO.setmode(GPIO.BOARD)
# Pin 18 (GPIO 24) as input
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# recording flag
isRecording = 0

# loop
while 1 :
    # read input
    if (GPIO.input(18) == GPIO.HIGH) and (isRecording == 0):
        # if input = high start recording
        camera.start_recording('video.h264')
        isRecording = 1

    if (GPIO.input(18) == GPIO.LOW) and (isRecording == 1):
        # loop waiting for the 'stop signal'
        camera.stop_recording
        isRecording = 0

Essentially this loops forever, checking for the following two cases:

  • Is the pin HIGH and recording is not happening (so start it)
  • Is the pin LOW and recording is happening (so stop it)

Your description of 'press button pin goes high, stop pressing other pin goes low' sounds odd - you might also want to post your pin connections / circuit diagram so we can check that is correct too.

  • I just tried this but its still starting right away did I do something wrong? GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) isRecording = False while 1: if GPIO.input(18) == GPIO.HIGH and isRecording == False: isRecording = True camera.start_recording('video.h264') GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) while 1: if GPIO.input(16) == GPIO.HIGH and isRecording == True: isRecording = False camera.stop_recording – Fanservice Sep 8 '16 at 11:47
  • You might want to check the logic level of pin 18 - check it in code and print the value out of debug purposes. It would also help to understand what it is you are trying to do - pin 18 for start and pin 16 for stop (completely independent ?, or does pin 18 have to stay HIGH after starting, etc). – KennetRunner Sep 8 '16 at 12:28
  • I have a button and if I press that button pin 18 is high. If I let go of that button pin 18 goes low and pin 16 goes high. If I keep the button pressed pin 18 stays high until I stop pressing the button. – Fanservice Sep 8 '16 at 12:43
  • I realized myself that I don't need pin 16.. :D thats why it took a while to accept your answer, I just accept yours because its the same thing I did after flagging 2 print commands and noticing that I can't get to the second but thanks anyway – Fanservice Sep 14 '16 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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