Richard Albritton
Published © GPL3+

Adafruit Trinket Color/Mode

This simple build will help to add some LED color to any project.

IntermediateFull instructions provided2 hours1,836
Adafruit Trinket Color/Mode

Things used in this project

Hardware components

Adafruit Trinket -5V
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
NeoPixel strip
NeoPixel strip
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1
USB Li Ion Battery Charger
Adafruit USB Li Ion Battery Charger
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Color Mode

Code

Color Mode

Arduino
This is the basic color mode code. This code uses two Switch statements to handel the colors and animation modes.
//Color and Patern Mode selector for NeoPixels
//Uses C_BUTTON to sycle through 9 colors and P_BUTTON to select any of the 8 patern sequanses.
// 64 total options are avalable
//code by Richard Albritton

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>

// Interupt Code start
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
// Interupt Code end

#define PIN 0
#define Sensor 1
#define C_BUTTON 4
#define P_BUTTON 2
#define Pixels 24

Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixels, PIN, NEO_GRB + NEO_KHZ800);

int C_MODE = 4; // Current color mode.
int P_MODE = 7; // Current pattern mode.
int STrigger = 0; // This tells us if the sensor interupt was triggered.
int R = 0;
int G = 50;
int B = 50;
long randNumber;
int wait = 10;

void setup(){
     // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  pinMode(C_BUTTON, INPUT_PULLUP);
  pinMode(P_BUTTON, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  pinMode(Sensor,INPUT); // Interupt Code
  sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
  sbi(PCMSK,PCINT1); // Which pins are affected by the interrupt
}

void loop(){
ColorSelect(); 
PatternSelect();
    //colorMode(C_MODE);
    patternMode(P_MODE);
    STrigger = 0;
}
void ColorSelect(){
  while (digitalRead(P_BUTTON) == LOW) {
    colorChange(strip.Color(R/4, G/4, B/4));
    while (digitalRead(C_BUTTON) == LOW) {
      delay(500);
      C_MODE += 1;
      if (C_MODE > 9) {
        C_MODE = 1; 
      }
      colorMode(C_MODE);
      colorChange(strip.Color(R/4, G/4, B/4));
    } 
  }
} 
void PatternSelect(){
  while(digitalRead(C_BUTTON) == LOW) {
    //colorChange(strip.Color(R/4, G/4, B/4));
    while(digitalRead(P_BUTTON) == LOW) {
      delay(500);
      P_MODE += 1;
      if (P_MODE > 9) {
        P_MODE = 1; 
      }
      patternMode(P_MODE);
    }
  }
} 
// Fill the all dots at once
void colorChange(uint32_t c) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
  }
  strip.show();
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(50);
  }
}

// Fill the dots one after the other with a color
void Sparkle(uint32_t c) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(50);
  }
}

void colorMode(uint32_t m) {
  switch (m) {
    case 1: // Red
      R = 100;
      G = 0;
      B = 0;
      break;
    case 2: // Orange
      R = 75;
      G = 25;
      B = 0;
      break;
    case 3: // Yellow
      R = 50;
      G = 50;
      B = 0;
      break;
    case 4: // Green
      R = 0;
      G = 100;
      B = 0;
      break;
    case 5: // Sky Blue
      R = 0;
      G = 50;
      B = 50;
      break;
    case 6: // Blue
      R = 0;
      G = 0;
      B = 100;
      break;
    case 7: // Violet
      R = 25;
      G = 0;
      B = 75;
      break;
    case 8: // Pink
      R = 50;
      G = 0;
      B = 50;
      break; 
    case 9: // White
      R = 34;
      G = 33;
      B = 33;
      break;     
  }
}
void patternMode(uint32_t p) {
  switch (p) {
case 1: // Solid bright
      colorChange(strip.Color(R, G, B));
      break;
    case 2: // Solid dim
      colorChange(strip.Color(R/2, G/2, B/2));
      break;
    case 3: // Slow strobe
      wait = 800;
      colorChange(strip.Color(R, G, B));
      delay(wait);
      colorChange(strip.Color(0, 0, 0));
      delay(wait);
      break;
    case 4: // Fast strobe
      wait = 300;
      colorChange(strip.Color(R, G, B));
      delay(wait);
      colorChange(strip.Color(0, 0, 0));
      delay(wait);
      break;
    case 5: // Pulsate
      wait = 100;
      uint16_t i;
      for(i=0; i<7; i++) {
        if (STrigger) wait = 20;
        strip.setBrightness(255-(36*i));
        colorChange(strip.Color(R, G, B));
        delay(wait);
      }
      for(i=0; i<7; i++) {
        if (STrigger) wait = 20;
        strip.setBrightness(0+(36*i));
        colorChange(strip.Color(R, G, B));
        delay(wait);
      }
      break;
    case 6: // Tracer
      colorWipe(strip.Color(R, G, B));
      colorWipe(strip.Color(0, 0, 0));
      break;
    case 7: // Sparkle
      randNumber = 300;
      for(uint16_t i=0; i<strip.numPixels(); i++) {
        if (STrigger) randNumber = 60;
        if (random(randNumber) < 50) {
          strip.setPixelColor(i, strip.Color(R, G, B));
        } else {
          strip.setPixelColor(i, strip.Color(0, 0, 0));
        }
                strip.show();
        delay(20);
      }

      break;
    case 8: // Rainbow (This will not use the selected colors)
        uint16_t j;
        for(j=0; j<256; j++) {
          for(i=0; i<strip.numPixels(); i++) {
            strip.setPixelColor(i, Wheel((i+j) & 255));
          }
          strip.show();
          PatternSelect();
          if (P_MODE != p) break;
          delay(20);
        }
      break;
  case 9: // Color Pulsate
      wait = 100;
      for(i=0; i<7; i++) {
        if (STrigger){
          C_MODE += 1;
          if (C_MODE > 9)C_MODE = 1;
          colorMode(C_MODE);
        }
        strip.setBrightness(255-(36*i));
        colorChange(strip.Color(R, G, B));
        delay(wait);
      }
      for(i=0; i<7; i++) {
        if (STrigger){
          C_MODE += 1;
          if (C_MODE > 9)C_MODE = 1;
          colorMode(C_MODE);
        }
        strip.setBrightness(0+(36*i));
        colorChange(strip.Color(R, G, B));
        delay(wait);
      }
      break;    
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) {
    WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

ISR(PCINT0_vect) {
        STrigger = 1;
}

Credits

Richard Albritton

Richard Albritton

4 projects • 31 followers
I am a graphic artist and tech developer working at the SoDo Makerspace in Seattle Washington.

Comments