Mon Jan 15, 2018 10:59 pm
I'm kind of a newbie here...and hardware is properly connected since I tried running codes separately and it worked without errors. The only problem I am facing is combining these 2 codes, so here are 2 codes I want to combine in such way so the led would turn on when a certain distance is detected by the ultrasonic sensor
Code for ultrasonic sensor:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
print("Distance Measurement In Progress")
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
def measure():
GPIO.output(TRIG, False)
print("Waiting For Sensor To Settle")
time.sleep(.5)
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)
print("Distance:",distance,"cm")
while True:
measure()
Code for LED diode:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
print("LED on")
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print("LED off")
GPIO.output(18,GPIO.LOW)
I would really appreciate any explanations on how to do this or something... Thanks.