I've attempted to modify this script here, but instead of for 4 video outputs, to only 2. I'm pretty new to programming a pi and am trying to wrap this up for an gallery project. Here is my current script. The video plays currently, but get stuck at the 102 second mark. What I am attempting to do is have a video of an eye closed when in idle state, but when motion is sensored, to have the eye appear to open and look around. Any help is appreciated.
from omxplayer.player import OMXPlayer
from pathlib import Path
import time #for determining the time of playback
import RPi.GPIO as GPIO #for taking signal from GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_PIR = 7 #The GPIO is plugged into BCM pin 07 (GPIO pin 26)
GPIO.setup(GPIO_PIR,GPIO.IN) # Set pin as input
Current_Motion = 0 #the current state of motion detection
Previous_Motion = 0 #the previous state
birdname = "goldfinch"
VIDEO_PATH = Path("/media/pi/ASTEYES/sensor_py_birds.mp4")
player = OMXPlayer(VIDEO_PATH)
''' The following variables need to be changed from one
program to the next. They are the times when each event happens
in the videos.'''
eyeclosed = 2 #eyeclosed
eyeopen = 102 #eyeopen
current_mode = 0 #determines which mode the player is in
motion_detect_bool = False #boolean for detecting motion
def eyeclosed_mode():
player.set_position(eyeclosed)
print("player seeked to " + str(player.position()) + "for eyeclosed")
def eyeopen_mode():
player.set_position(eyeopen)
print("player seeked to " + str(player.position()) + "for eyeopen")
def check_motion_sensor(Current_Motion, Previous_Motion):
Current_Motion = GPIO.input(GPIO_PIR) # Read PIR state
time.sleep(1)
if Current_Motion == 1 and Previous_Motion == 0:
Previous_Motion = 1
return True
elif Current_Motion == 0 and Previous_Motion == 1:
Previous_Motion = 0
return False
try:
player.play()
while GPIO.input(GPIO_PIR) == 1: #loop while the motion sensor is setting up and still reading high
print ("Waiting for PIR to settle ...")
time.sleep(2)
print (" Ready")
current_time = int(time.time())
while True:
motion_sensor_reading = GPIO.input(GPIO_PIR)
if current_mode == 0:
player.play()
next_mode = 1
if current_mode == 1:
motion_detect_bool = check_motion_sensor(Current_Motion, Previous_Motion)
if motion_detect_bool == True:
print("still motioned!")
next_mode = 1
elif motion_detect_bool == False:
if player.position() > eyeopen-2: #loops the eyeopen mode before eyeclosed happens
eyeopen()
if current_mode == 1:
motion_detect_bool = check_motion_sensor(Current_Motion, Previous_Motion)
if motion_detect_bool == True:
print("still motioned!")
if int(player.position()) > eyeopen+5:
eyeopen_mode()
elif motion_detect_bool == False:
print("no more motion!")
next_mode = 0
if next_mode is not None:
current_mode = next_mode #changes mode
time.sleep(5) #sleep for 5 seconds
print("current mode is " + str(current_mode))
print("motion detect reading is " + str(motion_sonsor_reading))
print("current time is " + str(current_time))
except KeyboardInterrupt:
print (" Quit")
# Reset GPIO settings
GPIO.cleanup()