# 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

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