import random
from machine import Pin, PWM
from utime import sleep
r = PWM(Pin(8, Pin.OUT))
g = PWM(Pin(9, Pin.OUT))
b = PWM(Pin(10, Pin.OUT))
r.freq(1000)
g.freq(1000)
b.freq(1000)
def interval_mapping(x, in_min, in_max, out_min, out_max):
return (x-in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def color_to_duty(rgb_value):
rgb_value = int(interval_mapping(rgb_value, 0, 255, 0, 65535))
return rgb_value
def color_set(r_value, g_value, b_value):
r.duty_u16(color_to_duty(r_value))
g.duty_u16(color_to_duty(g_value))
b.duty_u16(color_to_duty(b_value))
while True:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color_set(r, g, b)
sleep(0.25)