Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.3 kB
1
Indexable
Never
import RPi.GPIO as GPIO
import time

# Configuration des broches GPIO pour les canaux RGB
PIN_RED = 17
PIN_GREEN = 18
PIN_BLUE = 27

# Configuration de la fréquence PWM
PWM_FREQUENCY = 100

# Initialisation de la bibliothèque GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Configuration des broches en mode de sortie
GPIO.setup(PIN_RED, GPIO.OUT)
GPIO.setup(PIN_GREEN, GPIO.OUT)
GPIO.setup(PIN_BLUE, GPIO.OUT)

# Configuration des objets PWM pour chaque canal
pwm_red = GPIO.PWM(PIN_RED, PWM_FREQUENCY)
pwm_green = GPIO.PWM(PIN_GREEN, PWM_FREQUENCY)
pwm_blue = GPIO.PWM(PIN_BLUE, PWM_FREQUENCY)

# Démarrage des PWM avec une valeur initiale de 0 (LED éteinte)
pwm_red.start(0)
pwm_green.start(0)
pwm_blue.start(0)

try:
    while True:
        # Variation de la luminosité de chaque canal
        for duty_cycle in range(0, 101, 5):
            pwm_red.ChangeDutyCycle(duty_cycle)
            time.sleep(0.1)
        
        for duty_cycle in range(0, 101, 5):
            pwm_green.ChangeDutyCycle(duty_cycle)
            time.sleep(0.1)
        
        for duty_cycle in range(0, 101, 5):
            pwm_blue.ChangeDutyCycle(duty_cycle)
            time.sleep(0.1)

except KeyboardInterrupt:
    pass

# Arrêt des PWM et nettoyage des broches GPIO
pwm_red.stop()
pwm_green.stop()
pwm_blue.stop()
GPIO.cleanup()
Leave a Comment