3

I am working on a Raspberry Pi project with two ultrasonic sensors. But now I have a problem with the python script. The print() function at the end always tells me 0.0cm distance even if I hover with my hand over the sensor If I unplug the Sensor on Pin 23 the other sensor on Pin 24 works correct with the correct distance

#Bibliotheken einbinden
import RPi.GPIO as GPIO
import time
from pykeyboard import PyKeyboard

#GPIO Modus (BOARD / BCM)
GPIO.setmode(GPIO.BCM)

#GPIO Pins zuweisen
GPIO_TRIGGER = 18
GPIO_ECHO_RECHTS = 24
GPIO_ECHO_LINKS = 23

#Richtung der GPIO-Pins festlegen (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO_RECHTS, GPIO.IN)
GPIO.setup(GPIO_ECHO_LINKS, GPIO.IN)


#Instanz von PyKeyBoard erstellen
k = PyKeyboard()

#Testausagbe
print("Script startet") 

def distanz():
# setze Trigger auf HIGH
GPIO.output(GPIO_TRIGGER, True)

# setze Trigger nach 0.01ms auf LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)

StartZeit_RECHTS = time.time()
StopZeit_RECHTS = time.time()

StartZeit_LINKS = time.time()
    StopZeit_LINKS = time.time()

# speichere Startzeit des rechten sensors
while GPIO.input(GPIO_ECHO_RECHTS) == 0 and GPIO.input(GPIO_ECHO_LINKS) == 0:
    StartZeit_RECHTS = time.time()
    StartZeit_LINKS = time.time()


# speichere Ankunftszeit des rechten sensors
while GPIO.input(GPIO_ECHO_RECHTS) == 1 or GPIO.input(GPIO_ECHO_LINKS) == 1:
    StopZeit_RECHTS = time.time()
    StopZeit_LINKS = time.time()




# Zeit Differenz zwischen Start und Ankunft des rechten sensors
TimeElapsed_RECHTS = StopZeit_RECHTS - StartZeit_RECHTS
# mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
# und durch 2 teilen, da hin und zurueck
distanz_RECHTS = (TimeElapsed_RECHTS * 34300) / 2

# Zeit Differenz zwischen Start und Ankunft des linken sensors
    TimeElapsed_LINKS = StopZeit_LINKS - StartZeit_LINKS
    # mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
    # und durch 2 teilen, da hin und zurueck
    distanz_LINKS = (TimeElapsed_LINKS * 34300) / 2


if distanz_RECHTS < 12.0 and distanz_RECHTS > 0.0:
    #Kommando um ein Tab weiter zu springen
    k.press_key(k.control_l_key)
    k.tap_key(k.tab_key)
    k.release_key(k.control_l_key)
    return distanz_RECHTS

elif distanz_LINKS < 12.0 and distanz_LINKS > 0.0:
    #Kommando um ein Tab zurueck zu springen
            k.press_key(k.control_l_key)
    k.press_key(k.shift_l_key)
            k.tap_key(k.tab_key)
            k.release_key(k.control_l_key)
    k.release_key(k.shift_l_key)
            return distanz_LINKS

else:
    return 0.0

if __name__ == '__main__':
try:
    while True:
        abstand = distanz()
        print ("Gemessene Entfernung = %.1f cm" % abstand)
        time.sleep(1)

    # Beim Abbruch durch STRG+C resetten
except KeyboardInterrupt:
    print("Messung vom User gestoppt")
    GPIO.cleanup()

This is the Script I'm programming on. I know there is something wrong with the while loops, and I already tried a lot of things but nothing worked. Both sensors should just detect how far there is something in front of them. If you don't understand something in the code, thats not important, it's just german.

Hope somebody could help me

WeSt
  • 889
  • 5
  • 14
  • 32

1 Answers1

0

I think you should split the second while loop. Its good to split both while loops. First while loop responds when both sensors send low signal. But the second while loop gets executed as soon as it receives a high signal in any of the sensors. It doesn't wait for the second sensor to respond.

So split the while loops to four while loops two for sending signal and two for receiving signal.

Vivek V C
  • 7
  • 5