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

# 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, 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.

  distance = distance1 + distance2 + distance3
  distance = distance / 3
  return distance

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

# Use BCM GPIO references
# instead of physical pin numbers

# 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_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.

while True:

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

    if distance < 15:
       print "counter: " , a

except KeyboardInterrupt:
  # User pressed CTRL-C
  # Reset GPIO settings

code that i must use in bitwait or wait until is in :
Code: Select all
 if distance < 15:
       print "counter: " , a

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
Posts: 1
Joined: Thu Apr 04, 2013 3:01 am

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests