Page 1 of 1

Help Pythone Code

Posted: Sat Apr 02, 2016 9:56 am
by RaspDebut
GPIO.setmode(GPIO.BCM)

TRIG = 23
ECHO = 24

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.0001)
GPIO.output(TRIG, False)

While GPIO.input(ECHO)==0 :
pulse_start = time.time()

While GPIO.input(ECHO)==1 :
pulse_end = time.time()

pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance , 2)
print "distance :",distance, "cm"

while(True)
{
GPIO.output(TRIG, False)
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.0001)
GPIO.output(TRIG, False)
While GPIO.input(ECHO)==0 :
pulse_start = time.time()
While GPIO.input(ECHO)==1 :
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance2 = pulse_duration * 17150
distance2 = round(distance , 2)

while (distance2 != distance)
{ startvideo}
if(distance2 = distance)
{ Stopvideo }
}
GPIO.cleanup()

Please verifie if this code is good.

Re: Help Pythone Code

Posted: Sat Apr 02, 2016 3:32 pm
by TrapdoorSmoke
What is the code for?

Re: Help Pythone Code

Posted: Sat Apr 02, 2016 3:53 pm
by davidcoton
Please edit your post to include

Code: Select all

 ... 
tags. That way we can see the code with its indentations, which are important in Python.

Re: Help Pythone Code

Posted: Sat Apr 02, 2016 5:51 pm
by Heater
What is "Pythone" code?

I presume you mean "Python"

But that source code you posted is not any kind of Python.

It looks like C with all those curly brackets "{" and "}"

But then it looks like Python with the use of ":"

Either way nobody can help, as much as they like to, unless you say exactly what it is you expect your code to do. And you wrap it in code tags so that it is readable. Especially important if it is Python where white space changes the meaning of the code. (A stupid idea if ever there was one, by the way)

Re: Help Pythone Code

Posted: Sun Apr 03, 2016 6:29 am
by drice
Let me guess. You're using an HC-SR04 ultrasonic rangefinder module and you want to display a video any time an object is moved from its initial position.

Your code will not work at all for the following reasons:
  • It is not syntactically correct. Not even close.
  • It contains obvious logic errors.
  • Python is too slow to reliably handle the tight timing tolerances used by that module.
  • The best solution would be to use a microcontroller to gather data from the module and then send the range information to the Pi using SPI. It's not really appropriate to interface with a timing-sensitive device like the HC-SR04 from an operating system.
All of the information you need to complete this project is available on the Internet. If this is a school project, I advise you to contact the teacher or professor immediately, as you are completely off the rails at this point.

Re: Help Pythone Code

Posted: Sun Apr 03, 2016 8:00 am
by DougieLawson
drice wrote: Your code will not work at all for the following reasons:
  • Python is too slow to reliably handle the tight timing tolerances used by that module.
That is 100% wrong and total nonsense.

http://www.modmypi.com/blog/hc-sr04-ult ... spberry-pi
https://www.modmypi.com/download/range_sensor.py (you can download that python sample with wget or curl)

Re: Help Pythone Code

Posted: Sun Apr 03, 2016 8:51 am
by gordon77
Tidied it up a bit and added the record video, assuming you want to use picamera. I have no way of testing it.

Code: Select all

import time
import picamera
import RPi.GPIO as GPIO

cam=picamera.PiCamera()

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

TRIG = 23
ECHO = 24

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.0001)
GPIO.output(TRIG, False)
stop = 0
while GPIO.input(ECHO)==0 :
   pulse_start = time.time()

while GPIO.input(ECHO)==1 :
   pulse_end = time.time()

pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance , 2)
print "distance :",distance, "cm"

while stop = 0:

   GPIO.output(TRIG, False)
   time.sleep(2)
   GPIO.output(TRIG, True)
   time.sleep(0.0001)
   GPIO.output(TRIG, False)
   while GPIO.input(ECHO)==0 :
      pulse_start = time.time()
   while GPIO.input(ECHO)==1 :
      pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance2 = pulse_duration * 17150
distance2 = round(distance , 2)

while (distance2 != distance):
   #cam.start_preview()
   cam.start_recording('video.h264')
   if(distance2 = distance)
      #cam.stop_preview()
      cam.stop_recording()
      stop = 1

GPIO.cleanup()