B4R Question Sunset times

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Does anyone have something that will calculate the sunset times based on a specific location.
Doesn't have to be exact time, I just don't want my LED display to go on too early.

I've come across these source codes:

https://blog.sleeplessbeastie.eu/2013/05/21/how-to-determine-the-sunrise-and-sunset-times/

file:///R:/RDrive/WorkingOn/Sunrise-Sunset%20Algorithm.htm

Just not sure if I can implement them in B4R can I just put the routines inside a #if C and then do a RunNative?

Any help appreciated

BobVal
 

thetahsk

Active Member
Licensed User
Longtime User

Try this Arduino lib with Inline C, it's based on the NOAA Solar Calculator. So It's easy to verify your results for testing.

Calculation details:
Website:
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Thanks will give that code a try.

Current I did this

Found this table:


Which I parsed with BJ4 and then took the Average Sunset time for each month and rounded it and put the values into the code below.

This will work for the time being (NOT trying to be exact)

B4X:
#region CanRun
Private Sub CanRun(Month As Int, Hour As Int, Minutes As Int) As Boolean
            '    Average Sunset For Month:01  Is:05:21  Minutes:321  24Hours Minutes:1041
            '    Average Sunset for Month:02  Is:05:57  Minutes:357  24Hours Minutes:1077
            '    Average Sunset for Month:03  Is:07:10  Minutes:430  24Hours Minutes:1150
            '    Average Sunset for Month:04  Is:07:52  Minutes:472  24Hours Minutes:1192
            '    Average Sunset for Month:05  Is:08:18  Minutes:498  24Hours Minutes:1218
            '    Average Sunset for Month:06  Is:08:37  Minutes:517  24Hours Minutes:1237
            '    Average Sunset for Month:07  Is:08:31  Minutes:511  24Hours Minutes:1231
            '    Average Sunset for Month:08  Is:08:07  Minutes:487  24Hours Minutes:1207
            '    Average Sunset for Month:09  Is:07:23  Minutes:443  24Hours Minutes:1163
            '    Average Sunset for Month:10  Is:06:39  Minutes:399  24Hours Minutes:1119
            '    Average Sunset for Month:11  Is:05:10  Minutes:310  24Hours Minutes:1030
            '    Average Sunset for Month:12  Is:04:59  Minutes:299  24Hours Minutes:1019
            '----------------------------------------------------------------------------------------------------------------------------
            '                                                 Jan   Feb   Mar   Apr   May   June  July  Aug   Sept  Oct   Nov   Dec              
            '                                                 17:30 18:00 19:15 20:00 20:30 20:45 20:45 20:15 19:30 18:45 17:15 17:00                        
            Dim AverageSunsetTimes(12) As Int = Array As Int(1050, 1100, 1155, 1200, 1230, 1245, 1245, 1215, 1170, 1125, 1035, 1020)
            
            If  Month >= 1 And Month <= 12 Then
                If  Hour <= 1 Then
                    If  Hour = 1 And Minutes > 30 Then
                        Return False
                    End If
                    
                    Return True
                End If
                    
                Dim TotalMinutes As Int = (Hour * 60) + Minutes
                
                If  TotalMinutes < AverageSunsetTimes(Month-1) Then
                    Return False
                End If
                
                Return True
            End If
            
            Return False            
End Sub
#end Region

Again Thanks.
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
The BJ4 test program to make sure the LEDs were on after Sunset and off by 1:30 am

B4X:
    For mm = 0 To 11
        For hh = 0 To 23 
        Dim mins As Int = Rnd(23, 59)        
            Log("CanRun - Month:" &NumberFormat(mm+1, 2, 0) &"  Time:" &NumberFormat(hh, 2, 0) &":" &NumberFormat(mins, 2, 0) &"  ? " &CanRun(1, hh, mins))
        Next
    Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…