Created
June 21, 2022 07:03
-
-
Save JNSAPH/c2219e0223206b6635b4d2193c6b8e00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imports | |
import RIP.GIPIO as GPIO | |
import time | |
from time import sleep, time | |
import datetime | |
# GPIO Setup | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
# Variablen Definition | |
LED_GREEN, LED_RED, LED_BLUE = 13, 19, 26 | |
MOTION_DETECTOR, PUSH_BUTTON = 24, 23 | |
# GPIO Pin Setup | |
GPIO.setup((LED_GREEN, LED_RED, LED_BLUE), GPIO.OUT) | |
GPIO.setup((MOTION_DETECTOR, PUSH_BUTTON), GPIO.IN) | |
# PWM Setup | |
pwm = GPIO.PWM(LED_RED, 50) | |
# Returns 0 at night and 1 at day | |
def get_daytime(): | |
if datetime.datetime.now().hour < 6 or datetime.datetime.now().hour > 18: | |
return 0 | |
else: | |
return 1 | |
# Turn on LED | |
def turn_on_led(): | |
dc = 0 # Value in % | |
pwm.start = GPIO.PWM(dc) | |
day = get_daytime() | |
if day: | |
dc = 100 | |
pwm.changeDutyCycle(dc) | |
else: | |
dc = 20 | |
pwm.changeDutyCycle(dc) | |
sleep(5) | |
pwm.stop() | |
def push_button_function(): | |
turn_on_led() | |
def motion_detector_function(): | |
turn_on_led() | |
# GPIO event setup | |
GPIO.add_event_detector(PUSH_BUTTON, GPIO.FALLING, callback=push_button_function, bouncetime=200) | |
GPIO.add_event_detector(MOTION_DETECTOR, GPIO.RISING, callback=motion_detector_function) | |
try: | |
print("Programmstart. STRG + C zum Abbruch") | |
# Hauptprogrammschleife ab hier | |
while True: | |
time.sleep(0.2) | |
except KeyboardInterrupt: | |
print("Programmabbruch.") | |
finally: | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment