Ciber
Posts: 4
Joined: Sun Jan 28, 2018 8:58 am

Why is this incorrect?

Sun Jan 28, 2018 9:26 am

Code: Select all

import RPi.GPIO as GPIO    #Importamos la librería GPIO
import time                #Importamos time (time.sleep)
GPIO.setmode(GPIO.BCM)     #Ponemos la placa en modo BCM
GPIO_TRIGGER = 11          #Usamos el pin GPIO 25 como TRIGGER
GPIO_ECHO    = 7           #Usamos el pin GPIO 7 como ECHO
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  #Configuramos Trigger como salida
GPIO.setup(GPIO_ECHO,GPIO.IN)      #Configuramos Echo como entrada
GPIO.output(GPIO_TRIGGER,False)    #Ponemos el pin 25 como LOW
GPIO.setup(1, GPIO.OUT)


while True:     #Iniciamos un loop infinito
    GPIO.output(GPIO_TRIGGER,True)   #Enviamos un pulso de ultrasonidos
    time.sleep(0.00001)              #Una pequeñña pausa
    GPIO.output(GPIO_TRIGGER,False)  #Apagamos el pulso
    start = time.time()              #Guarda el tiempo actual mediante time.time()
    while GPIO.input(GPIO_ECHO)==0:  #Mientras el sensor no reciba señal...
        start = time.time()          #Mantenemos el tiempo actual mediante time.time()
    while GPIO.input(GPIO_ECHO)==1:  #Si el sensor recibe señal...
        stop = time.time()           #Guarda el tiempo actual mediante time.time()        elapsed = stop-start             #Obtenemos el tiempo transcurrido entre envío
    distance = (elapsed * 34300)/2   #D = (T x V)/2
    time.sleep(1)                    #Pequeña pausa para no saturar el procesador
    print distance


    if Distance <= 0.001				#here it says invalid syntax
            MakeSound = 1
            SetSound = 1
    elif Distance <= 0.0005
        SetSound = 2
    elif Distance <= 0.00025
        SetSound = 3
    elif Distance <= 0.0001
        SetSound = 4
    else Distance > 0.001
        MakeSound = 0

    if MakeSound = 1
        if SetSound = 10
            GPIO.output (1, True)
            sleep (5)
            GPIO.output (1, False)
            sleep (5)
        if SetSound = 2
            GPIO.output (1, True)
            sleep (3)
            GPIO.output (1, False)
            sleep (3)
        if SetSound = 3
            GPIO.output (1, True)
            sleep(2)
            GPIO.output (1, False)
            sleep (2)-
        if SetSound = 4
            GPIO.output (1, True)
Ok, so where I put that my code is going wrong, what is it, I can't understand what is wrong. can someone help me?

beta-tester
Posts: 1207
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: Why is this incorrect?

Sun Jan 28, 2018 1:59 pm

assuming you are talkong about python...
you missed a collon ':'

Code: Select all

if expression1:
   statement(s)
elif expression2:
   statement(s)
elif expression3:
   statement(s)
else:
   statement(s)
https://www.tutorialspoint.com/python/p ... f_else.htm

but hey... is it so complicated for you to start web searching by yourself for that general questions?
(ok, you are new here... Welcome)
{ I only give negative feedback }
RPi Model B (rev1, 256MB) & B (rev2, 512MB) & B+, RPi2B (1GB), 64GB microSDXC1 class 10, HDMI 1920x1080, keyboard-mouse-combo (wireless), PiCamera, ethernet-cable, 5V/1.2A power supply, Wifi dongle (rt5370)

User avatar
Paeryn
Posts: 2055
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Why is this incorrect?

Sun Jan 28, 2018 5:37 pm

The order of the if Distance... tests is wrong, all the elif clauses will be ignored since they are only True if the first if clause is also True, and if that is True then the elif and else parts are skipped.

Also you don't put a test after the else:, that block is for every case that hasn't been handled by the matching if and elif clauses.

If Distance was 0.0001 the first tests for Distance <= 0.001 is True so the block will set MakeSound=1 and SetSound=1 and all the elif clauses will not be checked even though the last one looks to be the one you want. You need to start with the smallest test first and work your way up in this instance.
She who travels light — forgot something.

fbe
Posts: 279
Joined: Thu Aug 17, 2017 9:08 pm

Re: Why is this incorrect?

Sun Jan 28, 2018 7:06 pm

  • distance is not Distance
  • in "if MakeSound = 1:" = is an assignment operator the comparision operator is ==
  • Make sure that MakeSound is defined before it is checked when you reorder your code. You can't guess the value of Distance when the loop body is executed for the first time.
  • Don't assume that the distance can't change from value <= 0.0001 to value > 0.001 between two loops.

Return to “General discussion”

Who is online

Users browsing this forum: jamesh and 47 guests