raspberry pi - Python GPIO add_event_detect each state individually -
i have few lever-type on off switch have status printed switches on/off independantly off other switches.
so far, have come far:
import rpi.gpio gpio time import sleep gpio.setmode(gpio.bcm) gpio.setup(7, gpio.in) # switch 2 gpio.setup(11, gpio.in) # switch 3 def print_func(pin): if gpio.input(7) == 0: print "switch 2 on" elif gpio.input(7) == 1: print "switch 2 off" elif gpio.input(11) == 0: print "switch 3 on" elif gpio.input(11) == 1: print "switch 3 off" gpio.add_event_detect(7, gpio.both, callback=print_func, bouncetime=300) gpio.add_event_detect(11, gpio.both, callback=print_func, bouncetime=300) while true: sleep(1)
however, doesn't me anywhere. can't figure out how mentioned status of lever move, without going through loop mentioning status each one..
any appreciated!
i don't have raspberry-pi on me right can't test this, i'm pretty sure following need.
lever_num_by_pin = [7: 2, 11: 3] def printon(pin): print "switch", lever_num_by_pin[pin], "on" def printoff(pin): print "switch", lever_num_by_pin[pin], "off" pin in lever_num_by_pin: gpio.add_event_detect(pin, gpio.rising, callback=printon, bouncetime=300) gpio.add_event_detect(pin, gpio.falling, callback=printoff, bouncetime=300)
callbacks called argument of channel received input from. can use selectively print out lever number based on dictionary of pins number. additionally, can use dictionary's keys way of iterating through pins levers , attach rising , falling event on each. rising turning on, , falling turning off.
Comments
Post a Comment