ltlklr
Posts: 5
Joined: Wed Mar 13, 2019 2:45 pm

Playing a video with a ultrasonic senzor

Sun Mar 24, 2019 11:59 am

Hi,
I need to play a video with a ultrasonic sensor.
But I also have a button to play the second video and a button to stop the video.
I'm using omxplayer.
I already have code to use buttons, but I don't have the code for the ultrasonic sensor
Here is my current code:

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import os
import sys
from subprocess import Popen
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Playaj edukativni video
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)

movie1 = ("/home/pi/Ja.mp4")
movie2 = ("/home/pi/Ja2.mp4")

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True
quit_video = True

player = False

while True:
    #Read states of inputs
    input_state1 = GPIO.input(17)

    quit_video = GPIO.input(27)

    #If GPIO(17) is shorted to Ground
    if input_state1 != last_state1:
        if (player and not input_state1):
            os.system('killall omxplayer.bin')
            omxc = Popen(['omxplayer', '-b', movie1])
            player = True
        elif not input_state1:
            omxc = Popen(['omxplayer', '-b', movie1])
            player = True

    
    #GPIO(27) to close omxplayer manually - used during debug
    if quit_video == False:
        os.system('killall omxplayer.bin')
        player = False

    #Set last_input states
    last_state1 = input_state1
    last_state2 = input_state2
[moderator: please next time add your code tags yourself!]

User avatar
B.Goode
Posts: 13218
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Playing a video with a ultrasonic senzor

Sun Mar 24, 2019 12:14 pm

I don't have the code for the ultrasonic sensor


Maybe there is more than one model of ultrasonic sensor? It would help to be specific.


The gpiozero Python library has support for the common hc-sr04 sensor - https://gpiozero.readthedocs.io/en/stab ... or-hc-sr04

There are many other examples of using this sensor available: many are poorly implemented and unreliable.

ltlklr
Posts: 5
Joined: Wed Mar 13, 2019 2:45 pm

Re: Playing a video with a ultrasonic senzor

Sun Mar 24, 2019 12:23 pm

I have the HC-sr04

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

Re: Playing a video with a ultrasonic senzor

Mon Mar 25, 2019 2:34 pm

To connect your HC-SR04 ( assuming 5v version of sensor ) to the pi you need to level shift the 5v output of the sensor down to the 3.3v required by the pi gpio using 2 resistors connected like this.

Image

then this code will trigger the sensor and read the range returned.

Code: Select all

#!/usr/bin/python

# Import required Python libraries
import time
import RPi.GPIO as GPIO

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO    = 24


# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

print "Ultrasonic Measurement"

# Allow module to settle
time.sleep(0.5)

# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()

while GPIO.input(GPIO_ECHO)==0:
  start = time.time()

while GPIO.input(GPIO_ECHO)==1:
  stop = time.time()

# Calculate pulse length
elapsed = stop-start

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distancet = elapsed * 34300

# That was the distance there and back so halve the value
distance = distancet / 2

print "Distance :", distance

print "Elaspsed time :", elapsed


# Reset GPIO settings
GPIO.cleanup()
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Beginners”