Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
2
Indexable
Never
import RPi.GPIO as GPIO
from time import sleep

# Select GPIO Mode
GPIO.setmode(GPIO.BCM)

# Set red, green, and blue pins
redPin = 12
greenPin = 19
bluePin = 13

# Set pins as outputs
GPIO.setup(redPin, GPIO.OUT)
GPIO.setup(greenPin, GPIO.OUT)
GPIO.setup(bluePin, GPIO.OUT)

# Initialize PWM
red_pwm = GPIO.PWM(redPin, 100)  # Frequency set to 100Hz
green_pwm = GPIO.PWM(greenPin, 100)
blue_pwm = GPIO.PWM(bluePin, 100)

# Start PWM with 0% duty cycle (off)
red_pwm.start(0)
green_pwm.start(0)
blue_pwm.start(0)

def turnOff():
    red_pwm.ChangeDutyCycle(0)
    green_pwm.ChangeDutyCycle(0)
    blue_pwm.ChangeDutyCycle(0)
    
def white():
    red_pwm.ChangeDutyCycle(100)
    green_pwm.ChangeDutyCycle(100)
    blue_pwm.ChangeDutyCycle(100)
    
def red():
    red_pwm.ChangeDutyCycle(100)
    green_pwm.ChangeDutyCycle(0)
    blue_pwm.ChangeDutyCycle(0)

def green():
    red_pwm.ChangeDutyCycle(0)
    green_pwm.ChangeDutyCycle(100)
    blue_pwm.ChangeDutyCycle(0)
    
def blue():
    red_pwm.ChangeDutyCycle(0)
    green_pwm.ChangeDutyCycle(0)
    blue_pwm.ChangeDutyCycle(100)
    
def yellow():
    red_pwm.ChangeDutyCycle(100)
    green_pwm.ChangeDutyCycle(100)
    blue_pwm.ChangeDutyCycle(0)
    
def purple():
    red_pwm.ChangeDutyCycle(100)
    green_pwm.ChangeDutyCycle(0)
    blue_pwm.ChangeDutyCycle(100)
    
def lightBlue():
    red_pwm.ChangeDutyCycle(0)
    green_pwm.ChangeDutyCycle(100)
    blue_pwm.ChangeDutyCycle(100)

try:
    while True:
        turnOff()
        sleep(1) # 1 second
        white()
        sleep(1)
        red()
        sleep(1)
        green()
        sleep(1)
        blue()
        sleep(1)
        yellow()
        sleep(1)
        purple()
        sleep(1)
        lightBlue()
        sleep(1)
        
except KeyboardInterrupt:
    pass

# Stop PWM
red_pwm.stop()
green_pwm.stop()
blue_pwm.stop()

# Cleanup GPIO
GPIO.cleanup()
Leave a Comment