Code Monkey home page Code Monkey logo

rgbxmastree's Introduction

rgbxmastree

Code examples for the RGB Xmas Tree

Getting started

Start by downloading the xmas tree file. Open a terminal and type:

wget https://bit.ly/2Lr9CT3 -O tree.py

Test the tree by running python3 tree.py (or running it from an IDE like Mu, Thonny or IDLE). All the lights should come on (white).

When you write your own Python code, make sure you keep this file in the same folder.

If you're using Raspbian Desktop, you don't need to install anything. If you're using Raspbian Lite, you'll need to install gpiozero with:

sudo apt install python3-gpiozero

Open a Python shell or IDE, import RGBXmasTree and initialise your tree:

from tree import RGBXmasTree

tree = RGBXmasTree()

Change the colour

You can set the colour of all the LEDs together using RGB values (all 0-1):

from tree import RGBXmasTree

tree = RGBXmasTree()

tree.color = (1, 0, 0)

Alternatively you can use the colorzero library:

from tree import RGBXmasTree
from colorzero import Color

tree = RGBXmasTree()

tree.color = Color('red')

You can write a loop to repeatedly cycle through red, green and blue:

from tree import RGBXmasTree
from time import sleep

tree = RGBXmasTree()

colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]

for color in colors:
    tree.color = color
    sleep(1)

Individual control

You can also control each LED individually, for example turn each one red, one at a time:

from tree import RGBXmasTree
from time import sleep

tree = RGBXmasTree()

for pixel in tree:
    pixel.color = (1, 0, 0)
    sleep(1)

To control a specific pixel, you can access it by its index number (0-24):

tree[0].color = (0, 1, 0)

Change the brightness

You can change the brightness from 0 to 1 - the default is 0.5. You can set this when initialising your tree:

from tree import RGBXmasTree

tree = RGBXmasTree(brightness=0.1)

Alternatively, you can change it after initialisation:

from tree import RGBXmasTree

tree = RGBXmasTree()

tree.brightness = 0.1

You'll find that 1 is extremely bright and even 0.1 is plenty bright enough if the tree is on your desk :)

Examples

RGB cycle

Cycle through red, green and blue, changing all pixels together

One-by-one

Cycle through red, green and blue, changing pixel-by-pixel

Hue cycle

Cycle through hues forever

Random sparkles

Randomly sparkle all the pixels

rgbxmastree's People

Contributors

bennuttall avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rgbxmastree's Issues

loop program

i entered the code for the loop program for the tree lights it runs red green blue then stops it doesnt loop again its exactly the same as in the instructions

'Color' is not defined

Trying the examples huecycle.py, rgb.py and onebyone.py, I get the error
NameError: name 'Color' is not defined

Threading while loop

Is it possible to run through one of the sequences with a while loop, while allowing the while condition to be changed, would this need to be ran through two threads? For example:

def run_tree(value):
        while value:
            #sparkle

def set_value():
    if tree_on:
        run_tree(tree_on)
        tree_on = False
    else:
        run_tree(tree_on)
        tree_on = True

Hardware schematic to stack Rpi Hats

More of a request: Is there a hardware schematic documented to what pins are being used and what LED location to what pin?. To determine if can stack another shield/hat. I wanted to see if i could stack another Pi hat "PiFace-Digital2" that has two relays. That hat uses hardware SPI and wanted to use one of the Relays to control another xmas decoration while using the RGB xmas tree.

lights off

i use cron on boot and for shutdown.
i use the cron command 30 23 * * * /sbin/shutdown -h now,but when the pi shutdown to red light the lights of christmas tree freezes,they did not turn off,can you please advise me a simple cron command to auto turn them off ?

Large CPU draw

The example code are very CPU intensive. On a pi zero the cpu is running at 95%+

questions about the SPI pins

This is more a request for information than an issue. I just wonder why you're not using the actual SPI pins, 19 and 23. I've heard of "hardware SPI" versus "bit banging", and it seems you're doing the latter? -
physical pin 22 == GPIO 25 (used for clock? )
physical pin 32 == GPIO 12 (used for data? )

Could you please provide the thinking behind your approach?

Lights full on

I have installed a new image and followed the instructions.

Tree seems to work OK with all lights being illuminated on the RGB module - bright white.

When I try rgb.py or random sparkles.py, again all the lights are bright white and never change.

Can you help?

No license attached to repo!

Hi ThePiHut-Crew,

every christmas I set-up my RGB Xmas tree to be a Cheerlight (see cheerlights.com). My implementation is currently based on your "tree.py".

Unfortunately, there isn't any licensing information is attached to your repo. Are you able to add a license?
Or do I have to write my own "tree.py" (to get my project released on github)?

Kind regards,

  • BumblebeeMan buzz, buzz

Control tree over IoT

I'm trying to make my RGB tree into a webthing to use with Mozilla IoT, https://github.com/mozilla-iot/webthing-python. I've tried to set the tree off using threading with a common boolean that the IoT can control to turn the tree on and off. The tree code works fine on it's own but when run it as a thread I get the following:

gpiozero.exc.GPIOPinInUse: pin 8 is already in use by <gpiozero.PWMLED object on pin GPIO8, active_high=True, is_active=False>

Here's my code, my python's pretty poor but any help would be great.

from __future__ import division, print_function
from webthing import (Action, Event, Property, SingleThing, Thing, Value, WebThingServer)

from gpiozero import LEDBoard
from gpiozero.tools import random_values
from signal import pause
from tree import RGBXmasTree
from colorzero import Color, Hue
from time import sleep

import logging
import _thread
import time
import uuid
import random

