Skip to content

Instantly share code, notes, and snippets.

@isaaguilar
Created August 9, 2023 14:45
Show Gist options
  • Save isaaguilar/bb845db8afc7696990aa651da0abb6eb to your computer and use it in GitHub Desktop.
Save isaaguilar/bb845db8afc7696990aa651da0abb6eb to your computer and use it in GitHub Desktop.
from machine import Pin, Timer, PWM
import random
import time
import math
maxPWM = 255 # 65535 max PWM value (When max is reached, the power is off)
# Define a function that takes n and m as parameters
def f(n, m):
# Calculate the angle based on n and m
a = 5 * math.pi * n / (3 * m) + math.pi / 2
# Calculate the red value based on the angle
r = math.sin(a) * 192 + 128
# Clamp the red value between 0 and 255
r = max(0, min(maxPWM, r))
# Calculate the green value based on the angle
g = math.sin(a - 2 * math.pi / 3) * 192 + 128
# Clamp the green value between 0 and 255
g = max(0, min(maxPWM, g))
# Calculate the blue value based on the angle
b = math.sin(a - 4 * math.pi / 3) * 192 + 128
# Clamp the blue value between 0 and 255
b = max(0, min(maxPWM, b))
return int(b/255*65535),int(r/255*65535),int(g/255*65535)
led = Pin(25, Pin.OUT)
timer = Timer()
def blink(timer):
led.toggle()
# Blink the onboard LED
timer.init(freq=2, mode=Timer.PERIODIC, callback=blink)
blue = Pin(15, Pin.OUT)
green = Pin(13, Pin.OUT)
red = Pin(1, Pin.OUT)
button = Pin(28, Pin.IN, Pin.PULL_DOWN)
redPWM = PWM(red)
redPWM.freq(2000)
greenPWM = PWM(green)
greenPWM.freq(2000)
bluePWM = PWM(blue)
bluePWM.freq(2000)
def setColor(r_val, g_val, b_val):
print(f"Setting r={int(r_val/1000)}, g={int(g_val/1000)}, b={int(b_val/1000)}")
redPWM.duty_u16(r_val)
greenPWM.duty_u16(g_val)
bluePWM.duty_u16(b_val)
# orange -> 1000, 40000, 64000
# yellow -> 0, 8000, 64000
# purple -> 30000, 61000, 9000
def handlers():
pressed = 0
toggle = False
toggleAt = 0 # 20 ~= 1 second
setColor(65535,65535,65535) # Start in the off position
while True:
if pressed:
if not toggle:
toggle = True
toggleAt = 0
else:
toggleAt += 1
if toggleAt > 40:
toggleAt = 0
loop2(toggle)
toggle = False
setColor(65535,65535,65535) # Start in the off position
pressed = button.value()
else:
if toggle:
toggle = False
r = random.randint(0, 65535)
g = random.randint(0, 65535)
b = random.randint(0, 65535)
setColor(r,g,b)
pressed = button.value()
time.sleep(.05)
change_color_time_in_millisecond = 10
def check_button_press(initToggle, toggle):
pressed = button.value()
millisecond_count = 0
while millisecond_count < change_color_time_in_millisecond:
pressed = button.value()
if not toggle:
if initToggle:
if not pressed:
initToggle = False
else:
if pressed:
toggle = True
time.sleep(.001)
millisecond_count += 1
if initToggle:
return True, False, False
if toggle:
return False, True, not pressed
return False, False, False
def loop2(initToggle):
toggle = False
released = False
i = 0
while True:
if i == 6*maxPWM:
i = 0
i+=1
r,g, b = f(i, maxPWM)
setColor(r,g,b)
initToggle, toggle, released = check_button_press(initToggle, toggle)
if toggle and released:
return
try:
handlers()
except Exception as e:
print(e)
redPWM.deinit()
greenPWM.deinit()
bluePWM.deinit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment