Need help with a script

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
codehobby
Posts: 8
Joined: Sun Nov 14, 2021 6:07 pm

Need help with a script

Post by codehobby » Sun Nov 14, 2021 6:22 pm

Hello,
I'm from from France (I didn't really find a forum in French so here I am)

I have an ESP32 and ws2812 led, I'd like to create a fade in/out effect with the color I want. I started a code but it doesn't work also I'm a big beginner in coding. Here is my code :

Code: Select all

import machine, time, neopixel

n = 5 #number of led
p = 13 #Pin number on the board
freq = 5000 #frequency

np = neopixel.NeoPixel(machine.Pin(p), n) #neopixel object
led = machine.PWM(machine.Pin(p), freq) #PWM object

while True:
    
    #color rgb
    r = 103
    g = 202
    b = 232
    
    for i in range(n):
        np[i] = (r, g, b)
        np.write()
    for duty_cycle in range(0, 1024, 10):
        led.duty(duty_cycle)
        time.sleep(0.05)
    for duty_cycle in range(1024, 0, -10):
        led.duty(duty_cycle)
        time.sleep(0.05)
        
Help would be much appreciated.

Thanks.

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: Need help with a script

Post by marcidy » Mon Nov 15, 2021 2:29 am

Code: Select all

import machine, time, neopixel

n = 5 #number of led
p = 13 #Pin number on the board
freq = 5000 #frequency

np = neopixel.NeoPixel(machine.Pin(p), n) #neopixel object
led = machine.PWM(machine.Pin(p), freq) #PWM object

while True:
    
    #color rgb
    r = 103
    g = 202
    b = 232
    
    for i in range(n):
        np[i] = (r, g, b)
        np.write()
 
This above snipped is (almost) the correct use of the NeoPixel driver.

First, remove the PWM config:

Code: Select all

# This correctly instantiates a NeoPIxel controller
np = neopixel.NeoPixel(machine.Pin(p), n) #neopixel object
# This creates a PWM on the same pin, effectively undoing any specific configuration of the pin required for the NeoPIxel.  Remove this line.
led = machine.PWM(machine.Pin(p), freq) #PWM object
Second, unindent the np.write call, it only needs to be called once to send the data to the strip. Filling the array creates the command string for the strip. np.write sends the command string.

Code: Select all

    # Fill the array, one color tuple for each LED
    for i in range(n):
        np[i] = (r, g, b)
        
    # send entire array of colors to LED strip all at once
    np.write()
And the "while True" is not doing anything here as you are not changing the color in the while loop.

Do this:

Code: Select all

import machine, time, neopixel

n = 5 #number of led
p = 13 #Pin number on the board

np = neopixel.NeoPixel(machine.Pin(p), n) #neopixel object

    #color rgb
r = 103
g = 202
b = 232
    
for i in range(n):
    np[i] = (r, g, b)

np.write()
 

All of the rest is incorrect for addressable LEDS and unneeded.

Code: Select all

    for duty_cycle in range(0, 1024, 10):
        led.duty(duty_cycle)
        time.sleep(0.05)
    for duty_cycle in range(1024, 0, -10):
        led.duty(duty_cycle)
        time.sleep(0.05)
PWMs control brightness by rapidly turning the LED on and off. Addressible LEDs take commands as each individual LED has a controller on the strip or in the LED. You send them commands only, you cannot control them via PWM.

codehobby
Posts: 8
Joined: Sun Nov 14, 2021 6:07 pm

Re: Need help with a script

Post by codehobby » Mon Nov 15, 2021 4:39 pm

Ok, thanks. I found 2 programs on which you can set the brightness but it's only for red, and white color.

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: Need help with a script

Post by marcidy » Wed Nov 17, 2021 6:24 pm

addressable LEDs are effectively 3 LEDS, red, green and blue.

The number you are setting IS the brightness of the LED. So you can manipulate that number to change the brightness. (so now you would need a loop).

Dealing with brightness in RGB color space is painful, I suggest looking up how to use HSV color space instead and convert to/from RGB. I'm sure someone in the world has figured it out, but here's some functions which can help.

Code: Select all

def rgb_to_hsv(r, g, b, base=255):                                              
    if base <= 0:                                                                  
        raise ValueError("Base must be strictly positive in rgb_to_hsv")           
    r = float(r)/base                                                           
    g = float(g)/base                                                           
    b = float(b)/base                                                           
                                                                                
    high = max(r, g, b)                                                         
    low = min(r, g, b)                                                          
    h, s, v = (high, high, high)                                                
                                                                                
    chroma = high - low                                                         
    s = 0 if high == 0 else chroma/v                                            
                                                                                
    if high == low:                                                             
        h = 0.0                                                                 
        return h, s, v                                                          
    elif r == high:                                                             
        h = (((g-b)/chroma) % 6) / 6                                            
        return h, s, v                                                          
    elif g == high:                                                             
        h = ((b-r)/chroma + 2) / 6                                              
        return h, s, v                                                          
    elif b == high:                                                             
        h = ((r-g)/chroma + 4) / 6                                              
        return h, s, v                                                          
                                                                                
                                                                                
def hsv_to_rgb(h, s, v, base=255):                                              
    if s == 0.0:                                                                
        ret = (v, v, v)                                                         
    i = int(h*6.)  # XXX assume int() truncates!                                
    f = (h*6.)-i                                                                
    p, q, t = v*(1.-s), v*(1.-s*f), v*(1.-s*(1.-f))                             
    i %= 6                                                                      
    if i == 0:                                                                  
        ret = (v, t, p)                                                         
    if i == 1:                                                                  
        ret = (q, v, p)                                                         
    if i == 2:                                                                  
        ret = (p, v, t)                                                         
    if i == 3:                                                                  
        ret = (p, q, v)                                                         
    if i == 4:                                                                  
        ret = (t, p, v)                                                         
    if i == 5:                                                                  
        ret = (v, p, q)                                                         
                                                                                
    return (int(ret[0]*base), int(ret[1]*base), int(ret[2]*base))

codehobby
Posts: 8
Joined: Sun Nov 14, 2021 6:07 pm

Re: Need help with a script

Post by codehobby » Thu Nov 18, 2021 10:50 am

Ok, I'll take a look at it. Thanks

Edison Franco
Posts: 1
Joined: Sat Jul 03, 2021 1:23 am
Location: Brasil

Re: Need help with a script

Post by Edison Franco » Sun Nov 21, 2021 2:11 am

codehobby wrote:
Mon Nov 15, 2021 4:39 pm
Ok, thanks. I found 2 programs on which you can set the brightness but it's only for red, and white color.
Hi, could you share these examples? Thanks!


Post Reply