RaspDebut
Posts: 1
Joined: Sat Apr 02, 2016 9:53 am

Help Pythone Code

Sat Apr 02, 2016 9:56 am

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.

TrapdoorSmoke
Posts: 81
Joined: Thu Nov 19, 2015 10:28 pm
Location: Scotland

Re: Help Pythone Code

Sat Apr 02, 2016 3:32 pm

What is the code for?

User avatar
davidcoton
Posts: 5028
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Help Pythone Code

Sat Apr 02, 2016 3:53 pm

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.
Signature retired

Heater
Posts: 15950
Joined: Tue Jul 17, 2012 3:02 pm

Re: Help Pythone Code

Sat Apr 02, 2016 5:51 pm

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)
Memory in C++ is a leaky abstraction .

drice
Posts: 23
Joined: Fri Dec 19, 2014 3:07 am

Re: Help Pythone Code

Sun Apr 03, 2016 6:29 am

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.

User avatar
DougieLawson
Posts: 39126
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Help Pythone Code

Sun Apr 03, 2016 8:00 am

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)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help Pythone Code

Sun Apr 03, 2016 8:51 am

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()

Return to “General discussion”