Files
piplayer/code/test/sensor_tcst2103.py
T
2020-06-03 20:10:51 +02:00

42 lines
908 B
Python

import RPi.GPIO as GPIO
import time
# photo sensor on PIN 5
PIN_SENSOR = 5
# LED on PIN 22
PIN_LED = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(PIN_LED, GPIO.OUT)
GPIO.output(PIN_LED, GPIO.LOW)
try:
while True:
print('Wait for photo sensor')
GPIO.wait_for_edge(PIN_SENSOR, GPIO.RISING)
print('Photo sensor active, activating light')
# turn LED on
GPIO.output(PIN_LED, GPIO.HIGH)
time.sleep(1)
GPIO.output(PIN_LED, GPIO.LOW)
# wait until sensor is not blocked anymore (v1)
while (GPIO.input(PIN_SENSOR) == GPIO.HIGH):
print('blocked')
time.sleep(1)
# Exit when Ctrl-C is pressed
except KeyboardInterrupt:
print('Shutdown')
finally:
print('Reset GPIO configuration and close')
GPIO.cleanup()