heisser_draht/rgb_led.py
2019-09-09 08:20:40 +02:00

118 lines
2.8 KiB
Python

import signal, sys, time, os, RPi.GPIO as GPIO
pin_red = 22
pin_green = 18
pin_blue = 16
red = ""
green = ""
blue = ""
def exit_application():
GPIO.cleanup()
sys.exit()
return
def signal_handler(sig, frame):
print('Programm exiting')
exit_application()
#signal handing for controlled exit via ctrl+c
signal.signal(signal.SIGINT, signal_handler)
GPIO.setmode(GPIO.BOARD)
def led_init():
global red,green,blue
GPIO.setup(pin_red, GPIO.OUT)
GPIO.setup(pin_green, GPIO.OUT)
GPIO.setup(pin_blue, GPIO.OUT)
red = GPIO.PWM(pin_red, 100)
red.ChangeDutyCycle(0)
#red.ChangeFrequency(100)
blue = GPIO.PWM(pin_blue, 100)
blue.ChangeDutyCycle(0)
#blue.ChangeFrequency(100)
green = GPIO.PWM(pin_green, 100)
green.ChangeDutyCycle(0)
#green.ChangeFrequency(100)
green.start(0)
blue.start(0)
red.start(0)
def fade_red(zeit):
for i in range(10,70):
print(i)
red.ChangeDutyCycle(i)
time.sleep(zeit)
for i in range(0,60):
print(i)
red.ChangeDutyCycle(70-i)
time.sleep(zeit)
def fade_blue(zeit):
for i in range(10,70):
print(i)
blue.ChangeDutyCycle(i)
time.sleep(zeit)
for i in range(0,60):
print(i)
blue.ChangeDutyCycle(70-i)
time.sleep(zeit)
def fade_green(zeit):
for i in range(10,70):
print(i)
green.ChangeDutyCycle(i)
time.sleep(zeit)
for i in range(0,60):
print(i)
green.ChangeDutyCycle(70-i)
time.sleep(zeit)
def change_green(amount):
# takes an input from 0 to 255 and converts it to Duty Cycle
global green
factor = 100/255
green.ChangeDutyCycle(amount*factor)
def change_blue(amount):
# takes an input from 0 to 255 and converts it to Duty Cycle
global blue
factor = 100/255
blue.ChangeDutyCycle(amount*factor)
def change_red(amount):
# takes an input from 0 to 255 and converts it to Duty Cycle
global red
factor = 100/255
red.ChangeDutyCycle(amount*factor)
def change_led_colour(red_amount,green_amount,blue_amount):
global red,green,blue
red.ChangeDutyCycle(red_amount)
blue.ChangeDutyCycle(blue_amount)
green.ChangeDutyCycle(green_amount)
led_init()
while True:
change_led_colour(99,0,99)
time.sleep(0.2)
change_led_colour(0,99,99)
time.sleep(0.2)
change_led_colour(99,99,0)
time.sleep(0.2)