My project is to build a car dash cam that will record my journey onto a USB stick (16Gb). The Pi is powered via a 12V to 5V USB car charger (2A). I have two LEDs (red to indicate recording + blue to indicate the program is running) and two buttons (red to start/stop recording+ green to shutdown the pi).
I have a Python program that starts automatically via rc.local. I have made it executable and when I boot my Pi, the program runs and everything works as expected. I am able to start and stop recording video to the USB and shutdown the Pi via the buttons.
The problem comes when I have been recording for a while, the pi stops reacting to button presses and is effectively stuck.
My question is: Is there a better way of coding my project? Would interrupts be a better idea? Am I hitting a recursion limit? Any advice or ideas would be appreciated!
The Python Code:
Code: Select all
GPIO.cleanup()
import time
import picamera
import RPi.GPIO as GPIO
import subprocess
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Red Rec Button
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Green Shutdown Button
GPIO.setup(24, GPIO.OUT) #Blue LED
GPIO.setup(23, GPIO.OUT) #Red LED
GPIO.output(24, 1)
GPIO.output(23, 0)
def choice():
while 1:
rec=GPIO.input(17)
shut=GPIO.input(18)
if rec==False: #If Pressed
camera()
elif shut==False: #If Pressed
shutdown()
def camera():
date=time.strftime("%d%m%Y%H%M%S")
with picamera.PiCamera() as camera:
#camera.start_preview()
GPIO.output(24, 0)
GPIO.output(23, 1)
camera.resolution = (1024, 768)
camera.framerate = 30
camera.start_recording('/media/usb/%s.h264' % date ) #USB is set up correctly, mounted okay
time.sleep(0.5)
GPIO.wait_for_edge(17, GPIO.FALLING)
camera.stop_recording()
GPIO.output(23, 0)
GPIO.output(24, 1)
#camera.stop_preview()
time.sleep(0.5)
#Do I need to go back to choice() here instead?
def shutdown():
GPIO.cleanup()
subprocess.call(['shutdown -h now "System halted by GPIO action"'], shell=True)
GPIO.cleanup()
choice()
Pog