2

I want to control some L.E.D's through the GPIO ports depending on the value of "variable1" which will become a raw input. The problem is, if variable1==70 then both led 12 and 16 with light, but only 12 should be! when variable1==69 only led 16 will light, which is correct, if it is less than the "if" listed, both led will quickly flash once. I need to contain the code so it only runs on the single if. Can anyone advise what am I doing incorrect?

Thanks very much!

import.RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)

variable1="70"

if variable1=="71":
    print 'Too high'

elif variable1=="70":
    print 'warning'
    GPIO.output(12,1)
    time.sleep(3)

elif variable1=="69":
    print 'OK'
    GPIO.output(16,1)
    time.sleep(3)
else:
    print 'fine'
GPIO.cleanup()
1

I'd suggest removing the new lines between the elifs. My version of python seems to not like that. What you may be seeing is the fact that you never turn the LED off after you turn it on. So the next time you run the program with a different number, it's still lit from before, making it look like both have responded when in fact they haven't. The solution is to reset the LEDs to a known state every time the program starts:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)

# reset the LEDs to off!
GPIO.output(12,GPIO.LOW)
GPIO.output(16,GPIO.LOW)

variable1="70"

if variable1=="71":
    print 'Too high'
elif variable1=="70":
    print 'warning'
    GPIO.output(12,GPIO.HIGH)
    time.sleep(3)
elif variable1=="69":
    print 'OK'
    GPIO.output(16,GPIO.HIGH)
    time.sleep(3)
else:
    print 'fine'

GPIO.cleanup()
0

I think this is a hardware wiring problem, as the python code seems to be correct.

Are you keeping track of the direction of the (l.e.)diodes?

Can you show us your wiring (in a picture preferably, or asciiart?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.