Software Setup
If this is your first time using Pro Trinket, take a look at Introducting Pro Trinket to get a guided tour.
Once you've got your Pro Trinket up and running with Arduino, you'll need to install the FastLED library. (Sketch > Include Library > Manage Libraries...
)
Libraries? Why? What's a Library?
In a nutshell, Arduino libraries have a lot of pre-written functions that make your neopixels easy to command. You can do fancy stuff without being a code guru. Yay Libraries!
FastLED is a fast, efficient, easy-to-use Arduino library for programming addressable LED strips and pixels. It has a lot of features to get your animations up and running fast -- and it has a lot of code samples available if you're just learning to code.
All about Arduino Libraries will tell you everything you ever wanted to know about libraries, including more detailed installation instructions.
Once your curiosity is satiated and your library is installed, copy and paste the code into your Arduino window.
Go to your Tools menu and select "Pro Trinket 5V USB" from the list of boards. Plug your Pro Trinket into your computer via the onboard USB port. Press the "reset" button on your Pro Trinket and wait for the blinky red light, then click the upload button in Arduino.
//Code by Erin St. Blaine for Adafruit.com #include <FastLED.h> #define LED_PIN 5 #define COLOR_ORDER GRB #define NUM_LEDS 7 int HUE = 0; int SATURATION = 255; int BRIGHTNESS = 255; int STEPS = 7; const byte BUTTON_PIN = 3; uint8_t gHue = 0; // rotating "base color" used by confetti CRGB leds[NUM_LEDS]; TBlendType currentBlending; CRGBPalette16 currentPalette; //BUTTON SETUP STUFF byte prevKeyState = HIGH; // button is active low unsigned long keyPrevMillis = 0; const unsigned long keySampleIntervalMs = 25; byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms byte longKeyPressCount = 0; int ledMode = 0; //FIRST ACTIVE MODE //------------------SETUP------------------ void setup() { delay( 3000 ); // power-up safety delay pinMode(BUTTON_PIN, INPUT_PULLUP); FastLED.addLeds<WS2812B, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); currentBlending = LINEARBLEND; } #define NUM_MODES 6 //------------------MAIN LOOP------------------ void loop() { switch (ledMode) { case 999: break; case 0: confetti(); break; case 1: currentPalette = OceanColors_p; rainbow(); break; case 2: currentPalette = LavaColors_p; rainbow(); break; case 3: currentPalette = ForestColors_p; rainbow(); break; case 4: currentPalette = PartyColors_p; rainbow(); break; case 5: currentPalette = RainbowColors_p; rainbow(); break; case 6: currentPalette = RainbowStripeColors_p; rainbow(); break; case 888: ledMode=0; break; } // key management section if (millis() - keyPrevMillis >= keySampleIntervalMs) { keyPrevMillis = millis(); byte currKeyState = digitalRead(BUTTON_PIN); if ((prevKeyState == HIGH) && (currKeyState == LOW)) { keyPress(); } else if ((prevKeyState == LOW) && (currKeyState == HIGH)) { keyRelease(); } else if (currKeyState == LOW) { longKeyPressCount++; } prevKeyState = currKeyState; } EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow FastLED.show(); FastLED.delay(20); } //BUTTON CONTROL STUFF // called when button is kept pressed for less than 2 seconds void shortKeyPress() { Serial.println("short"); ledMode++; if (ledMode > NUM_MODES){ ledMode=0; } } // called when button is kept pressed for more than 2 seconds void longKeyPress() { Serial.println("long"); ledMode=888; } // called when key goes from not pressed to pressed void keyPress() { Serial.println("key press"); longKeyPressCount = 0; } // called when key goes from pressed to not pressed void keyRelease() { Serial.println("key release"); if (longKeyPressCount >= longKeyPressCountMax) { longKeyPress(); } else { shortKeyPress(); } // other code goes here } // SOLID ---------------------------------------------------- void solid() { fill_solid(leds, NUM_LEDS, CHSV( HUE, SATURATION, BRIGHTNESS)); FastLED.show(); } void rainbow() { static uint8_t startIndex = 0; startIndex = startIndex + 1; /* motion speed */ FillLEDsFromPaletteColors( startIndex); FastLED.show(); FastLED.delay(20); } //this bit is in every palette mode, needs to be in there just once void FillLEDsFromPaletteColors( uint8_t colorIndex) { for( int i = 0; i < NUM_LEDS; i++) { leds[i] = ColorFromPalette( currentPalette, colorIndex, BRIGHTNESS, currentBlending); colorIndex += STEPS; } } 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); FastLED.show(); }
If you encounter trouble…
Any time you hit a roadblock with a neopixel project, we’ll usually ask that you start with the “strandtest” example from our own Adafruit_NeoPixel library. This helps us narrow down whether it’s a hardware or software issue. The library is installed similarly to FastLED or any other in Arduino (Sketch > Include Library > Manage Libraries...
)
You’ll find the strandtest example under File→Sketchbook→Libraries→Adafruit_NeoPixel→strandtest
If strandtest fails to run, this suggests a hardware issue…for example, connecting to the wrong pin on the Pro Trinket.
If you’re new to Arduino programming and LEDs, we usually suggest starting with the Adafruit_NeoPixel library…it’s pretty basic, the strip declaration is more conventional, and we can stay on top of keeping it compatible with our own products and the most mainstream Arduino boards.
As FastLED is a more “bleeding edge” third-party library, we can’t always guarantee compatibility across versions or with specific boards. You can find help through their community on Google+. This is potent stuff, written by people with a deep appreciation for LED art, and we wanted to showcase it.
Page last edited September 16, 2017
Text editor powered by tinymce.