2016-06-30 10 views
0

Ich versuche ein Python-Programm mit Raspberry Pi zu machen, dass, wenn ich einen Knopf drücke, nur die rote LED aufhellt, und wenn ich einen anderen Knopf drücke, erhellt nur die grüne LED. Wenn keine Tasten gedrückt werden, sind alle LEDs aus. Aber wenn ich es versuche, bleiben beide LEDs an, und wenn ich den Knopf drücke, sagt der Monitor, dass ich ihn gedrückt habe, aber nichts ändert sich. Was kann ich tun, um das zu beheben? Ich bin 100% sicher, dass es keine Verkabelungsprobleme gibt, und der Monitor zeigt keine Fehler. BTW, hier ist der Code:Raspberry PI: 2 Tasten, 2 LEDs

 import RPi.GPIO as GPIO 
     import time 

     GPIO.setwarnings(False) 
     red_walk_LED = 16 
     green_traf_LED = 15 
     Btn_one = 22 # pin12 --- button 
     Btn_two = 29 # pin29 --- 2nd button 
     # GLOBAL VARIABLES 
     red_Led_status = 1 
     Green_Led_status = 1 
     flag_btn_one_pushed = 0 
     flag_btn_two_pushed = 0 

     def all_leds_off(): 
      GPIO.output(green_traf_LED, GPIO.HIGH) 
      GPIO.output(yellow_traf_LED, GPIO.HIGH) 
      GPIO.output(red_traf_LED, GPIO.HIGH) 
      GPIO.output(red_walk_LED, GPIO.HIGH) 
      GPIO.output(white_walk_LED, GPIO.HIGH) 
    def setup(): 
     global green_LED_frequence 
     global red_LED_frequence 
     GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location 
     #set LEDs as outputs 
     GPIO.setup(green_traf_LED, GPIO.OUT) 
     GPIO.setup(red_traf_LED, GPIO.OUT) 
     GPIO.setup(Btn_one, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode as input, and pull up to high level(3.3V) 
     GPIO.setup(Btn_two, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
     red_LED_frequence = GPIO.PWM(red_traf_LED, 1000) # set Frequece to 1KHz 
     green_LED_frequence = GPIO.PWM(green_traf_LED, 1000) 
     red_LED_frequence.start(0) # Duty Cycle = 0 
     green_LED_frequence.start(0) 

def Btn_one_push(ev=None): 
    print('OK, the 1st button was pushed') 
    global red_Led_status #we are allowed to change these variables in this function 
    global my_counter 
    global flag_btn_one_pushed 
    global red_LED_frequence 

    red_Led_status  = not red_Led_status #change LED status 0-1 or 1-0 
    flag_btn_one_pushed = 1 

    my_delay = 0.2 
    GPIO.output(green_traf_LED, GPIO.HIGH) 
    if red_Led_status == 1: #1-on 
     print('ok, reds on') 
     for dc in range(0, 101, 4): # Increase duty cycle: 0~100 
      red_LED_frequence.ChangeDutyCycle(dc) # Change duty cycle 
      time.sleep(0.02) 
     for dc in range(100, -1, -4): # Decrease duty cycle: 100~0 
      red_LED_frequence.ChangeDutyCycle(dc) 
      time.sleep(0.02) 


    all_leds_off() #turn all LEDs off!! 
    flag_btn_pushed = 0 

def Btn_two_push(ev=None): 
    print('OK, the 2nd button was pushed') 
    global Green_Led_status 
    global flag_btn_two_pushed 
    global green_LED_frequence 

    Green_Led_status  = not Green_Led_status #change LED status 0-1 or 1-0 
    flag_btn_two_pushed = 1 

    my_delay = 0.2 
    GPIO.output(red_traf_LED, GPIO.HIGH) 
    if Green_Led_status == 1: #1-on 
     print('This is supposed to work!') 
     for dc in range(0, 101, 4): # Increase duty cycle: 0~100 
      print(green_LED_frequence) 
      green_LED_frequence.ChangeDutyCycle(dc) # Change duty cycle 
      time.sleep(0.08) 
     for dc in range(100, -1, -4): # Decrease duty cycle: 100~0 
      green_LED_frequence.ChangeDutyCycle(dc) 
      time.sleep(0.08)  
    all_leds_off() #turn all LEDs off!! 
    flag_btn_two_pushed = 0 





def loop(): 
    global flag_btn_one_pushed 
    global flag_btn_two_pushed 
    GPIO.add_event_detect(Btn_one, GPIO.FALLING, callback=Btn_one_push) # wait for change in GPIO 0-1 or 1-0 
    GPIO.add_event_detect(Btn_two, GPIO.FALLING, callback=Btn_two_push) 
    while True: #when the button isn't pushed, do this 
     all_leds_off() 
    else: 
     pass 

def destroy(): 
    all_leds_off() 
    GPIO.cleanup() # Release resource 

if __name__ == '__main__': # Program start from here 
    setup() 
    try: 
     loop() 
    except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 
     destroy() 

Antwort

0

Sie sollten versuchen, diese Zeilen zu entfernen:

while True: #when the button isn't pushed, do this 
    all_leds_off() 
else: 
    pass 

und ersetzt sie mit:

all_leds_off() 

Die Rückrufe jeden Tastendruck ausgelöst werden sollen.

Hier ist ein weiteres Beispiel zu betrachten:

https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=102631&p=760629

Außerdem haben Sie einige einfache Code-Schnipsel versucht, die LEDs ein- und ausgeschaltet werden einzeln drehen und die Taste drückt erkennen seine das ist kein zu beweisen Verkabelungsproblem? Es braucht nicht viel, um die Verkabelung falsch zu machen!

+0

Hmm, ich bin mir nicht sicher, ob das alles ist, um das Chaos zu beseitigen. Es muss etwas sein, das eine Schleife bildet, aber die Funktion loop() führt auch das Setup des Callback durch. – barny