1

I've given this a code a good go and got it working to a point. The first section of the code is for the distance sensor and seems to work fine, also the 'if' statement, when I get it to check the distance between 2cm and 30cm also seems to work fine by switching on a relay for 5 seconds, but what I would now like to do is have it switch on 2 relays for 5 seconds, not just the 1, but not sure how to add the second relay into the mix. Currently I have the Pi connected to a 4 relay board.

    import RPi.GPIO as GPIO            
import time                        
GPIO.setmode(GPIO.BCM)                

GPIO.setwarnings(False)

TRIG = 23                                  
ECHO = 24                                 

print "Distance measurement in progress"

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

while True:

  GPIO.output(TRIG, False)                
  print "Waitng For Sensor To Settle"
  time.sleep(2)                            

  GPIO.output(TRIG, True)                 
  time.sleep(0.00001)                     
  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)            

  if distance > 2 and distance < 400:      
    print "Distance:",distance - 0.5,"cm"  
  else:
    print "Out Of Range"                  

GPIO.setwarnings(False)

  if distance >2 and < 30:
pinList = [3]

for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
try:
  GPIO.output(3, GPIO.LOW)
  print "ON"
  time.sleep(5)
  GPIO.output(3, GPIO.HIGH)
  print "OFF"

except KeyboardInterrupt:
  print "  Quit"

  GPIO.cleanup()
1
  • if distance >2 and < 30: is not valid syntax, if you want to chain the expression it is if 2 < distance < 30: Commented Sep 26, 2015 at 21:06

1 Answer 1

1

It looks like you've cut together a number of different sources. One of the original scripts simply used a list (pinList) to iterate through - this original use seems to have got lost somewhere.

The following code should get you back on the right track. It's probably worth comparing what I've changed and doing some further reading. You'll need to change line 11 to match the pin number of the additional relay.

import RPi.GPIO as GPIO            
import time                        

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

TRIG = 23                                  
ECHO = 24                                 

# Add your relay output pins here:
pinList = [3, your_other_relaypin]

for pin in pinList: 
    GPIO.setup(pin, GPIO.OUT)

print "Distance measurement in progress"

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

try:
    while True:

        GPIO.output(TRIG, False)                
        print "Waitng For Sensor To Settle"
        time.sleep(2)                            

        GPIO.output(TRIG, True)                 
        time.sleep(0.00001)                     
        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)            

        if distance > 2 and distance < 400:      
            print "Distance: {distance}cm".format(distance=(distance - 0.5) )  
        else:
            print "Out Of Range"                  

        if distance >2 and distance < 30:

            for pin in pinList: 
                print "ON PIN {pin}".format(pin=pin)
                GPIO.output(pin, GPIO.LOW)

            time.sleep(5)

            for pin in pinList: 
                print "OFF PIN {pin}".format(pin=pin)
                GPIO.output(pin, GPIO.HIGH)

    except KeyboardInterrupt:
        print "  Quit"
        GPIO.cleanup()
Sign up to request clarification or add additional context in comments.

3 Comments

Alastair your a life saver, thank you, it works great. Yes this is a hybrid of two separate scripts but now I have your fix and my original code, this will give me a good comparison as to where I went wrong and clearly learn from this. Thanks again.
No worries. Remember to upvote and accept my answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.