Sunrise / Sunset PWM Light

Hey! Thanks for an real cool software!

I am building an controller for an terrarium.
It feels like i have made this solution too complicated. But this is what i come up with.

I know i could done this with fades, but i dont feel like thats proper, cause if i move sunrise/sunset time, then “the sun” wont move until “next day”.

How would you guys do this?

I don’t have access to google drive, so I can’t see your code, but the best way to avoid the skipping problem is to use less and greater instead of equal. If you can change on the hour, this should be close to what you want:

Fade to 1 if between daylight hours, else fade to 0. If you want to be able to start/stop at times not on the hour, the easiest solution is probably to use hour x 60 + min and convert your time-of-day used in compares to minutes (so 8:15 am would be 8 x 60 + 15).

Thx for the reply, The convert strategy is closer to my solution. Ive converted time into decimals and then used some math to get noon-time from sunrise and sunset values.
And then map-clip, feels really messy but works i guess :man_shrugging:

Wasnt able to upload it to forum since new user. I will play some with your solution!

But still if im past sunrise, and for Example move sunrise back, it would be “sunnier” at current time?

When using fade method that wont apply? It will just continue to fade up at sunrise and down at sunset?

Fade will move from the current value to the target value. If it is passed sunset, it will be moving darker (but may not yet be full dark). If you change time so it is again daylight, it will start getting brighter again until sunset when it will start getting dark again. Not ideal, but better than missing sunrise and staying dark all day…

Ill make an example using 24hr format.

Sunrise: 04:00
Sunset: 17:00

Clock is now 07:00 and lights are at 33%

I now change my sunrise time to 05:00 so my lights change to 28%

If i go for fade approach to this problem, lights wont change in real-time when nudging sunrise/sunset?

Ah…that’s different. I was assuming you would spend 15-30 minutes fading light on at sunrise, then leave it on all day and spend 15-30 minutes fading light at sunset.

What you want is max bright middle of the day and fade up to it starting at sunrise, then fade back down until sunset. Ignoring that you probably really want to quickly go up to around 50% bright after sunrise, then slowly increase until midday we will start on the problem…

You don’t really want to use fade & start/stop at sunrise/sunset. You want to set your brightness based on how far through the day we are, so we need to come up with some math that allows you to calculate a value between 0 & 1 based on how far we are through the day.

The first step is to determine if we are within day or night. Just set brightness to zero if we are outside of daytime. I’m going to assume hours for this example. Using time-of-day in minutes will give you better fade (it won’t jump to new value each hour), but this will make code a little easier & you can see the change if you change hour for testing.

When we are in daytime mode, we need to know how many hours are in the day so we can calculate how far we are through the day, so subtract sunrise from sunset time to get length of day (17-4=13 hours in your initial example). Now subtract sunrise time from current time to determine how far we are into the day (7-4=3). So we are 3 hours into a 13 hour day.

We get brighter until mid-day, then get dimmer, so we need to know mid-day. Since day is 13 hours, mid-day would be 13/2=6.5 hours. Current time divided by length of 1/2 day (3/6.5=0.46) can be used to set LED brightness. This works until we get to hour 7 (after mid-day) & 7/6.5=1.07, which is greater than 1, but we want to start getting dimmer after mid-day. One solution is to calculate brightness using current/mid-day (where current already has sunrise time subtracted from it), but subtract the result from 2 if value is >1. So before mid-day, LED is set to current/mid-day and we get brighter as we approach mid-day. Just after mid-day, calculated value is just over 1; subtract it from 2, and we get an LED value just under 1. As we approach sunset, calculated value approaches 2, and LED value approaches 0.

Assuming you can figure out the calculations, the divide in this example will be fed the current time (minus sunrise) on the left, and length of 1/2 day ( (sunset-sunrise)/2 ) on the right. You’ll need to add in additional code to make sure the LED is off at nighttime (an additional if-else between this if-else & led-LUM would be one option).


1 Like

Thanks! I will look into your solution when i get home from work… That looks so much less messy then my solution :joy::+1:

circadian rhythm lighting?

A clever mathematical solution will often be simpler than trying to brute-force code. The problem is finding the clever solution. I think the start of my logic above is pretty easy to follow. Realizing a simple divide gets you a number between 0 & 1 is not so obvious. Even if you figure that out, the “obvious” solution for afternoon is probably to reverse the morning calculations and determine how far we are from sunset like we determine how far we are from sunrise, but that is a lot more complicated (and compute-intensive) than just subtracting the morning number from 2.

Subtracting from 2 is not very obvious and would be easy to overlook as a possible solution. I only got there by starting to think about subtracting from 1 to reverse the progression (get a smaller result as the value increased). Realizing that gave a negative number, I thought about using absolute value, but saw that would go dim->bright. That’s when I realized I just needed to subtract from 2 instead of 1 to get bright->dim in the afternoon. You seldom come up with a clever solution right off the bat. It usually involves a thought process with several dead-ends. Sometimes you code up an entire complex solution and only realize days later there is a clever trick to simplify everything.