Giter Site home page Giter Site logo

Comments (3)

mattytrentini avatar mattytrentini commented on August 22, 2024

Tried a non-async 20ms step (1.5s 0-100%) fade and it's very smooth:

from machine import Pin, PWM
from time import sleep_ms
def fadein():
    led=PWM(Pin(12))
    for i in range(0, 1024, 13): # 13 = 1024/(1500ms/20ms)
        led.duty(i)
        sleep_ms(20)

from led_strip.

seanlano avatar seanlano commented on August 22, 2024

On the point:

Brightness appear to be non-linear

I found that using an exponential function gave a better-looking response - in my case in the WiLED project I had a notion of "brightness steps", i.e. the dimmer would change between say five distinct levels only, rather than the full range PWM values. Then I have a mapping between those brightness levels and an actual PWM value. I suppose if you wanted to do 100 "brightness steps" then those would effectively be a percentage to PWM mapping.

The C++ code I have for it is:

float max_log10 = log10(led->getMaxPWM());
float steps_log10 = log10(NUM_DIM_STEPS-1);
float exponent = max_log10/steps_log10;
float result;
pwm_dim_levels[0] = 0;
for(int idx=1; idx<NUM_DIM_STEPS; idx++){
	result = pow(idx,exponent);
	pwm_dim_levels[idx] = int(result);
}
pwm_dim_levels[NUM_DIM_STEPS-1] = led->getMaxPWM();

The gist of it is though, suppose you want 8-bit values and 6 steps, you find the exponent with exponent = log10(255)/log10(6) = 3.092638069 and then the actual value with pwm = step ^ exponent.

Then the values for each step are:

pwm_dim_levels[0] = 0
pwm_dim_levels[1] = 1
pwm_dim_levels[2] = 9
pwm_dim_levels[3] = 30
pwm_dim_levels[4] = 72
pwm_dim_levels[5] = 145
pwm_dim_levels[6] = 255

from led_strip.

mattytrentini avatar mattytrentini commented on August 22, 2024

Thanks @seanlano!

I was had intended to insert a mapping shim method - pass a function a value from 0-100 and then have it return a non linear value from 0-100. Something like what you've proposed ought to work well!

Looking forward to seeing your LED controller in the flesh... :)

from led_strip.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.