Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Led Strip - Random Colour
#1
I'm a complete newb when it comes to electronics and python, but I thought I would share what I think is simple code that may help a beginner.

I'm running a WS2801 32 led strip via SPI on a Raspberry Pi.

I couldn't actually find any simple first time example to show off a simple animation...but then I didn't look too hard Dodgy Wink It was nearing midnight after a hard days work. Maybe there is something on Github.

This animation works for me. It's supposed to randomly select an led and fill with a random colour. Once the whole strip is coloured, it then works in reverse by switching a random led off. This whole process will continue to loop.

Like I said I'm a newb so if you hardcore Python programers are laughing, which I don't blame you, please provide some tips Smile. I didn't bother to create a class or define functions as in my opinion these layouts are easier to understand for a beginner, although you're more than welcome to correct me.


Code:
from bibliopixel.drivers.WS2801 import DriverWS2801
from bibliopixel.led import *
import random


led_number = 32
led = LEDStrip(DriverWS2801(num=led_number))

c = [colors.Red, colors.Green, colors.Blue]

while True:
    led_index = []

    # Append 0 - led_number to led_index list, i.e [0, 1, 2, 3, 4, .., 31]
    for index1 in range(led_number):
        led_index.append(index1)

    # Randomly select an led and color. The selected led is then removed from the list so it's not selected again.
    for each_led1 in range(led_number):
        x1 = random.choice(led_index)
        y1 = random.choice(c)
        led.set(x1, y1)
        led.update()
        led_index.remove(x1)

        # Adjust the sleep time to speed up/down the effect
        time.sleep(0.05)

    # Append 0 - led_number to led_index list again
    for index2 in range(led_number):
        led_index.append(index2)

    # Randomly select an led and switch it off. The led_index is then removed.
    for each_led2 in range(led_number):
        x2 = random.choice(led_index)
        y2 = colors.Off
        led.set(x2, y2)
        led.update()
        led_index.remove(x2)

        # Adjust the sleep time to speed up/down the effect
        time.sleep(0.05)
#2
@Sparkacus - Looks great!
There's documentation about writing an animation here: https://github.com/ManiacalLabs/BiblioPixel/wiki/Writing-an-Animation
But I am realizing not enough of a "Quickstart" to kick people off. I'll add that shortly.
#3
(03-16-2015, 12:28 PM)Adam Wrote: @Sparkacus - Looks great!
There's documentation about writing an animation here: https://github.com/ManiacalLabs/BiblioPixel/wiki/Writing-an-Animation
But I am realizing not enough of a "Quickstart" to kick people off. I'll add that shortly.

Adam, There is one piece of help that I would appreciate as another newbie.  I followed the link you gave to Writing an Animation but, how would I take that sample code and create a complete script which would actually run?  I also saw that some users have shared their animations which is great - but what steps are required to take one of those animations and create a complete script?  Thanks Chip
#4
Chip, using an animation is pretty straight forward. Here's the simplest case:

Code:
from bibliopixel.drivers.serial_driver import DriverSerial, LEDTYPE
from bibliopixel import LEDStrip
from MyAnimation import MyAnimation

#init driver
#use whatever type and LED count you have
driver = DriverSerial(type=LEDTYPE.LPD8806, num=100)

#init controller
led = LEDStrip(driver)
#nothing else needed for LEDStrip really.

#init anim - this will depend on the animation you are using
anim = MyAnimation(paramA=1, paramB=2)

#run it
anim.run()

Like I said, the above is the *simplest case possible*. It will run the animation as fast as the CPU and LEDs can handle it.
You can then change params like frame rate, threading, etc on the animation, or many others on the driver or LEDStrip class. But those are the 4 required steps: Driver, Controller, Animation, Run

For another example: https://github.com/ManiacalLabs/BiblioPixel/blob/master/strip_setup.py
For a more complicated one: https://github.com/ManiacalLabs/BiblioPixel/blob/master/text_demo.py


Forum Jump:


Users browsing this thread: 1 Guest(s)