text: python - How to properly implement threading within a function toggled by a button - Raspberry Pi Stack Exchange
  I'm trying to make a threaded function which will make my LEDs blink randomly as long as the button is pushed. when it is released it should exit the function and turn all LEDs off.
  When I run the script below I get indent errors, but have no clue on where it goes wrong. All threaded function examples I find are either far more complex or not related to changing input/interaction.
  Can anyone tell me where my indent goes wrong?
  from gpiozero import Button, LEDBoard
  leds = LEDBoard(yellow=16, green=19, red=20, blue=21)
  button = Button(12)
  def ledrandom(self):
  leds[x].on()
  leds[y].on()
  leds[x].off()
  leds[y].off()
  t = Thread(target=ledrandom) # Create thread
  t.start() # Start thread
  button.when_pressed = pushed
  There is nothing wrong with the indenting as presented. Perhaps your local copy has a mixture of tabs and spaces? – joan Aug 14 '18 at 15:12
  you are right Joan, the indents itself was a local editor issue (tab/spaces got mixed up), – nixx Aug 15 '18 at 10:24
  should give you a line number of where it is having an indent error. The issue with your threading is that you dont have a way to stop the thread. For a simple case like this, could use a global variable like so...
  This way you signal the running thread to stop, which will let it turn off the LED's and return from its function. The way you had it, you will keep launching threads that can never finish.
  For a more advanced way of handling threads, look into the Queue module, this allows you to send massages back and forth between threads.
  shouldRun=False  #global variable to let the thread know when it should stop
  def ledrandom():
  global shouldRun
  while shouldRun :
  leds.off() #turn them off now that shouldRun is False
  edited Aug 15 '18 at 15:34
  Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/usr/lib/python2.7/threading.py", line 754, in run self.__target(*self.__args, self.__kwargs) **TypeError: ledrandom() takes exactly 1 argument (0 given) thanks for thinking along Chad, somehow the error above turns up when I use your code. I assume this is because the shouldRun variable doesn't reach the ledrandom function? but have no idea why not? – nixx Aug 15 '18 at 10:31
  ya, the 'self' shouldn't have been there, I didn't notice it when I copied your original code. I edited my answer to reflect that. Glad you got it working – Chad G Aug 15 '18 at 15:33
  Ok, I found a working solution as shown below. the self argument in the ledrandom function got it all confused. this works perfectly now
  print ("leds on")
  while ShouldRun:
  time.sleep(.0)
  leds.off()      #turn them off now that ShouldRun is False
  print ("leds off")
  Not the answer you're looking for? Browse other questions tagged python led input-device process or ask your own question.
metadata.key: website_description
metadata.type: global
metadata.value: Stack Exchange is a network of question-and-answer (Q&A) websites on topics in diverse fields, each site covering a specific topic, where questions, answers, and users are subject to a reputation award process. The reputation system allows the sites to be self-moderating.