carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Play video file when someone sit

Sat Feb 02, 2019 9:46 pm

Hi there,
I am a beginner and
I want to make play control system by using pressure sensor.
In general situation, only sample1 video is played but
If someone sit on the chair, which is built pressure sensor, play sample2 video.
To make this system, I built below code, but when I run the code, only sample1 video was played for a second, and suddenly my
RPI is shutdown. How can I fix this problem? If you have any suggestions, I would greatly appreciate it! Thanks!

Code: Select all

 


import spidev, time, sys, os
from subprocess import Popen
import subprocess as sp


spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1000000

def analog_read(channel):
    r = spi.xfer2([1, (0x08 + channel) << 4, 0])
    adc_out = ((r[1]&0x03) << 8) + r[2]
    return adc_out

video1 = ('/home/pi/Videos/sample1.mp4')
video2 = ('/home/pi/Videos/sample2.mp4')

while True:
    reading = analog_read(1)
    time.sleep(1)
    if (reading <= 20):
        zone = 1
    elif (reading > 20):
        zone = 2
    if (zone == 1):
        Popen(['omxplayer', '-b', video1])
    if (zone == 2):
        Popen(['omxplayer', '-b', video2])

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Play video file when someone sit

Sun Feb 03, 2019 12:37 pm

Hi.

Before anyone can help you need to supply more information for example.

model of pi
power supply used
which OS you are running
details of the sensor your are using
ADC chip you are using
a diagram of how all the parts pi, ADC & sensor are connected.

and finally elaborate on what you mean by " suddenly my RPI is shutdown. " does the program just stop running or does the pi physically power off or reboot etc.......
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

danjperron
Posts: 3508
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Play video file when someone sit

Sun Feb 03, 2019 1:37 pm

'-b' gives me problem with the new update.

Could you please use this line instead and tell me if it works?

Popen(['/usr/bin/omxplayer', '--display', '0', video1]


Also in your loop you should check if the video is already started otherwise you will rerun the same video every second.

User avatar
rpiMike
Posts: 1391
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Play video file when someone sit

Sun Feb 03, 2019 2:02 pm

Does the following play successfully or crash your Pi?

omxplayer /home/pi/Videos/sample1.mp4

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: Play video file when someone sit

Sun Feb 03, 2019 11:18 pm

pcmanbob wrote:
Sun Feb 03, 2019 12:37 pm
Hi.

Before anyone can help you need to supply more information for example.

model of pi
power supply used
which OS you are running
details of the sensor your are using
ADC chip you are using
a diagram of how all the parts pi, ADC & sensor are connected.

and finally elaborate on what you mean by " suddenly my RPI is shutdown. " does the program just stop running or does the pi physically power off or reboot etc.......
I am so sorry, and thank you for answering my question!
I use Raspberry 3 PI b+ model which is installed with Raspbian OS.
My pressure sensor is FSR-406 with MCP3008 ADC, and I connected these components based on below diagram
https://es.mathworks.com/help/supportpk ... meter.html

And using this 'Popen(['/usr/bin/omxplayer', '--display', '0', video1]' instead of my past code, now my PI's stop running and power off problem is solved but until now, I have a problem.
My problem is that when input value is over 20, the video2 is played but when input value is again back to under 20, the video1 is not played and just keep playing video2. Even when video2 is end, my program stop running, so could you give some advise or solution for this problem? thanks !

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: Play video file when someone sit

Sun Feb 03, 2019 11:21 pm

danjperron wrote:
Sun Feb 03, 2019 1:37 pm
'-b' gives me problem with the new update.

Could you please use this line instead and tell me if it works?

Popen(['/usr/bin/omxplayer', '--display', '0', video1]


Also in your loop you should check if the video is already started otherwise you will rerun the same video every second.
Thank you so much! your code fix my problem. Now the two video samples are played, but when input value is over 20, the video2 is played but when input value is again back to under 20, the video1 is not played and just keep playing video2. Even when video2 is end, my program stop running, so could you give some advise or solution for this problem? thanks !

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: Play video file when someone sit

Sun Feb 03, 2019 11:26 pm

rpiMike wrote:
Sun Feb 03, 2019 2:02 pm
Does the following play successfully or crash your Pi?

omxplayer /home/pi/Videos/sample1.mp4
Thank you for answering my question! Now I can play these two video samples with helpful advise, but until I have some problem in playing part. For example, when input value is over 20, the video2 is played but when input value is again back to under 20, the video1 is not played and just keep playing video2. Even when video2 is end, my program stop running, so could you give some advise or solution for this problem? thanks !

Return to “Graphics, sound and multimedia”