Contact Us

Use the form on the right to contact us.

You can edit the text in this area, and change where the contact form on the right submits to, by entering edit mode using the modes on the bottom right. 


Oak Ridge, TN, 37830
United States

Lyf-Hacking

Shortcuts, easter eggs, and the exaustingly fun pursuit of solving lifes problems in unconventional ways.

Tank Project - Part 7 - All The Maths!

Wade Cantley

After a few hours of playing around I came up with a chunk of code that :

  1. Creates arrays to manage brightness (and eventually color) settings.
  2. Evaluates time between midnight and sunrise.
  3. Evaluates the time between Sunrise and Sunset.
  4. Evaluates the time between Sunset and Midnight.
  5. Outputs the correct brightness to print but later to the IR LED

 

What is important about the output is that the last iteration has some overlap.  So the last iteration (iteration 20) for the daytime overlaps with the first iteration of the evening.  I could be more precise but this is fine for my purposes and it will only happen under the circumstance where we are moving into a different time span.

I had hoped to just do two time periods... night and day.  But it turned out easier to divide the day into three segments of code for evaluation; morning, daytime, night.

So here is the code.

from urllib2 import Request, urlopen, URLError
import xml.etree.ElementTree as ET
import datetime
import time

#So we have to prep this URL with the day, month and whether to use daylight savings time.
#AND it has to be a string so that it is one big url string.
requestURL = "http://www.earthtools.org/sun/36.0286645/-84.2211902/" + str(datetime.datetime.today().day)  + "/" + str(datetime.datetime.today().month) + "/-5/" + str(time.localtime().tm_isdst)
request = Request(requestURL)

#Setup the variables that will be used and default values to start with.
sunrise = ""
sunset = ""
sunriseMinute = ""
sunsetMinute = ""
currentTimeMinutes = datetime.datetime.now().hour * 60 + datetime.datetime.now().minute
isError = False
incrementBy = 20
incrementalPeriod = ""
totalMinutesInDay = 60*24

print "current time minute : " + str(currentTimeMinutes)

#So we fetch the time stamp
try:
    response = urlopen(request)
    timestuff = response.read()
    #print timestuff[1:100000]
except URLError, e:
    print 'UUUHHHGGGGG. Got an error code:', e
    isError = True

# and we parse the data if there is no error
if isError == False:

    #get to the root of the XML
    root = ET.fromstring(timestuff)

    #pick out the sunrise and sunset from the XML, replace the colons with commas
    #then split it into a list with multiple objects. So from 07:10:25 into ['07','10','25']
    sunrise = root[3][0].text.replace(":",",").split(',')
    sunset = root[4][0].text.replace(":",",").split(',')

    #now we calculate the minutes into the day for each. These will be our base numbers.
    sunriseMinute = int(sunrise[0]) * 60 + int(sunrise[1])
    sunsetMinute = int(sunset[0]) * 60 + int(sunset[1])

    print "Sunrise Minute : " + str(sunriseMinute)
    print "Sunset Minute : " + str(sunsetMinute)



    # *********************  Day Time Calculations ************************

    #I need to reound this so that it is a whole number.  Just easier to deal with.
    incrementalPeriodDay = round((sunsetMinute - sunriseMinute) / incrementBy)
    print "Increment by Minutes Daytime : " + str(incrementalPeriodDay)

    #put the daytime brightness settings in an array
    brightness = [5,5,6,6,7,7,8,9,10,10,10,10,9,8,7,7,6,6,5,5]

    #NOTE : Add a color array here when we figure out what the color settings are.

    thisItteration = 1
    while thisItteration <= incrementBy:
        #from sunrise to sunset
        if (sunriseMinute + (incrementalPeriodDay * thisItteration)) > currentTimeMinutes:
            print "Daytime Brightness " + str(brightness[thisItteration - 1])
            print "Itteration " + str(thisItteration)
            break

        thisItteration = thisItteration + 1





    #reset increment to 10 since we are going to be handling two quarters of the day seperately.
    # so 10 increments after sunset to midnight, and 10 increments from midnight to sunrise.
    incrementBy = 10

    # *********************  Morning Time Calculations ************************

    #I need to reound this so that it is a whole number.  Just easier to deal with.
    incrementalPeriodMorning = round(sunriseMinute / incrementBy)
    print "Increment by Minutes Morning : " + str(incrementalPeriodMorning)

    #put the daytime brightness settings in an array
    brightness = [1,1,2,2,2,3,3,4,4,5]

    #NOTE : Add a color array here when we figure out what the color settings are.

    thisItteration = 1
    while thisItteration <= incrementBy:
        #from midnight to sunrise
        if (0 + (incrementalPeriodMorning * thisItteration)) > currentTimeMinutes:
            print "Morning Brightness " + str(brightness[thisItteration - 1])
            print "Itteration " + str(thisItteration)
            # color and brightness commands will go here.
            break

        thisItteration = thisItteration + 1




    # ********************* Evening Time Calculations ************************

    #Increments if we take the time between sunset and midnight and divide by 10
    incrementalPeriodDay = round((totalMinutesInDay - sunsetMinute) / incrementBy)
    print "Increment by Minutes Daytime : " + str(incrementalPeriodDay)

    #put the Evening brightness settings in an array
    brightness = [5,4,4,3,3,2,2,2,1,1]

    #NOTE : Add a color array here when we figure out what the color settings are.

    thisItteration = 1
    while thisItteration <= incrementBy:
        #From Sunset to midnight
        if (sunsetMinute + (incrementalPeriodDay * thisItteration)) > currentTimeMinutes:
            print "Evening Brightness " + str(brightness[thisItteration - 1])
            print "Itteration " + str(thisItteration)
            break


        thisItteration = thisItteration + 1

And the results after running it along with the overlap.

Again, as you can see, iteration 20 triggers the day time code as well as iteration 1 for the evening.  But I have the brightness setting the same so, no big deal.  I can refine this later.

Now that I have the logic nailed down, I need to figure out how to setup a python executable.