Skip to content

Instantly share code, notes, and snippets.

@PogiNate
Last active November 19, 2024 19:12
Show Gist options
  • Save PogiNate/9bf20e079e1886fc71cc1fa6cf21b8a4 to your computer and use it in GitHub Desktop.
Save PogiNate/9bf20e079e1886fc71cc1fa6cf21b8a4 to your computer and use it in GitHub Desktop.
# Christmas Tree with random lights
# By Nate Dickson
# 19 November 2024
# This code is written in CircuitPython
# For a Raspberry Pi Pico 2040
# But should work for any CircuitPython capable microcontroller.
import board
import digitalio
import time
import neopixel
import random
import math
from analogio import AnalogIn
# Set this to any Analog pin that isn't currently in use.
analog_in = AnalogIn(board.A1)
# Using this to seed the randomizer. For some reason when I wrote this code a truly random
# Pattern was very important to me. This is as close to true random
# As we can get, since it's using data from an unused analog pin.
def get_voltage():
return math.floor((analog_in.value * 3.3))
# Green Indicator
# Just to make sure the board is running our code at all.
# led = digitalio.DigitalInOut(board.LED)
# led.direction = digitalio.Direction.OUTPUT
# on_time = 0.25
# off_time = 0.75
# NeoPixels
# Set COUNT to match what you actually have
# In the next line use the GP pin you're actually using.
COUNT = 40
pixels = neopixel.NeoPixel(board.GP17, COUNT, brightness=0.6, auto_write=False)
# Some Colors!
# For some reason these are Green, Red, Blue.
# I think they changed the hardware of the NeoPixels, and I probably should just change a setting, but meh.
GREEN = (255, 0, 0)
RED = (0, 255, 0)
BLUE = (0, 0, 255)
PINK = (50, 200, 50)
YELLOW = (205, 185, 0)
CYAN = (175, 0, 175)
PURPLE = (20, 105, 30)
ORANGE = (40, 255, 0)
WHITE = (120, 150, 80)
OFF = (0, 0, 0)
random.seed(get_voltage()) # Seed the randomizer.
lights = []
COLORS = [GREEN, RED, CYAN, ORANGE, PURPLE] # Set these to whichever ones you want on your tree at the moment.
# Let's give every pixel some random amount of time to stay on.
# This is all in seconds.
LONG = 6
SHORT = 1
SLEEP = 0.1
for i in range(COUNT):
new_light = [random.choice(COLORS), random.randint(SHORT, LONG)]
# print(new_light) # Another debug line.
lights.append(new_light)
pixels[i] = lights[i][0]
pixels.show()
# This is the main loop, each light's on time will decrement by SLEEP each loop
# The light will be off for a random amount of time (shorter than the "on" time)
# Then come back on, with a randomly set color and amount of time.
while True:
time.sleep(SLEEP)
for i in range(COUNT):
lights[i][1] = lights[i][1] - SLEEP if lights[i][1] > 0 else 0
if lights[i][1] == 0:
if lights[i][0] == OFF:
lights[i][0] = random.choice(COLORS)
lights[i][1] = random.randint(SHORT, LONG)
else:
lights[i] = [OFF, random.random() + 0.1]
pixels[i] = lights[i][0]
pixels.show()
# print((get_voltage(),)) #Useful if we want to see what sort of range we're getting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment