Installing Arduino

You'll need to customize some settings in the Arduino IDE to get configured to compile code onto the Adafruit Pro Trinket.  Be sure to check out the Introduction to Pro Trinket guide for setting that up and than download and install the NeoPixel Library.

Uploading Sketches

With the Adafruit Arduino IDE and NeoPixel library installed, create a new sketch. Paste the desired code into the text editor. Select Pro Trinket from Tools > Board. Then USBtinyISP from Tool > Programmmer. Proceede by pluging a micro USB cable from your computer to the Pro Trinket. Watch for the red LED to start blinking and click upload to compile the sketch onto the Pro Trinket. If everything is good, Arduino will prompt with you a successful message in the console window.

Muzzle Flash Blast

The code below was written by Tony DiCola. The basic idea is to pass it a strip of pixels, starting RGB color, ending RGB color, and total duration in milliseconds.  Then it will loop through and animate all the pixels going from the start to end color over that period of time.  Just one call to go from white-ish to black works well for a simple flash, but you can get crazy and make multiple calls to flash between different colors and fade out.

Linear Interpolation 

Tony put together a color demo to show linear interpolation can be used to generate color gradients. The start and end colors dynamically change as you drag the slider, it shows the intermediate colors and equations that generate them.

//Written by Tony DiCola for Adafruit Industries.
//Adafruit invests time and resources providing this open source code,
//please support Adafruit and open-source hardware by purchasing products
//from Adafruit!
//BSD license, all text above must be included in any redistribution.

#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN    3  // Pin connected to neo pixels
#define FIREPIN      4  // Fire button
#define PIXEL_COUNT  12  // Count of neo pixels

int buttonState = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show();

  pinMode(FIREPIN, INPUT_PULLUP);

}

void loop() { 
  uint8_t  i;
  //Button switch
  buttonState = digitalRead(FIREPIN);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {     
  // Run It:    

  // Nice flash and fade out over about 3/4 of a second:
  animate_gradient_fill(strip,         
                      255, 255, 255,
                      255, 0, 0,
                      150);
  // Then flash from purple to nothing over a longer period.
  animate_gradient_fill(strip,         
                      255, 0, 0,
                      20, 0, 0,
                      150);
  animate_gradient_fill(strip,         
                      20, 0, 0,
                      0, 0, 0,
                      150);
  } 
  
  else {
     strip.setPixelColor(i, 0,0,0); //Button not pressed, turn off pixels
     strip.show(); //Show no pixels
  }
 
  // More complex flash between two colors and fade out using multiple calls.
  // First flash from red to purple over a short period
  //animate_gradient_fill(strip,         // NeoPixel strip/loop/etc. object.
  //                      255, 128, 128, // Starting R,G,B color.
  //                      0, 0, 0,       // Ending R,G,B color (0,0,0 for black or off).
  //                      500);          // Total duration of the animation in milliseconds.
  //
  //animate_gradient_fill(strip,         
  //                    255, 0, 0,
  //                    128, 0, 255,
  //                    200);
  // Then flash from purple to nothing over a longer period.
  //animate_gradient_fill(strip,         
  //                      128, 0, 255,
  //                      0, 0, 0,
  //                      400);                  
  // Delay to see different animations.
  //delay(1000);
}

// Linear interpolation of y value given min/max x, min/max y, and x position.
float lerp(float x, float x0, float x1, float y0, float y1)
{
  // Clamp x within x0 and x1 bounds.
  x = x > x1 ? x1 : x;
  x = x < x0 ? x0 : x;
  // Calculate linear interpolation of y value.
  return y0 + (y1-y0)*((x-x0)/(x1-x0));
}

// Get the color of a pixel within a smooth gradient of two colors.
uint32_t color_gradient(uint8_t start_r, // Starting R,G,B color
                        uint8_t start_g,
                        uint8_t start_b, 
                        uint8_t end_r,   // Ending R,G,B color
                        uint8_t end_g,
                        uint8_t end_b,
                        float pos)       // Position along gradient, should be a value 0 to 1.0
{
  // Interpolate R,G,B values and return them as a color.  
  uint8_t red   = (uint8_t) lerp(pos, 0.0, 1.0, start_r, end_r);
  uint8_t green = (uint8_t) lerp(pos, 0.0, 1.0, start_g, end_g);
  uint8_t blue  = (uint8_t) lerp(pos, 0.0, 1.0, start_b, end_b);
  return Adafruit_NeoPixel::Color(red, green, blue);
}

// Set all pixels to the specified color.
void fill_pixels(Adafruit_NeoPixel& pixels, uint32_t color)
{
  for (int i=0; i < pixels.numPixels(); ++i) {
    pixels.setPixelColor(i, color);
  }
  strip.show();
}

void animate_gradient_fill(Adafruit_NeoPixel& pixels, // NeoPixel strip/loop/etc.
                           uint8_t start_r,           // Starting R,G,B color
                           uint8_t start_g,
                           uint8_t start_b, 
                           uint8_t end_r,             // Ending R,G,B color
                           uint8_t end_g,
                           uint8_t end_b,
                           int duration_ms)           // Total duration of animation, in milliseconds
{
  unsigned long start = millis();
  // Display start color.
  fill_pixels(pixels, Adafruit_NeoPixel::Color(start_r, start_g, start_b));
  // Main animation loop.
  unsigned long delta = millis() - start;
  while (delta < duration_ms) {
    // Calculate how far along we are in the duration as a position 0...1.0
    float pos = (float)delta / (float)duration_ms;
    // Get the gradient color and fill all the pixels with it.
    uint32_t color = color_gradient(start_r, start_g, start_b, end_r, end_g, end_b, pos);
    fill_pixels(pixels, color);
    // Update delta and repeat.
    delta = millis() - start;
  }
  // Display end color.
  fill_pixels(pixels, Adafruit_NeoPixel::Color(end_r, end_g, end_b));
}

Trigger Spinny Animation

The code below was orignally written by Phillip Burgress and used in the Kaleidoscope project but is slightly modified to fire the animation when a push button is pressed. 

// Original Kaleidoscope eyes code from Phillip Burgess. 
// Modified to play spinning animation when push button is set to low
//Written by Philip Burgess for Adafruit Industries.
//Adafruit invests time and resources providing this open source code,
//please support Adafruit and open-source hardware by purchasing products
//from Adafruit!
//BSD license, all text above must be included in any redistribution.

#include <Adafruit_NeoPixel.h>

#define PIN          0  //NeoPixel Pin
#define FIREPIN      2  // Fire button

int buttonState = 0; //Default button state

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(12, PIN);


uint8_t         offset = 0;         // Position of spinny eyes
uint32_t color  = 0x00ff5a;  // HEX color here
uint32_t prevTime;

void setup() {
  pixels.begin();
  prevTime = millis();
  pinMode(FIREPIN, INPUT_PULLUP); //Button enabler
}

void loop() {
  uint8_t  i;
  uint32_t t;
  buttonState = digitalRead(FIREPIN); //Trigger button state
  if (buttonState == LOW) { // If button pressed
    for(i=0; i<16; i++) { //Run the animation
      uint32_t c = 0; //Blank color
      if(((offset + i) & 7) < 2) c = color; // 4 pixels on...
      pixels.setPixelColor(i, c); //Set the color of pixels
    }
    pixels.show();
    offset++;
  }

  t = millis();
  if((t - prevTime) > 10) {      // Every 10th of a second...
    
    for(i=0; i<32; i++) pixels.setPixelColor(i, 0);
    prevTime = t;
    }

  else {
    pixels.setPixelColor(i, 0,0,0); //Button not pressed, turn off pixels
    pixels.show(); //Show no pixels
    delay(25); //duration of spinniness
  }
}

This guide was first published on Nov 04, 2014. It was last updated on Mar 08, 2024.

This page (Software) was last updated on Mar 08, 2024.

Text editor powered by tinymce.