Bitwait or Wait Until

This is the place for queries that don't fit in any of the other categories.

Bitwait or Wait Until

Postby winda » Thu Apr 04, 2013 3:31 am

Hi, everyone...
I'm beginner in python.
I want to make python can function like "bitwait" instruction in BASCOM AVR or "wait..until.."
can python do that?
I'll use this instruction for counter object with ultrasonic sensor SRF04 connected with raspberry, that must set and reset in certain condition for count the object.
here is my coding:

Code: Select all
#!/usr/bin/python

# Measure distance using an ultrasonic module
# in a loop.

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

# -----------------------
# Define some functions
# -----------------------

def measure():
  # This function measures a distance

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

elapsed = stop-start
  distance = (elapsed * 34300)/2

  return distance

def measure_average():
  # This function takes 3 measurements and
  # returns the average.

  distance1=measure()
  time.sleep(0.1)
  distance2=measure()
  time.sleep(0.1)
  distance3=measure()
  distance = distance1 + distance2 + distance3
  distance = distance / 3
  return distance

# -----------------------
# Main Script
# -----------------------

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

# Define GPIO to use on Pi
GPIO_TRIGGER = 23         #GPIO_TRIGGER = 23 is in pin 16
GPIO_ECHO    = 24         #GPIO_ECHO    = 24 is in pin 18

print "Ultrasonic Measurement"

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

# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.

try:
a=a+1
while True:

    distance = measure_average()
    print "Distance : %.1f" % distance
    time.sleep(1)

    if distance < 15:
       a=a+1
       print "counter: " , a
       time.sleep(0.5)

except KeyboardInterrupt:
  # User pressed CTRL-C
  # Reset GPIO settings
  GPIO.cleanup()



code that i must use in bitwait or wait until is in :
Code: Select all
 if distance < 15:
       a=a+1
       print "counter: " , a
       time.sleep(0.5)


because " time.sleep(0.5)" can't make the counter set and reset automatically. "time.sleep(0.5)" just make the sensor counting every receiver got the signal from transmitter, whereas signal that reflection from transmitter is signal from the same object (object is always moving by conveyor).
got it?
Can u help me?
Thankyou very much :D
winda
 
Posts: 1
Joined: Thu Apr 04, 2013 3:01 am

Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 2 guests