I need help with a formula

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

I need help with a formula

#1 Post by chromo23 » 10 May 2022, 21:12

For more than a decade i use arduinos to control my led lights (kitchenlight, wakeuplight in bedroom)
I use a formula for this to have a nice fade in/out effect that also takes care of the non linearity of the brightness of a led while fading in a linear way.
by slowing the process down i have also a wakeuplight for the winter.

I would like to move this to an esp8266 or esp32.
How would this work in rules? I can not do the syntax right....

Code: Select all

value = (exp(sin((x - 1000) / 2000.0 * PI)) - 0.36787944) * 108.0;
value2 = (int) value;
analogWrite(pulsepin, value2);

TD-er
Core team member
Posts: 8644
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: I need help with a formula

#2 Post by TD-er » 10 May 2022, 21:24

How often do you need to update the value per second?
For a wake-up light, it doesn't have to be quick, but for other purposes I can imagine you may need to do it several times a second.
This may be a bit too much for rules.

Edit:
Just plotted the formula to get an idea of the curve: https://www.desmos.com/calculator/yu1a1y27a1

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: I need help with a formula

#3 Post by chromo23 » 10 May 2022, 21:37

right now it is limited to 255 so the maximum needs to be adjusted to 1024.
i also use this for a pulsing/breathing led....therefore the sinusoidal component.. so milliseconds
I already thought that rules would be to slow. probably putting it into a plugin would do the trick right?

TD-er
Core team member
Posts: 8644
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: I need help with a formula

#4 Post by TD-er » 10 May 2022, 21:38

Code: Select all

value = (exp(sin((x - 1000) / 2000.0 * PI)) - 0.36787944) * 108.0;
value2 = (int) value;
analogWrite(pulsepin, value2);
Just pre-compute a constant.

/2000*pi = *(pi/2000) = *0.0015708

Code: Select all

on compValue do
  let,1,exp(sin((%eventvalue1% - 1000) * 0.0015708)) - 0.36787944
  let,1,[var#1]*108
  pwm,2,[var#1] // GPIO 2
endon
The event can then be something like

Code: Select all

event,compValue=100

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: I need help with a formula

#5 Post by chromo23 » 10 May 2022, 22:28

i changed the formula to use less steps for one cycle but still it is way to slow...

TD-er
Core team member
Posts: 8644
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: I need help with a formula

#6 Post by TD-er » 10 May 2022, 22:34

Then we must add some new flag to the PWM command, to apply some LUT and still allow for a fade from one value to another.

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests