Author Topic: Digispark WS2812 LED strip X-mas lights  (Read 8559 times)

smartynov

  • Newbie
  • *
  • Posts: 7
Digispark WS2812 LED strip X-mas lights
« on: December 20, 2015, 05:37:35 pm »
Recently I came across a WS8212 LED strip and decided to use it for making some decoration for Christmas. I developed an algorithm that draws three blurred spots of red, green and blue, moving across the strip and creating various colors when overlapped. The movement is randomized on restart.

I use Gaussian distribution to provide a smooth look. The exp() function appeared to be very slow, so I replaced it with a polynomial approximation. If someone needs to further optimize it for speed (for example, if you have longer LED strip), one can rewrite it all in fixed-point math (Probably I will do it some day).

If you define DEBUG, the LED strip will output current fps (number of redraws per second) by lighting one of the LEDs to permanent white (fps equals to number of white LED, counting from strip head). My digispark @ 16MHz is capable of something like 30+ fps.

Code is available at https://github.com/smartynov/iotfun/blob/master/arduino/xmas_lights/xmas_lights.ino

A picture worth a thousand words, so you can take a look at the video here (multiple "copies" of light is a reflection in my window pane – looks great in the dark).


OSComputing

  • Newbie
  • *
  • Posts: 5
Re: Digispark WS2812 LED strip X-mas lights
« Reply #1 on: January 02, 2016, 10:57:34 pm »
Wow, works perfectly!

What are the practical limits on the number of LEDs that could be lit using a DigiSpark and this sketch?

Is the waves variable in use in the current version?
Code: [Select]
/*
typedef struct {
  float len;
  float pos;
  float amp;
  float spd;
} wave;
wave waves[1];
*/

How do I use DEBUG?

I hope you get a chance to write more neopixel magic!
Richard
« Last Edit: January 02, 2016, 11:03:08 pm by OSComputing »

smartynov

  • Newbie
  • *
  • Posts: 7
Re: Digispark WS2812 LED strip X-mas lights
« Reply #2 on: January 06, 2016, 07:41:07 am »
Wow, works perfectly!
Thanks, Richard! I'm glad you liked it.

Quote
What are the practical limits on the number of LEDs that could be lit using a DigiSpark and this sketch?
In terms of hard limits, there are none – the "neopixel" protocol can control any number of LEDs. It takes about 1/30000 of second to send color data to one pixel, so you can update, say, 1000 pixels 30 times per second. Or more than a million once a minute .) But make sure to provide enough power (it is recommended to have a power supply connected each 100-300 pixels).

However, in this sketch most time is spent on math calculations: it recalculates color of each pixel on each redraw. My measurements show that I can get a 20-30 fps (redraws per second) on a 60-LED strip. It means that you can try 100-200 LEDs while maintaining it relatively smooth.

If you need a longer strip, there are two possible solutions: either you can use a more powerful chip (for example, on Raspberry Pi all the calculations will take nearly no time) or we can optimize the code. I think I can make it 5 to 10 times faster by switching to integer calculations only. I was going to do this anyway, so if you need a longer version, please write me and I will find time soon.

Quote
Is the waves variable in use in the current version?
No. As you can see, it's commented out. It was my initial idea to create a class that works with any number of "waves", but attiny seems to be too small to handle it properly. Probably I will return to this idea later and create an Arduino visual effects library.

Quote
How do I use DEBUG?
You just uncomment the "#define DEBUG" line (remove heading "//"). The only thing it does is lighting one "pixel" white. The number of white LED (counting from strip head) represents current number of redraws per second (I made this because DigiSpark has no hardware UART and it seems to be a simple way to output some values).

smartynov

  • Newbie
  • *
  • Posts: 7
Re: Digispark WS2812 LED strip X-mas lights
« Reply #3 on: January 08, 2016, 03:36:11 pm »
In terms of hard limits, there are none – the "neopixel" protocol can control any number of LEDs. It takes about 1/30000 of second to send color data to one pixel, so you can update, say, 1000 pixels 30 times per second. Or more than a million once a minute .) But make sure to provide enough power (it is recommended to have a power supply connected each 100-300 pixels).

One important note on this. I totally forgot that NeoPixel library allocates memory to store all the color data. It means that you are also limited by free memory in your device: you need 3*NUMPIXELS bytes for NeoPixel library to work. It seems that it is impossible to overcome this, because WS2812 protocol requires you to send all data at once.

Digispark has only 512 bytes of RAM, so is able to control about 100 "pixels".

However, you can always switch to a more powerful chip. I just tried the same code on a 144-LED strip with Atmega328 (Arduino Pro Mini) – works well.

OSComputing

  • Newbie
  • *
  • Posts: 5
Re: Digispark WS2812 LED strip X-mas lights
« Reply #4 on: January 09, 2016, 12:39:51 am »
Yes, it works well on many other 'duinos that I have. I plan on getting some really long strings in time and testing them. My plan is to have an interesting Christmas display.

I've ordered a nodemcu and I'll set everything up for programming it in Lua. I couldn't program my way out of a paperbag. ::) I'm keeping my eye on your github and I'll test anything your throw up there.  ;D

I also recently got my Digi Oaks from their recent Kickstarter campaign. They are very cool little devices and I am looking forward to exploring them!

Richard

smartynov

  • Newbie
  • *
  • Posts: 7
Re: Digispark WS2812 LED strip X-mas lights
« Reply #5 on: January 22, 2016, 04:04:18 pm »
I finally wrote a detailed instructable, and made some photos and video of this project.
Take a look at http://www.instructables.com/id/USB-NeoPixel-Deco-Lights-via-Digispark-ATtiny85/

And yes, I will be happy if you vote for this project in Arduino contest there on Instructables.