2016-06-24 11 views
1

저는 Raspberry Pi와 Python의 멍청한 놈입니다. 5 개의 LED와 버튼이있는 간단한 신호등 시뮬레이터를 만들려고합니다.Raspberry Pi Python 버튼을 누르고 코드를 실행하십시오.

진정한 동안 : 버튼을 누르면하지 않는 경우

inputValue = GPIO.input(17) 
if (inputValue == False): #if the button was pushed 
    print("Button press ") 
else: #if it wasn't pressed 
    GPIO.output(green_traf_LED, GPIO.LOW) #green T. LED on 
    GPIO.output(red_walk_LED, GPIO.LOW) #red W. LED always on 
    time.sleep(6) 
    GPIO.output(green_traf_LED, GPIO.HIGH) #green T. LED off 
    #yellow blinking, red 
    for k in range(10): 
     #red walk LED still on 
     GPIO.output(yellow_traf_LED, GPIO.LOW) #yellow T. LED on 
     time.sleep(0.2) 
     GPIO.output(yellow_traf_LED, GPIO.HIGH) #yellow T. LED off 
     time.sleep(0.2) 
    #red, white 
    GPIO.output(red_traf_LED, GPIO.LOW) #red T. LED ON 
    time.sleep(6) 
    GPIO.output(red_traf_LED, GPIO.HIGH) #red T. LED off 
time.sleep(0.3) 

그래서 기본적으로, 나는 코드 X를 실행하는 내 파이썬 코드를 원하는 여기에 내 코드입니다. 버튼을 누르면 코드 y를 실행 한 다음 나중에 다시 누를 때까지 코드 x를 계속 실행합니다. 그러나 코드를 실행할 때 LED가 켜지지 않고 단추를 누르면 메시지가 나타나지 않습니다. 배선, LED 번호 매기기 및 버튼 번호 매기기가 정확하다는 것을 100 % 확신하므로 무엇을 고칠 필요가 있습니까?

+0

버튼을 눌렀을 때 올바른 값을 얻었습니까? –

+0

문제는 코드가 button_click에 대해 한 번만 확인한 후 전체 프로세스 동안 코드가 입력 이벤트를 더 이상 보지 않는 것입니다. –

답변

0

먼저 HIGHLOW을 거꾸로 처리 한 것 같습니다.

입력 포트를 수신하고 예를 들어 파일에 상태를 넣으려면 서비스/백그라운드 스크립트가 필요합니다. Node-RED를 설치하면 GPIO 포트를 모니터링 할 수 있으므로 서비스를 프로그래밍 할 필요가 없습니다.

import time 
#initialise a previous input variable to 0 (assume button not pressed last) 
prev_input = 0 
while True: 
    #take a reading 
    input = GPIO.input(17) 
    #if the last reading was low and this one high, print 
    if ((not prev_input) and input): 
     print("Button pressed") #put information in file or other... 
     #update previous input 
     prev_input = input 
     #slight pause to debounce 
     time.sleep(0.05) 

Source

는 그런 다음 위의 프로그램 대신 핀 값을 읽을 때, 파일 내용을 읽어 보시기 바랍니다.