简体   繁体   中英

Python code for 'Distance Sensor' and 'Relay' Raspberry Pi

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM