berto86
Posts: 8
Joined: Mon Jan 16, 2017 12:49 pm

Advent Calendar

Thu Nov 30, 2017 8:33 pm

Hi everyone,

I'd to share a project for the festive period. Using a Pi 0 W and the Pi Huts 3D Xmas Tree, I have created an advent calendar. The code is as follows:

Code: Select all

#!/usr/bin/env python3
#import necessaries
from gpiozero import LEDBoard, LED, PWMLED
from gpiozero.tools import random_values
from signal import pause
from datetime import datetime, date
from time import sleep

#define tree lights and the star
cTree = LEDBoard(*range(3,28), pwm=True)
star = LED(2)

#this displays the number of LEDs on the tree for a given day in Advent
def calendar_lights():
    cTree.off()
    t = datetime.now().minute
    d = datetime.now().day
    while t < 1:
        x = (d + 1)
        for led in cTree[:x]:
            led.on()
            sleep(2)
        z = (60 - x)
        sleep(z)
        t = datetime.now().minute
    return(t)

#Make the LEDs sparkle when its not December    
def twinkle_lights():
    n = datetime.now().month
    while n < 13:
        for led in cTree:
            led.source_delay = 0.1
            if n < 12:
                led.source = random_values()
                n = datetime.now().month
            elif n > 11:
                led.source = 0
                n = datetime.now().month
                return(n)
        n = datetime.now().month
    return(n)    

#Controls lights for December
def festive_lights():
    t = datetime.now().minute
    while t < 60:
        d = datetime.now().day
        m = datetime.now().month
        if m == 12 and d < 25:
            for led in cTree:
                led.source_delay = 0.1
                if 0 < t < 59:
                    led.source = random_values()
                    t = datetime.now().minute
                    d = datetime.now().day
                elif t < 1:
                    calendar_lights()
                    t = datetime.now().minute
                    d = datetime.now().day
                else:
                    led.source = 0
                    cTree.off()
                    t = datetime.now().minute
                    d = datetime.now().day
                    
        else:
            for led in cTree:
                led.source_delay = 0.1
                led.source = random_values()
                d = datetime.now().day
                t = datetime.now().minute
    t = datetime.now().minute
    d = datetime.now().day

#Main part
#Turns on star                
star.on()
#This section will remind the user if its not December and ask if the
#lights are to be activated or if its December it will start the Advent sequence
try:
    m = datetime.now().month
    while m < 13:
        if m < 12:
            a = input("Its not Christmas yet, would you like to turn on the lights? ")
            if a == 'y':
                n = 0
                twinkle_lights()
                for led in cTree:
                    led.source = 0
                    cTree.off()
                m = n
            elif a == 'n':
                exit()
            else:
                print("Only enter [y] or [n]!!")
                m = datetime.now().month
        else:
            festive_lights()
        m = datetime.now().month

#Closes everything down   
finally:
    cTree.off()
    star.off()
    cTree.close()
Feel free to use or modify this code. Likewise any suggestions for improvements will always be welcome. Enjoy the festivities!!

Return to “Other projects”