1

I'm a noobie with Python3 and am having problems getting (2) LEDs to blink. The threads run one after the other and not concurrently.

'''
    Python3 - gpio_test_02
            - use threads to blink leds from GPIO pins
'''
import threading
import time
import RPi.GPIO as GPIO

# Set GPIO mode
GPIO.setmode(GPIO.BCM)
print("GPIO mode: BCM {}".format(GPIO.getmode()))

# Class definition
class Blink_LED(threading.Thread):
    def __init__(self, pin, counter, delay, name):
        threading.Thread.__init__(self)
        self.pin     = pin
        self.counter = counter
        self.delay   = delay
        self.name    = name
        print("New class - {} {} {} {}".format(self.name, self.pin, self.counter, self.delay))

    def run(self):
        try:
            GPIO.setup(self.pin, GPIO.OUT)
            i = 0
            while i <= self.counter:
                print(i, "", self.pin)
                GPIO.output(self.pin, GPIO.HIGH)
                time.sleep(self.delay)
                GPIO.output(self.pin, GPIO.LOW)
                time.sleep(self.delay)
                i += 1
        except:
            RuntimeWarning
# end class Blink_LED

def main():
    11# Run the threads
    T1 = Blink_LED(19, 10, 0.25, "T1")
    T2 = Blink_LED(13, 10, 0.25, "T2")

    T1.run()
    T2.run()

    T1.join()
    T2.join(T1)

    # cleaning up all GPIO
    GPIO.cleanup()
# end mIn

if __name__ == "__main__":
    main()
1

I would make the following modifications.

  1. After the line print("New class - {} {} {} {}".format(self.name, self.pin, self.counter, self.delay)) add the line self.start().
  2. Comment out the T1.run() and T2.run() lines.
  3. Change T2.join(T1) to T2.join().

Did you modify the example?

| improve this answer | |

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.