Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learn to use thread
#1
Hello, I am experiment with thread on the Pi. I want to keep the main thread for the UI. Start a new threat to turn a relay on and off. The part for the relay tread is:
import threading, time 
#from tkinter import *
from time import sleep
#Prepare GPIO
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) #disable annoying warning messages
GPIO.setup(40,GPIO.OUT)
#initially is off
GPIO.output(40,GPIO.LOW)

def foo():
  print ("Hello threading!")
  
def timed_on_off():
	GPIO.output(40,GPIO.HIGH)
	sleep(4)
	GPIO.output()
	sleep(4)

my_thread = threading.Thread(target = timed_on_off)
When I run it in Thonny, it doean't give me any error but neighter functions foo() nor timed_on_off() does anything. What am I doing wrong ? Thanks.
Reply
#2
You should start your thread: my_thread.start()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,390 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020