multithreading - Thread doesn't run like I say it should with GPIOs in Python on RPi -


i creating script on raspberry pi if press button led on button need flashing till press button again. code:

#!/usr/bin/python import rpi.gpio gpio import threading time import sleep  gpio.setmode(gpio.bcm) pushbutton = 2 led = 3 gpio.setup(pushbutton, gpio.in) gpio.setup(led, gpio.out)  class rpithread(threading.thread):         def __init__(self):                 self.running = false                 super(rpithread, self).__init__()         def start(self):                 self.running = true                 super(rpithread, self).start()         def run(self):                 self.running = true                 while (self.running == true):                         gpio.output(led, gpio.high)                         print "high"                         sleep(0.05)                         gpio.output(led,gpio.low)                         print "low"                         sleep(0.05)         def stop(self):                 self.running = false                 gpio.output(led, gpio.low)  def main():         myrpithread = rpithread()         pushed = gpio.input(pushbutton)         try:                 while(true):                         if(pushed == false):                                 if(gpio.input(pushbutton) == false):                                         sleep(0.5)                                         if(gpio.input(pushbutton) == false):                                                 myrpithread.start()                                                 pushed = true                                                 print "the button pushed"                         else:                                 if(gpio.input(pushbutton) == true):                                         gpio.output(led, gpio.low)                                         myrpithread.stop()                                         pushed = false                                         print "the button not pushed"         except keyboardinterrupt:                 print "quit" main() 

always when run script leds arn't flashing each 0.05 sec. takes 2 seconds before turns on , sometime doesn't flash. dont know doing wrong? can please me figure out problem is?

is possible gpio pins not made use in multithreading?

i followed tutorial when needed use threaded callbacks gpio: how use interrupts python on raspberry pi , rpi gpio. has discussion in comments how avoid using global variables pass value threads.

it helpful know how have button wired, , type of button using answer problem more specifically.

i wrote following example code assuming have button wired gpio 2 +3.3v if have wired ground use "pull_up_down=gpio.pud_down" , "gpio.falling" assumes have momentary push button. when button push detected flips global boolean value. value checked in main loop, , blink(led) function called if pushed = true.

import rpi.gpio gpio time import sleep  gpio.setmode(gpio.bcm) pushbutton = 2 led = 3 gpio.setup(pushbutton, gpio.in, pull_up_down=gpio.pud_down) gpio.setup(led, gpio.out) pushed = false  # run in thread when button pushed  def button_callback(channel):       pushed = not pushed  gpio.add_event_detect(pushbutton, gpio.rising, callback=button_callback)  while true:     if pushed:         blink(led)  def blink(led):     gpio.output(led, true)     sleep(0.5)     gpio.output(led, false) 

i don't have raspberry pi in front of me right test code, check later. may have modify specific set up, gets in right direction.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -