joemillervi
joemillervi

Reputation: 1067

thread.start_new_thread not working

I'm attempting to make these GPIO ports turn on and off simultaneously, but at different intervals on a RPi. I can run one of the loops and it works but when I bring in the threading it does not.

import RPi.GPIO as GPIO  
from time import sleep  
import thread

GPIO.setmode(GPIO.BOARD)  

GPIO.setup(11, GPIO.OUT)  
GPIO.setup(13, GPIO.OUT)  
GPIO.setup(15, GPIO.OUT)  

def fast():  
    while True:  
        GPIO.output(11, True)  
        sleep(.02)  
        GPIO.output(11, False)  
        sleep(.02)  

def med():
    while True:
        GPIO.output(13, True)
        sleep(.2)
        GPIO.output(13, False)
        sleep(.2)

def slow():
    while True:
        GPIO.output(15, True)
        sleep(2)
        GPIO.output(15, False)
        sleep(2)

thread.start_new_thread(fast,())
thread.start_new_thread(med,())
thread.start_new_thread(slow,())

Upvotes: 0

Views: 2340

Answers (1)

chris
chris

Reputation: 4996

It's because there is no main program/loop. Your code starts those threads, but then it will come to the end of the code and exit the process running python, killing the threads. So maybe add a raw_input("Press enter to exit") at the bottom.

Upvotes: 6

Related Questions