Programming Addressable LED Strips
-
Upload
atuline -
Category
Technology
-
view
442 -
download
10
description
Transcript of Programming Addressable LED Strips
Having fun with an Arduino Nano & FastLED
By: Andrew Tuline
Programming Addressable LED Stripsor
Overview
• Introductions• Install IDE and check out drivers• Included examples• More examples• Some reference material• Create your own project
If you like, you can go at your own pace
Introduction
Work on your own or with partners, and help each other out.
This Workshop
• Builds upon the previous Arduino workshop• Assumes you’re familiar with .zip files, folders
and basic programming• More C++ programming• We’ll run several cool lighting examples
Installing Arduino Software• On Windows 7• Installs to C:\Program Files (x86)\Arduino• Your programs are in C:\Users\userid\Documents\Arduino
• ‘Tools | Board’ should be changed to say ‘Arduino Nano w/ ATmega328’
• In Device Manager, see ‘Ports (COM & LPT)’• Device drivers in C:\Program Files (x86)\Arduino\drivers\FTDI
USB Drivers
Are you running 32 bit or 64 bit?
Touring the IDE• ‘File | Examples’ includes lots of examples• ‘File | Upload’ is how we compile and upload our program to
the Arduino• ‘Help | Reference’ includes a local copy of the Language
Reference
Make GOOD use of those examples!
Test the Drivers
Declare constants & global variables
One time setup of ports
Continuous loop
Test your drivers by compiling and uploading Blink.ino
Arduino Nano Pinout
• Digital pins (input/output)• Analog pins (input/output)• Vin, Ground pins• 5V, 3.3V pins• Analog out is PWM• Most pins support other functions
This project just uses Vin, 5V, Ground and a digital pin
Addressable LED Strips
• Traditionally, we assign output values to each R, G and B pin on each LED
• This can use up a LOT of pins• Addressable LED strips provide circuitry to
support data being sent over a single pin to multiple RGB LED’s
LED Strip Connections
What is FastLED?
• FastLED is a fast, easy-to-use Arduino library for programming addressable LED strips.
• FastLED is used by thousands of developers.
Do a YouTube search for ‘FastLED’
Downloading FastLED
• The latest version is on GitHub at: https://github.com/FastLED/FastLED
• Download the zip file• Extract the directory FastLED-master• Rename the directory from:
FastLED-master to FastLED• Place that directory into: C:\Program Files(x86)\Arduino\libraries
FastLED Examples
• In the Arduino IDE, select‘File | Examples | FastLED | FirstLight’
• We need to modify the code in order to define the correct data pin.
• We also need to change the number of LED’s we’re using.
FirstLight#include "FastLED.h"
#define NUM_LEDS 10#define DATA_PIN 13uint8_t max_bright = 64;
CRGB leds[NUM_LEDS];
void setup() { delay(2000); FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); FastLED.setBrightness(max_bright);}
void loop() { for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed++) { leds[whiteLed] = CRGB::White; FastLED.show(); delay(100); leds[whiteLed] = CRGB::Black; }}
The white LED should march across the array.
// Include the library
// Number of LED’s// Our data pin// Define brightness limiter
// Define the LED array
// Run once// Power up delay for safety// Initialize the LED array// Limit the brightness
// Run continuously// Cycle through each LED in the array// Set the current one white// Show it// Wait for a bit// Set it to black
Setting LED values
• leds[9] = CRGB::Cyan; // Web colours• leds[9].g = 255; // Just set green• leds[0] = CRGB(255,0,0); // CRGB values• leds[0] = 0x0000ff; // Use a hex #
FastLED provides even more ways to assign colours to LED’s
Variable Types & Scope
• You can define a variable to be global or local– Global is good for anywhere in the program– Local to a function or to a loop
• Variable types include:– int (-32768 to 32767 is 16 bit signed)– uint (0 to 65535 is 16 bit unsigned)– uint8_t (0 to 255 is 8 bit unsigned)– long (is 32 bits signed)– and many more. . .
Random Numberslong num = random(); // 32 bituint8_t unum = random8(); // 8 bitint inum = random16(0, 1023); // 16 bit
• The ‘long’ can be used to assign random RGB colours• The ‘uint8_t’ can be used to assign hue, brightness values• It can also refer to LED elements in your strip
random8() and random16() are FastLED functions
If Statements
This example occasionally flashes a random colour on a random LED.
void loop() { uint8_t j = random16(0, 100); // Which LED will we light up if ( j < NUM_LEDS ) { // Only display if that number IS in our array
leds[j] = random(); // 32 bit randomizer for colourFastLED.show();
delay(100); leds[j] = CRGB::Black; // Make that LED black again }}
For LoopsThis example sets each LED a different colour, waits 2 seconds and repeats.
void loop() { for (int i = 0; i < NUM_LEDS; i++) { // Last element is leds[NUM_LEDS-1] leds[i] = random(); // A 32 bit random number } FastLED.show(); delay(2000);}
We’re Just Getting Started
FastLED contains a LOT of functions including:• Random number generation• Fast 8 and 16 bit basic math functions• Fast trigonometry• Setting RGB and HSV values• fill_rainbow, fill_gradient, fading, scaling, blending, noise,
palettes• Power management
We’re talking FAST
Rainbow March#include "FastLED.h"
#define LED_DT 13 // Data pin#define NUM_LEDS 10 // Number of LED's#define COLOR_ORDER GRB // Change the order as necessary#define LED_TYPE WS2812B // What kind of strip are you using?uint8_t max_bright = 64; // How bright do we want to go
struct CRGB leds[NUM_LEDS]; // Initialize our array
uint8_t thisdelay = 8; // A delay value for the sequenceuint8_t thishue = 0; // Starting hue value.int8_t thisrot = 1; // Hue rotation speed. Can be a negative #.uint8_t deltahue = 5; // Difference in hue between LED’s
Use variables where possible
Rainbow March cont’dvoid setup() { LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); FastLED.setBrightness(max_bright);}
void loop () { rainbow_march(); FastLED.show(); delay(thisdelay);}
void rainbow_march() { thishue = thishue + thisrot; fill_rainbow (leds, NUM_LEDS, thishue, deltahue); // FastLED does the heavy lifting}
More FastLED Demos
• There’s also several 3rd party demos at:https://github.com/atuline/FastLED-Demos
• Unzip and store them in:C:\users\<username>\Documents\Arduino
aatemplate definitions#include "FastLED.h" // FastLED library. Preferably the latest copy of FastLED 2.1. // Fixed definitions cannot change on the fly.#define LED_DT 13 // Serial data pin for the strip.#define COLOR_ORDER GRB // Are they RGB, GRB or what??#define LED_TYPE WS2812B // What kind of strip are you using?#define NUM_LEDS 10 // Number of LED's.
// Initialize changeable global variables.uint8_t max_bright = 255; // Overall brightness definition. It can be changed on the fly.
struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
// Define variables used by the sequences.int twinkrate = 100; // The higher the value, the lower the number of twinkles.uint8_t thisdelay = 20; // A delay value for the sequence(s).uint8_t thisfade = 8; // How quickly does it fade? Lower = slower fade rate.uint8_t thishue = 50; // The hue.uint8_t thissat = 100; // The saturation, where 255 = brilliant colours.uint8_t thisbri = 255; // Brightness of a sequence.
aatemplate setup()void setup() { delay(1000); Serial.begin(57600); LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); FastLED.setBrightness(max_bright); set_max_power_in_volts_and_milliamps(5, 500);}
Power management is critical for large arrays of LED’s
aatemplate loop
void loop () { ChangeMe(); // Change variable values on the fly. twinkle(); // Call our display sequence. show_at_max_brightness_for_power(); // Power managed display of LED's. delay_at_max_brightness_for_power(2.5*thisdelay); // Power managed FastLED delay. Serial.println(LEDS.getFPS()); // Display frames per second.} // loop()
void twinkle() { if (twinkrate < NUM_LEDS) twinkrate = NUM_LEDS; // Must be at least NUM_LEDS. int i = random16(twinkrate); // Higher number => fewer twinkles. if (i < NUM_LEDS) { leds[i] = CHSV(thishue, thissat, thisbri); // You could randomize these. } for (int j = 0; j < NUM_LEDS; j++) { leds[j].fadeToBlackBy(thisfade); // Use the FastLED 2.1 fade method. }} // twinkle()
aatemplate completevoid ChangeMe() { // A time (rather than loop) based demo sequencer. uint8_t secondHand = (millis() / 1000) % 15; // Change ‘15' to a different value for a longer demo. static uint8_t lastSecond = 99; // Static variable, only defined once. if (lastSecond != secondHand) { // Debounce to make sure we're not repeating. lastSecond = secondHand; if (secondHand == 0) {thisdelay = 20; thishue=0; thissat=255; thisfade=16; thisdelay=50;
twinkrate=20;} if (secondHand == 5) {thisdelay = 20; thishue=128; thisfade=64; thisdelay=10; twinkrate=100;} if (secondHand == 10) {thisdelay = 20; thishue=random16(0,64); twinkrate=40;} if (secondHand == 15) {} if (secondHand == 20) {} if (secondHand == 25) {} if (secondHand == 30) {} if (secondHand == 35) {} if (secondHand == 40) {} if (secondHand == 45) {} if (secondHand == 50) {} if (secondHand == 55) {} }} // ChangeMe()
Try More Demo’s
• Try one_sine_demo• Try pop_fade_demo• Try ripple• Try two_sin_demo• Try noise16_demo• Try three_sin_demo• Try rainbow_march_demo
aalight
• This demo combines several sequences• It can support a microphone• It can support IR control• It supports keyboard commands• It also supports a pushbutton• It supports timed demo modes• Set the starting sequence to 99 in order to
loop through several different sequences.
Google for more FastLED
Arduino Research
• www.fastled.io• github.com• arduino.cc/tutorial• playground.arduino.cc• tronixstuff.com/tutorials• learn.sparkfun.com/tutorials• oomlout.com/a/products/ardx
Don’t forget the examples included with the Arduino software
Inspirational Web Sites
• www.instructables.com• www.tindie.com• www.makershed.com• www.makezine.com
Sources for Parts
• www.robotshop.ca• www.sparkfun.com• www.leeselectronics.com• www.gravitech.us• www.adafruit.com• www.ebay.ca• www.aliexpress.com• www.amazon.com• www.dealextreme.com
All it takes is a VISA card and your imagination
Local Events
• North Delta Luminary Festival• Vancouver Mini Maker Faire• WestSurDel Fun Night• Vancouver Bike Rave• Any night market
Then there’s Burning Man
University
Hardware Hacker
https://diy.org/skills/hardwarehacker
Light Something