Difference between revisions of "NeoPixel Workshop Instructions"
(→Code) |
(→Code) |
||
Line 30: | Line 30: | ||
= Code = | = Code = | ||
− | + | == Color Pallet.ino == | |
<pre> | <pre> | ||
Line 180: | Line 180: | ||
CRGB::Black | CRGB::Black | ||
}; | }; | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | == Demo Reel.ino == | ||
+ | |||
+ | <pre> | ||
+ | #include "FastLED.h" | ||
+ | |||
+ | |||
+ | #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000) | ||
+ | #warning "Requires FastLED 3.1 or later; check github for latest code." | ||
+ | #endif | ||
+ | |||
+ | #define DATA_PIN 6 | ||
+ | #define LED_TYPE WS2811 | ||
+ | #define COLOR_ORDER GRB | ||
+ | #define NUM_LEDS 60 | ||
+ | CRGB leds[NUM_LEDS]; | ||
+ | |||
+ | #define BRIGHTNESS 96 | ||
+ | #define FRAMES_PER_SECOND 80 | ||
+ | |||
+ | void setup() { | ||
+ | delay(3000); // 3 second delay for recovery | ||
+ | |||
+ | // tell FastLED about the LED strip configuration | ||
+ | FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | ||
+ | //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | ||
+ | |||
+ | // set master brightness control | ||
+ | FastLED.setBrightness(BRIGHTNESS); | ||
+ | } | ||
+ | |||
+ | |||
+ | // List of patterns to cycle through. Each is defined as a separate function below. | ||
+ | typedef void (*SimplePatternList[])(); | ||
+ | SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm }; | ||
+ | |||
+ | uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current | ||
+ | uint8_t gHue = 0; // rotating "base color" used by many of the patterns | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | // Call the current pattern function once, updating the 'leds' array | ||
+ | gPatterns[gCurrentPatternNumber](); | ||
+ | |||
+ | // send the 'leds' array out to the actual LED strip | ||
+ | FastLED.show(); | ||
+ | // insert a delay to keep the framerate modest | ||
+ | FastLED.delay(1000/FRAMES_PER_SECOND); | ||
+ | |||
+ | // do some periodic updates | ||
+ | EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow | ||
+ | EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically | ||
+ | } | ||
+ | |||
+ | #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) | ||
+ | |||
+ | void nextPattern() | ||
+ | { | ||
+ | // add one to the current pattern number, and wrap around at the end | ||
+ | gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns); | ||
+ | } | ||
+ | |||
+ | void rainbow() | ||
+ | { | ||
+ | // FastLED's built-in rainbow generator | ||
+ | fill_rainbow( leds, NUM_LEDS, gHue, 7); | ||
+ | } | ||
+ | |||
+ | void rainbowWithGlitter() | ||
+ | { | ||
+ | // built-in FastLED rainbow, plus some random sparkly glitter | ||
+ | rainbow(); | ||
+ | addGlitter(80); | ||
+ | } | ||
+ | |||
+ | void addGlitter( fract8 chanceOfGlitter) | ||
+ | { | ||
+ | if( random8() < chanceOfGlitter) { | ||
+ | leds[ random16(NUM_LEDS) ] += CRGB::White; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void confetti() | ||
+ | { | ||
+ | // random colored speckles that blink in and fade smoothly | ||
+ | fadeToBlackBy( leds, NUM_LEDS, 10); | ||
+ | int pos = random16(NUM_LEDS); | ||
+ | leds[pos] += CHSV( gHue + random8(64), 200, 255); | ||
+ | } | ||
+ | |||
+ | void sinelon() | ||
+ | { | ||
+ | // a colored dot sweeping back and forth, with fading trails | ||
+ | fadeToBlackBy( leds, NUM_LEDS, 20); | ||
+ | int pos = beatsin16( 13, 0, NUM_LEDS-1 ); | ||
+ | leds[pos] += CHSV( gHue, 255, 192); | ||
+ | } | ||
+ | |||
+ | void bpm() | ||
+ | { | ||
+ | // colored stripes pulsing at a defined Beats-Per-Minute (BPM) | ||
+ | uint8_t BeatsPerMinute = 62; | ||
+ | CRGBPalette16 palette = PartyColors_p; | ||
+ | uint8_t beat = beatsin8( BeatsPerMinute, 64, 255); | ||
+ | for( int i = 0; i < NUM_LEDS; i++) { //9948 | ||
+ | leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10)); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void juggle() { | ||
+ | // eight colored dots, weaving in and out of sync with each other | ||
+ | fadeToBlackBy( leds, NUM_LEDS, 20); | ||
+ | byte dothue = 0; | ||
+ | for( int i = 0; i < 8; i++) { | ||
+ | leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255); | ||
+ | dothue += 32; | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | == Fire Nash.ino == | ||
+ | |||
+ | <pre> | ||
+ | #include <Adafruit_NeoPixel.h> | ||
+ | #define PIN 6 | ||
+ | #define NUM_LEDS 144 | ||
+ | // Parameter 1 = number of pixels in strip | ||
+ | // Parameter 2 = pin number (most are valid) | ||
+ | // Parameter 3 = pixel type flags, add together as needed: | ||
+ | // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) | ||
+ | // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) | ||
+ | // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) | ||
+ | // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) | ||
+ | Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800); | ||
+ | |||
+ | void setup() { | ||
+ | strip.begin(); | ||
+ | strip.show(); // Initialize all pixels to 'off' | ||
+ | } | ||
+ | |||
+ | // *** REPLACE FROM HERE *** | ||
+ | void loop() { | ||
+ | Fire(55,120,15); | ||
+ | } | ||
+ | |||
+ | void Fire(int Cooling, int Sparking, int SpeedDelay) { | ||
+ | static byte heat[NUM_LEDS]; | ||
+ | int cooldown; | ||
+ | |||
+ | // Step 1. Cool down every cell a little | ||
+ | for( int i = 0; i < NUM_LEDS; i++) { | ||
+ | cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2); | ||
+ | |||
+ | if(cooldown>heat[i]) { | ||
+ | heat[i]=0; | ||
+ | } else { | ||
+ | heat[i]=heat[i]-cooldown; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Step 2. Heat from each cell drifts 'up' and diffuses a little | ||
+ | for( int k= NUM_LEDS - 1; k >= 2; k--) { | ||
+ | heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3; | ||
+ | } | ||
+ | |||
+ | // Step 3. Randomly ignite new 'sparks' near the bottom | ||
+ | if( random(255) < Sparking ) { | ||
+ | int y = random(7); | ||
+ | heat[y] = heat[y] + random(160,255); | ||
+ | //heat[y] = random(160,255); | ||
+ | } | ||
+ | |||
+ | // Step 4. Convert heat to LED colors | ||
+ | for( int j = 0; j < NUM_LEDS; j++) { | ||
+ | setPixelHeatColor(j, heat[j] ); | ||
+ | } | ||
+ | |||
+ | showStrip(); | ||
+ | delay(SpeedDelay); | ||
+ | } | ||
+ | |||
+ | void setPixelHeatColor (int Pixel, byte temperature) { | ||
+ | // Scale 'heat' down from 0-255 to 0-191 | ||
+ | byte t192 = round((temperature/255.0)*191); | ||
+ | |||
+ | // calculate ramp up from | ||
+ | byte heatramp = t192 & 0x3F; // 0..63 | ||
+ | heatramp <<= 2; // scale up to 0..252 | ||
+ | |||
+ | // figure out which third of the spectrum we're in: | ||
+ | if( t192 > 0x80) { // hottest | ||
+ | setPixel(Pixel, 255, 255, heatramp); | ||
+ | } else if( t192 > 0x40 ) { // middle | ||
+ | setPixel(Pixel, 255, heatramp, 0); | ||
+ | } else { // coolest | ||
+ | setPixel(Pixel, heatramp, 0, 0); | ||
+ | } | ||
+ | } | ||
+ | // *** REPLACE TO HERE *** | ||
+ | |||
+ | void showStrip() { | ||
+ | #ifdef ADAFRUIT_NEOPIXEL_H | ||
+ | // NeoPixel | ||
+ | strip.show(); | ||
+ | #endif | ||
+ | #ifndef ADAFRUIT_NEOPIXEL_H | ||
+ | // FastLED | ||
+ | FastLED.show(); | ||
+ | #endif | ||
+ | } | ||
+ | |||
+ | void setPixel(int Pixel, byte red, byte green, byte blue) { | ||
+ | #ifdef ADAFRUIT_NEOPIXEL_H | ||
+ | // NeoPixel | ||
+ | strip.setPixelColor(Pixel, strip.Color(red, green, blue)); | ||
+ | #endif | ||
+ | #ifndef ADAFRUIT_NEOPIXEL_H | ||
+ | // FastLED | ||
+ | leds[Pixel].r = red; | ||
+ | leds[Pixel].g = green; | ||
+ | leds[Pixel].b = blue; | ||
+ | #endif | ||
+ | } | ||
+ | |||
+ | void setAll(byte red, byte green, byte blue) { | ||
+ | for(int i = 0; i < NUM_LEDS; i++ ) { | ||
+ | setPixel(i, red, green, blue); | ||
+ | } | ||
+ | showStrip(); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | == Demo Reel.ino == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | </pre> | ||
+ | == Demo Reel.ino == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | </pre> | ||
+ | == Demo Reel.ino == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | </pre> | ||
+ | == Demo Reel.ino == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | </pre> | ||
+ | == Demo Reel.ino == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | </pre> | ||
+ | == Demo Reel.ino == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | </pre> | ||
+ | == Demo Reel.ino == | ||
+ | |||
+ | <pre> | ||
</pre> | </pre> |
Revision as of 20:26, 10 November 2017
This is the instruction page for the NeoPixel Workshop ran by Andy Miller on 11/11/17
Contents
Software to install
You will need to install the following programs and drivers
Download and install the Arduino IDE V 1.8.5 (or newer).
Do not download/install the web IDE!
https://www.arduino.cc/en/Main/Software
There are some step by step instructions here: https://www.arduino.cc/en/Guide/HomePage
Download and install the CH340 USB drivers for the Arduino clones:
https://sparks.gogo.co.nz/ch340.html
Note: The CH340 chip is a USB to serial chip used frequently on Arduino clones. Official Arduinos use the FTDI chip. Those drivers are included in the Arduino software install.
Download the FastLED library from:
https://github.com/FastLED/FastLED/releases Current version is 3.1.6
We will show you how to install this library. The FastLED library is used to control NeoPixels! The official page for the FASTLED library is:
https://github.com/FastLED/FastLED/tree/v3.1.6
Code
Color Pallet.ino
#include <FastLED.h> #define LED_PIN 6 #define NUM_LEDS 60 #define BRIGHTNESS 96 #define LED_TYPE WS2811 #define COLOR_ORDER GRB CRGB leds[NUM_LEDS]; #define UPDATES_PER_SECOND 100 // FastLED provides a few pre-configured color palettes, and makes it // extremely easy to make up your own color schemes with palettes. // // Some notes on the more abstract 'theory and practice' of // FastLED compact palettes are at the bottom of this file. CRGBPalette16 currentPalette; TBlendType currentBlending; extern CRGBPalette16 myRedWhiteBluePalette; extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM; void setup() { delay( 3000 ); // power-up safety delay FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; } void loop() { ChangePalettePeriodically(); static uint8_t startIndex = 0; startIndex = startIndex + 1; /* motion speed */ FillLEDsFromPaletteColors( startIndex); FastLED.show(); FastLED.delay(1000 / UPDATES_PER_SECOND); } void FillLEDsFromPaletteColors( uint8_t colorIndex) { uint8_t brightness = 255; for( int i = 0; i < NUM_LEDS; i++) { leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); colorIndex += 3; } } void ChangePalettePeriodically() { uint8_t secondHand = (millis() / 1000) % 60; static uint8_t lastSecond = 99; if( lastSecond != secondHand) { lastSecond = secondHand; if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; } if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; } if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; } if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; } if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; } if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; } if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; } if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; } if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; } if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; } if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; } } } // This function fills the palette with totally random colors. void SetupTotallyRandomPalette() { for( int i = 0; i < 16; i++) { currentPalette[i] = CHSV( random8(), 255, random8()); } } // This function sets up a palette of black and white stripes, // using code. Since the palette is effectively an array of // sixteen CRGB colors, the various fill_* functions can be used // to set them up. void SetupBlackAndWhiteStripedPalette() { // 'black out' all 16 palette entries... fill_solid( currentPalette, 16, CRGB::Black); // and set every fourth one to white. currentPalette[0] = CRGB::White; currentPalette[4] = CRGB::White; currentPalette[8] = CRGB::White; currentPalette[12] = CRGB::White; } // This function sets up a palette of purple and green stripes. void SetupPurpleAndGreenPalette() { CRGB purple = CHSV( HUE_PURPLE, 255, 255); CRGB green = CHSV( HUE_GREEN, 255, 255); CRGB black = CRGB::Black; currentPalette = CRGBPalette16( green, green, black, black, purple, purple, black, black, green, green, black, black, purple, purple, black, black ); } // This example shows how to set up a static color palette // which is stored in PROGMEM (flash), which is almost always more // plentiful than RAM. A static PROGMEM palette like this // takes up 64 bytes of flash. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM = { CRGB::Red, CRGB::Gray, // 'white' is too bright compared to red and blue CRGB::Blue, CRGB::Black, CRGB::Red, CRGB::Gray, CRGB::Blue, CRGB::Black, CRGB::Red, CRGB::Red, CRGB::Gray, CRGB::Gray, CRGB::Blue, CRGB::Blue, CRGB::Black, CRGB::Black };
Demo Reel.ino
#include "FastLED.h" #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000) #warning "Requires FastLED 3.1 or later; check github for latest code." #endif #define DATA_PIN 6 #define LED_TYPE WS2811 #define COLOR_ORDER GRB #define NUM_LEDS 60 CRGB leds[NUM_LEDS]; #define BRIGHTNESS 96 #define FRAMES_PER_SECOND 80 void setup() { delay(3000); // 3 second delay for recovery // tell FastLED about the LED strip configuration FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // set master brightness control FastLED.setBrightness(BRIGHTNESS); } // List of patterns to cycle through. Each is defined as a separate function below. typedef void (*SimplePatternList[])(); SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm }; uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current uint8_t gHue = 0; // rotating "base color" used by many of the patterns void loop() { // Call the current pattern function once, updating the 'leds' array gPatterns[gCurrentPatternNumber](); // send the 'leds' array out to the actual LED strip FastLED.show(); // insert a delay to keep the framerate modest FastLED.delay(1000/FRAMES_PER_SECOND); // do some periodic updates EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically } #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) void nextPattern() { // add one to the current pattern number, and wrap around at the end gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns); } void rainbow() { // FastLED's built-in rainbow generator fill_rainbow( leds, NUM_LEDS, gHue, 7); } void rainbowWithGlitter() { // built-in FastLED rainbow, plus some random sparkly glitter rainbow(); addGlitter(80); } void addGlitter( fract8 chanceOfGlitter) { if( random8() < chanceOfGlitter) { leds[ random16(NUM_LEDS) ] += CRGB::White; } } void confetti() { // random colored speckles that blink in and fade smoothly fadeToBlackBy( leds, NUM_LEDS, 10); int pos = random16(NUM_LEDS); leds[pos] += CHSV( gHue + random8(64), 200, 255); } void sinelon() { // a colored dot sweeping back and forth, with fading trails fadeToBlackBy( leds, NUM_LEDS, 20); int pos = beatsin16( 13, 0, NUM_LEDS-1 ); leds[pos] += CHSV( gHue, 255, 192); } void bpm() { // colored stripes pulsing at a defined Beats-Per-Minute (BPM) uint8_t BeatsPerMinute = 62; CRGBPalette16 palette = PartyColors_p; uint8_t beat = beatsin8( BeatsPerMinute, 64, 255); for( int i = 0; i < NUM_LEDS; i++) { //9948 leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10)); } } void juggle() { // eight colored dots, weaving in and out of sync with each other fadeToBlackBy( leds, NUM_LEDS, 20); byte dothue = 0; for( int i = 0; i < 8; i++) { leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255); dothue += 32; } }
Fire Nash.ino
#include <Adafruit_NeoPixel.h> #define PIN 6 #define NUM_LEDS 144 // Parameter 1 = number of pixels in strip // Parameter 2 = pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800); void setup() { strip.begin(); strip.show(); // Initialize all pixels to 'off' } // *** REPLACE FROM HERE *** void loop() { Fire(55,120,15); } void Fire(int Cooling, int Sparking, int SpeedDelay) { static byte heat[NUM_LEDS]; int cooldown; // Step 1. Cool down every cell a little for( int i = 0; i < NUM_LEDS; i++) { cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2); if(cooldown>heat[i]) { heat[i]=0; } else { heat[i]=heat[i]-cooldown; } } // Step 2. Heat from each cell drifts 'up' and diffuses a little for( int k= NUM_LEDS - 1; k >= 2; k--) { heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3; } // Step 3. Randomly ignite new 'sparks' near the bottom if( random(255) < Sparking ) { int y = random(7); heat[y] = heat[y] + random(160,255); //heat[y] = random(160,255); } // Step 4. Convert heat to LED colors for( int j = 0; j < NUM_LEDS; j++) { setPixelHeatColor(j, heat[j] ); } showStrip(); delay(SpeedDelay); } void setPixelHeatColor (int Pixel, byte temperature) { // Scale 'heat' down from 0-255 to 0-191 byte t192 = round((temperature/255.0)*191); // calculate ramp up from byte heatramp = t192 & 0x3F; // 0..63 heatramp <<= 2; // scale up to 0..252 // figure out which third of the spectrum we're in: if( t192 > 0x80) { // hottest setPixel(Pixel, 255, 255, heatramp); } else if( t192 > 0x40 ) { // middle setPixel(Pixel, 255, heatramp, 0); } else { // coolest setPixel(Pixel, heatramp, 0, 0); } } // *** REPLACE TO HERE *** void showStrip() { #ifdef ADAFRUIT_NEOPIXEL_H // NeoPixel strip.show(); #endif #ifndef ADAFRUIT_NEOPIXEL_H // FastLED FastLED.show(); #endif } void setPixel(int Pixel, byte red, byte green, byte blue) { #ifdef ADAFRUIT_NEOPIXEL_H // NeoPixel strip.setPixelColor(Pixel, strip.Color(red, green, blue)); #endif #ifndef ADAFRUIT_NEOPIXEL_H // FastLED leds[Pixel].r = red; leds[Pixel].g = green; leds[Pixel].b = blue; #endif } void setAll(byte red, byte green, byte blue) { for(int i = 0; i < NUM_LEDS; i++ ) { setPixel(i, red, green, blue); } showStrip(); }