ฝากพี่ๆ ดูหน่อยค่ะ
import RPi.GPIO as GPIO
import time
LED_pin = 5
input_pin = 27
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_pin, GPIO.OUT)
GPIO.setup(input_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
LED_state = False
def toggle_LED(channel):
global LED_state
LED_state = not LED_state
GPIO.output(LED_pin, LED_state)
GPIO.add_event_detect(input_pin, GPIO.FALLING, callback=toggle_LED, bouncetime=300)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()
ขออนุญาติฝากข้อสอบค่ะพี่ๆ
import RPi.GPIO as GPIO
import time
LED_pin = 5
input_pin = 27
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_pin, GPIO.OUT)
GPIO.setup(input_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
LED_state = False
def toggle_LED(channel):
global LED_state
LED_state = not LED_state
GPIO.output(LED_pin, LED_state)
GPIO.add_event_detect(input_pin, GPIO.FALLING, callback=toggle_LED, bouncetime=300)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()