SHARE
TWEET

Unicorn Sunrise

sweenig Jun 11th, 2015 1,498 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import unicornhat as UH
  3. import time
  4. midnight = [25, 25, 112] #possible future color during night time
  5. targetcolor = [253, 94, 83] #this is the color of the sunrise. I picked one. Modify as needed [R,G,B]
  6. duration = 6400 #this is the total amount of time that the sun should take to rise, there are 64 LEDs and they each cycle through from 1 to 100, so 6400 gets you 1 second per LED. If you want a faster sunrise, lower this number
  7. dayduration = 900 #this is the total amount of time that the sun should stay risen (900s = 15min)
  8. def drawsun(brightness): #function to draw all 64 pixels at a specified percentage of the final color.
  9.         red = targetcolor[0] * brightness / 100 #calculate the R value for the given progress between 0 and the target R value.
  10.         green = targetcolor[1] * brightness / 100 #calculate the G value for the given progress between 0 and the target G value.
  11.         blue = int(targetcolor[2] * brightness / 100) #calculate the B value for the given progress between 0 and the target B value.
  12.         for y in range(8): #cycle through every row
  13.                 for x in range(8): #cycle through every column
  14.                         UH.set_pixel(x,y,red,green,blue) #set the pixel
  15.                         UH.show() #make it draw
  16.                         time.sleep(duration/6400) #wait before moving on
  17. try:
  18.         print "Sun rising..."
  19.         for a in range(1,101): #cycle through the brightness levels from 1 to 100
  20.                 drawsun(a)
  21.         print "Sun up."
  22.         time.sleep(dayduration) #let the sun shine in!
  23.         print "Sun setting..."
  24.         for b in range(99,-1,-1): #cycle through the brightness levels back down to 0
  25.                 drawsun(b)
  26. except:
  27.         print("terminated manually")
RAW Paste Data
Top