Skip to content

Instantly share code, notes, and snippets.

@Data-ptr
Last active June 10, 2017 02:57
Show Gist options
  • Select an option

  • Save Data-ptr/b2c135f92cedae7156571d65010c5871 to your computer and use it in GitHub Desktop.

Select an option

Save Data-ptr/b2c135f92cedae7156571d65010c5871 to your computer and use it in GitHub Desktop.
Building a White, Red, Green, Blue Flashlight with NeoPixels and an ATTiny/85
// +---+
// | |
// | |
// |Neo|
// | |
// | |
// +---+
// | |
// | |
// |Pix|
// | |
// | |
// +-+-+
// XXXXXXXXXXXXXXXXXXXXXXXXXXXX GND <--+ | +--> +5V XXXXXXXXXXXXXXXXXXXXXX
// X | X
// X v XXXXXXXXXXXXXXXXXXXXX X
// X +Data X X
// X X X
// X XXXXXXXXXXXXXXXXXXXXXXXXXXX X X
// X X +-----------+ X XXXXX X
// X +----------+ X +-+ NC V+ +--->XXXXXXXXXXX X XXXXXXX
// X | +5V +-->X | | X X
// X | | +-+ NC P2 +--->XXXXXXXXXXXXX X
// X | BATT | | | X
// X | | | ATTiny/85 | X
// X | -5V +->X | | X
// X +----------+ X +-+ NC P1 +--->XXX X
// X X | | X X
// XXXXXXXXXXXXXXXXXXX XXXXXXX<---+ GND P0 +--->X X X
// X +-----------+ X X X
// X X X X
// X +----+ +--+ X X X
// XX<-+LED1+->X<-+R2+->X X +-------+ X
// X +----+ +--+ XXX<--+ +-+ X
// X 220k X | SW1 | X
// X X +-+ +-->X
// X +------+ X +-------+
// XXXX<--+ R1 +-->XXXXXX
// +------+
// 10k
//
//
// +SW1: Momentary switch (push switch)
//
// +LED1: (optional) "Blink" LED
//
// +R1: 10k Pull+down resistor
//
// +R2: ~220k (If you include an LED) Current Limiting Resistor
//
//
// ATTiny/85 Pins
//
// +V+: +5V From BATT, also to NeoPixel and SW1
//
// +GND: +5V From BATT,
// also to NeoPixel,
// Pull+down Resistor for SW1,
// and negative for optional LED1
//
// +P2: Data to NeoPixelPixel
//
// +P1: to SW1, Switch to change NeoPixel Colors
//
// +P0: Optional "Blink" LED (for testing, or indication)
//
//
// Diagram made with http://asciiflow.com/
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 2
#define NUM_LEDS 2
#define BUTTON 1
#define SW_DELAY 50
// Wait, I don't even use this yet...
enum colorSelections {
O, // Off
W, // White
R, // Red
G, // Green
B // Blue
};
int colorValues[5][3] =
{
{0, 0, 0 }, // Off
{255, 255, 255}, // White
{255, 0, 0 }, // Red
{0, 255, 0 }, // Green
{0, 0, 255} // Blue
};
int numColors = (sizeof( colorValues ) / (sizeof( int ) * 3)) - 1;
int buttonWas = 0;
int colorWas = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode( BUTTON, INPUT );
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
if( HIGH == digitalRead(BUTTON) ) {
if( 1 != buttonWas ) {
if( numColors == colorWas ) {
colorWas = 0;
}
else {
colorWas++;
}
}
buttonWas = 1;
for(int i = 0; i < NUM_LEDS; ++i) {
strip.setPixelColor( i, strip.Color( colorValues[colorWas][0], colorValues[colorWas][1], colorValues[colorWas][2] ) );
}
strip.show();
}
else {
buttonWas = 0;
delay(SW_DELAY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment