Year Round Holiday LEDs Part 2: Software

October 17, 2018

Holiday Lights: Part 2

Today on the hookup we’re going to look at the wiring and programming side of things in part 2 of my year round holiday house LED video series.

When I start a project I always like to have an initial set of goals in mind.  For this video we’re going to set up different LED lighting zones, and map out those zones so we can use that information to create a custom effect with colors that can be controlled from home assistant.

Lets quickly go through the hardware portion.  I’m using an ESP8266 based NodeMCU to do the heavy lifting, a logic level converter to change the 3.3V signal coming from the NodeMCU to a 5V signal, and a single 5 volt 60 amp power supply.  If you’d like to know more about the hardware I selected for this project I’d highly recommend watching Part 1 of this series first.

I currently have my LED strips divided into 4 zones, first floor, second floor, wreath, and candy canes.  Each of these zones is connected to different pin on the Node-MCU.  You can see in the first part of my code I have my pins setup for my different zones with first floor lights on D2, second floor lights on D1, Candy canes on D5 and the wreath on D6.  I’ll use these variables to set up each of my zones in my code.

For each zone I need to do 3 things in my code.

First, at the top of your code you’ll need to define the number of LEDS in the strip, you’ll use this value in your animations later, you can see that I overestimate my values.  I do this because lighting an LED that exists in code but not in real life doesn’t cause any problems, but lighting an LED that doesn’t exist in the code makes the program very angry.

Under each of my define values I’ve got the layout of my roofline to help me remember how many LEDs is on each part.  You don’t need to count the LEDS as you are installing them.  I’ll show you an easy way to map out your rooflines using home assistant later in this video.

Second, in the variables section of your code you’ll need to define your fastLED arrays.  Each zone should be named something descriptive because this is how you’re actually going to light up each LED.  firstFloor[FIRSTFLOOR_LEDS] sets up an array of numbers called firstFloor that contains the numbers 0 through 480, each corresponding to an LED in your strip.  Complete an array definition for each zone of LEDs.

Third, in the void setup section of your code you’ll need to configure your fastLED strips.  To do this you’ll use the FastLED.addLeds command and tell the microcontroller what type of LED strip you’re using, the pin on the board where it is connected , the order of the red green and blue pixels, and finally the name of the array that it should use to control those LEDs, and the size of that array.

Now if we call the value firstFloor[0]=CRGB::White it will light up the first LED on the first floor roof, if we call secondFloor[9] = CRGB:: Blue  it will light up the 10th LED on the second floor.

I’ve included my whole Arduino sketch in the video description.  I really tried to avoid complex programming wherever possible to allow it to be modified by a novice programmer, this meant doing things a little less efficiently at times, but I really want you to be able to modify this code for your own use, so I think it’s worth it… so much so that I spent about 5 hours last Saturday rewriting this code from scratch to make it more novice friendly.

Fair warning: I’m using home assistant and node-red for these examples.  You can use YAML automations to send the values from the json lights if you want, but I really prefer node-red for ease of use.

In our configuration.yaml file we’re going to add 4 mqtt json lights, the first light be the master that will switch the lights on and off, control global brightness, and the effect.  The other three mqtt json lights will be for selecting custom colors to be able to change our patterns on the fly.  We’ll also add an input slider to go from 0 to 500, if you have more than 500 leds in one of your zones, you can just increase the maximum value of the slider. All the yaml is in the description including the addition to the groups.yaml file to get your entities together.  If you use lovelace you’ll need to get that set up yourself.

In node-red we need to parse the json coming out of each of those lights and then send them to different MQTT topics.  This is one of those places where I made some efficiency trade-offs to make the code easier to read.  In the BRUH automation code he parses the json objects in Arduino, but doing it for 4 different lights makes the code really scary for beginners and much more difficult to modify.

Each of the color changing lights is processed the same way.  The MQTT topic comes in, gets parsed into a json object, and then each part gets split off into the red, green, and blue values to be sent to separate mqtt topics.

The master light is very similar, but instead of being split into different color channels it sends the effect name, brightness, and on/off state to different mqtt topics.

The input_number is also processed in node-red and sent to the configure MQTT topic.

You can see that each of them also just pipes the received json back into the state topic with a retain flag in so home assistant will know the state of the lights even after a restart.

After you’ve restarted home assistant and deployed node red you can get started mapping out your roofline so you can make custom tailored animations for your house.

Turning on the main light switch will bring up the list of effects available in this code.  Selecting the effect LED_Locator will allow you to use the input slider to light up a single LED in each of your zones.  Use this information to draw out a map of your roofline including the first and last LED in each roof segment.  When you’re done you should have something like this.  I put a text representation of this diagram in my Arduino file to easily refer back to.

Now that our zones are mapped out, lets create a custom pattern using our new layout.

One of the best patterns to customize is what I call the “crash” pattern.  This pattern uses custom color 1 and custom color 2 to create a cool pattern that bounces back and forth on each roof section using a sine wave pattern, which means that it goes faster in the middle and slows down at the edges.

To generate this pattern we’ll use a built in fastLED function called beatsin8.  Beatsine8 expects a few different arguments.  The first number is the “beats per minute”, or the number of times it will bounce back and forth per minute.  I’m using 16 beats per minute, but you can tweak this number as you see fit.  The next number is the minimum value that you want it to generate, and the last is the maximum value.  I’m using beatsine8 here which means it uses 8 bits to make the calculation, which means the maximum value it can generate is 255.  If you have a roof section that is bigger than 255 LEDs you can use beatSine16 which can go up to 65,535, if you have a roof section larger than 65,535 LEDs you should check out my patreon page.

You’ll need to generate one of these beatsine values for each of your roof sections and you want your maximum value to be the total LEDs in that section of the roof.  You can see that my first roof peak goes up to LED 115 so I’ve generated a beatsine that goes from 0 to 115.  Then to generate the effect I want to light up my first floor LEDs starting at each end of the roof peak.  That means I can start one of them at LED0 and the other one at LED115, but I want the one starting at LED115 to go in reverse, so I’ll subtract the value of the beatsine instead of adding it.  For one of the waves I’ll use red1, green1 and blue1 which is my first custom color, and for the other wave I’ll use red2, green2, and blue2 which is my second custom color.

Save, compile, and upload to your nodeMCU and you should be in business.  Again, all the yaml, node-red flows and Arduino code are in the description and on my github page, but I’m hoping the major advantage of this code over the really great code the bruh automation has is that this one will be easier for a tinkerer to read, understand and modify.  If you just want to upload the code and use it as is, I’m still going to recommend BRUH automations code to you, but if you want your lighting effects to look custom tailored to your house.

Also in the code you can check out the first of my sound effects addition.  For now it’s limited only to the lightning effect, but I hope to add more effects for the Christmas show.

In a couple of weeks I’ll be doing another video on how to create other props that can be put up and taken down easily like the LED wreath that hangs on my garage.  Thank you to all my patrons you’re your continued support of my channel.  If I’ve convinced you to dive into this project in time for Christmas this year consider using my amazon affiliate links from the description to support my channel.  If you have questions about this project please leave them down in the comments, it’s the easiest way for me to respond to them and it helps other people find the answer as well.  If you enjoyed this video, please consider subscribing, and as always, thanks for watching the hookup.

Code:

Arduino Sketch: https://github.com/thehookup/Holiday_LEDS/blob/master/LightsMCU_CONFIGURE.ino

Configuration.yaml Additions: https://github.com/thehookup/Holiday_LEDS/blob/master/yaml.yaml

Groups.yaml Additions: https://github.com/thehookup/Holiday_LEDS/blob/master/groups_additions.yaml

Node-RED Flow: https://github.com/thehookup/Holiday_LEDS/blob/master/node-red-flow.js

Hardware:

NodeMCU: https://amzn.to/2yRYpE7

Logic Level Converter: https://amzn.to/2CPzbKV

5V 60A Power Supply: https://amzn.to/2CQpR9J

WS2812B IP65 Strip: https://amzn.to/2NLtXkh

Aluminum Channels: https://amzn.to/2xTTbrG

3 Core Wire: https://amzn.to/2Ovd01U

Visit my website: http://www.thesmarthomehookup.com

Follow me on Twitter: @TheHookUp1

Support my channel:

Patreon: https://www.patreon.com/thehookup

Music by www.BenSound.com

Related Posts