Kapitel 15 GPIO
This commit is contained in:
37
teil15/button.py
Executable file
37
teil15/button.py
Executable file
@@ -0,0 +1,37 @@
|
||||
#! /usr/bin/python3
|
||||
import RPi.GPIO as GPIO
|
||||
from time import sleep
|
||||
import logging
|
||||
|
||||
|
||||
__PIN__ = 16 # GPIO Pin, für den Taster
|
||||
__WAIT__ = 0.5 # Warten für 0,5 Sekunden
|
||||
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
||||
|
||||
if __name__ =='__main__':
|
||||
GPIO.setwarnings(False)
|
||||
# benutze Broadcom Pin Nummerierung
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
logging.debug(f'konfiguriere Pin ${__PIN__} als Input.')
|
||||
# Pin 16 als Input mit Pull-Up-Widerstand schalten.
|
||||
GPIO.setup(__PIN__, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
pressed = False
|
||||
|
||||
logging.debug('Start der Schleife.')
|
||||
try:
|
||||
while True:
|
||||
if not GPIO.input(__PIN__):
|
||||
if not pressed:
|
||||
logging.info('Button wurde gedrückt.')
|
||||
pressed = True
|
||||
else:
|
||||
logging.debug('Button nicht gedrückt.')
|
||||
pressed = False
|
||||
|
||||
sleep(__WAIT__)
|
||||
except KeyboardInterrupt:
|
||||
logging.debug('Abbruch durch Benutzer.')
|
||||
finally:
|
||||
pass
|
||||
|
Reference in New Issue
Block a user