tree = LEDBoard(*range(2,28),pwm=True)
tree_on = True
tl = RGBXmasTree(brightness=0.2)
colors = [Color('red'),Color('green'),Color('blue'),Color('yellow')]
tl.color = (0.5,0.5,0.5)

def random_color():
    r = random.random()
    g = random.random()
    b = random.random()
    return(r,g,b)

def rgb():
    print('rgb')
    for sequence in range (1,50):
        for color in colors:
            tl.color = color
            sleep(3)

def onebyone():
    print('onebyone')
    for sequence in range(1,20):
        for color in colors:
            for pixel in tl:
                pixel.color = color

def hue():
    print('hue')
    tl.color = Color('red')
    for sequence in range(1,200):
        tl.color += Hue(deg=1)

def sparkle():
    print('sparkle')
    for sequence in range(1,80):
        pixel = random.choice(tl)
        pixel.color = random_color()
        #sleep(0.1)

def tree_thread():
    print('Running tree thread')
    while tree_on:
        for sequence in range(1,6):
            if sequence == 1:
                sparkle()

            if sequence == 2:
                hue()

            if sequence == 3:
                onebyone()

            if sequence == 4:
                sparkle()

            if sequence == 5:
                rgb()

    while not tree_on:
        tree.color=(0,0,0)
        print('Tree off')
def make_thing():
        thing = Thing(
                'urn:dev:ops:rgb-tree',
                'RGB Tree',
                ['OnOffSwitch', 'Light'],
                'A web connected RGB Tree'
        )

        thing.add_property(
                Property(thing,
                'on',
                Value(True, lambda tree_on: print('On-State is now', tree_on)),
                metadata={
                        'title': 'On/Off',
                        'type': 'boolean',
                        'description': 'Whether the RGB-Tree is turned on',
                }))
        thing.add_property(
                Property(thing,
                'speed',
                Value(50),
                metadata={
                    '@type': 'BrightnessProperty',
                    'title': 'Speed',
                    'type': 'integer',
                    'description': 'The speed from 0-100',
                    'minimum': 0,
                    'maximum': 100,
'unit': 'percent',
                }))
        return thing

def run_server():
    print('starting run server')
    thing = make_thing()
    print('thing = make_thing')
    # If adding more than one thing, use MultipleThings() with a name.
    # In the single thing case, the thing's name will be broadcast.
    server = WebThingServer(SingleThing(thing), port=8888)
    try:
        logging.info('starting the server')
        server.start()
    except KeyboardInterrupt:
        logging.info('stopping the server')
        server.stop()
        GPIO.cleanup()
        logging.info('done')

if __name__ == '__main__':
    logging.basicConfig(
        level=10,
        format="%(asctime)s %(filename)s:%(lineno)s %(levelname)s %(message)s"
    )
    _thread.start_new_thread(tree_thread, ())
run_server()

LED brightness change

Even if I use the procedure to change the brightness 0.1 - 0.9, all LEDs are still fully lit ... what about that?

Lights do not work

Hi,

I've tried getting the lights to work on a RPI3B+ to no avail.
I've checked:

  • Python 3.7.3
  • just-updated pi
  • original pi PSU
  • GPIO is working (tested a few pins with same code)
  • SPI and IC2 both enabled in raspi-config and confirmed in config.txt
  • full buster installed (tried with buster lite too)

Any options I've missed?

TIA

C

Problem with individual led control

Hi. I have installed the RGBXmasTree on a Pi Zero W. It works except for the individual pixel control... e.g. tree[1].color = (1,0,1) does nothing; the example code with loops for pixels do nothing... I modified tree.py so that the class def for RGBXmasTree init sets each led separately - i.e. self._value = [(0,1,1),(1,1,0),(0,1,0), ... ] rather than a value * pixels and it duly sets each led to the individual colour, but as I say, I cannot get the leds to change individually after that... Any ideas?

Colorzero library missing

Where can I find the correct colorzero library for use with this project? I’ve searched github and found a library but it doesn’t work with this project.
Any help would be appreciated.

Directions from scratch

Could you write the directions from a fresh install of raspbian?

Lots of things that are not installed by default on some distrobutions of raspbian, pip, gpiozero, statistics......

tree.py doesn't seem to turn lights on

Hi,

I'm completely new to python.
I followed the instructions and the tree.py code doesn't seem to do as the instructions say (the lights don;t turn on white).

I'm not sure what has gone wrong or how to fix it.

no response from board

Hi,
I've set up the board as specfied here. Here's pic, showing the board is correctly slotted in.
I run a python code example but I get no response from the board, and also no python error (although when I took the board out and ran the same code, I got no error).
I'm running the code within a heavily configured Raspian image: https://www.pyimagesearch.com/2016/11/21/raspbian-opencv-pre-configured-and-pre-installed/. Could this be an issue?
Best, Andy.

update: I tried the tree out on a different pi3 using the latest Raspian, and afraid it still does not light (and no warnings from Python).

Labeled Pins Use?

There are 4 pins labelled D, C, P, and G in the middle of the tree but nothing detailing what they are used for. At first I thought I might be missing an LED but then I realized the example pictures on the website has the same thing.

Lights do not come on when running 'python3 tree.py'

Hi, having some issues following this guide. I have installed the RGB tree onto a Pi Zero W with a fresh install of raspbian lite OS.
The lights do not come on when testing by running python3 tree.py and I'm not too sure what to troubleshoot next.
Is there a way I can check my GPIO connection?

Very slow

Hi, my tree works, but is very slow. Setting a single value takes about a second (or more). I found out that it's device specific, it's slow on the old Pi 1.2B, but works fine on Pi 4B. Is it because of the software based SPI transfer? Any idea how to make this faster?

I'd need to use it on the old Pi and in the current state it's hard to code any meaningful effect, as you literally see the LEDs changing one by one.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.