Skip to content

Instantly share code, notes, and snippets.

@calebsander
Created March 20, 2016 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebsander/47aadfcd66b3de82e207 to your computer and use it in GitHub Desktop.
Save calebsander/47aadfcd66b3de82e207 to your computer and use it in GitHub Desktop.
Script for dank pit LEDs
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define START_PIN 3
#define NUM_STRIPS 3
#define NUM_PIXELS 150
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino 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)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel *strips[NUM_STRIPS];
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
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
for (uint8_t strip = 0; strip < NUM_STRIPS; strip++) {
strips[strip] = new Adafruit_NeoPixel(NUM_PIXELS, START_PIN + strip, NEO_GRB | NEO_KHZ800);
strips[strip]->begin();
strips[strip]->show(); // Initialize all pixels to 'off'
}
srand(millis());
Serial.begin(38400);
}
const uint8_t RED = 0, GREEN = 103, BLUE = 160;
const uint32_t color = strips[0]->Color(RED, GREEN, BLUE);
void loop() {
Serial.println("Pulse");
for (uint8_t i = 0; i != 10; i++) pulse(RED, GREEN, BLUE, 0);
Serial.println("Retract");
for (uint8_t i = 0; i != 3; i++) retract(color, 10);
Serial.println("Theater");
for (uint8_t i = 0; i != 50; i++) theaterChase(color, 100);
Serial.println("Wipe");
for (uint8_t i = 0; i != 3; i++) {
colorWipe(color, 0);
colorWipe(0, 0);
colorWipeInverse(color, 0);
colorWipeInverse(0, 0);
}
setAll(0);
Serial.println("Random");
for (uint8_t i = 0; i != 100; i++) randomPixels(color, 100);
Serial.println("Rainbow");
for (uint8_t i = 0; i != 3; i++) uniformRainbow(5);
}
//Input a value 0 to 255 to get a color value
//The colors are a transition r - g - b - back to r.
uint32_t wheel(uint8_t wheelPos) {
wheelPos = 255 - wheelPos;
if (wheelPos < 85) return strips[0]->Color(255 - wheelPos * 3, 0, wheelPos * 3);
else if (wheelPos < 170) {
wheelPos -= 85;
return strips[0]->Color(0, wheelPos * 3, 255 - wheelPos * 3);
}
else {
wheelPos -= 170;
return strips[0]->Color(wheelPos * 3, 255 - wheelPos * 3, 0);
}
}
//Set all the pixels to the same color
void setAll(uint32_t color) {
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
for (uint8_t pixel = 0; pixel != NUM_PIXELS; pixel++) strips[strip]->setPixelColor(pixel, color);
strips[strip]->show();
}
}
//Fill the pixels one after the other with a color
void colorWipe(uint32_t color, uint16_t wait) {
for (uint8_t pixel = 0; pixel != strips[0]->numPixels(); pixel++) {
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
strips[strip]->setPixelColor(pixel, color);
strips[strip]->show();
}
delay(wait);
}
}
//Fill the pixels one after the other starting at the end with a color
void colorWipeInverse(uint32_t color, uint16_t wait) {
for (int16_t pixel = strips[0]->numPixels(); pixel != -1; pixel--) {
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
strips[strip]->setPixelColor(pixel, color);
strips[strip]->show();
}
delay(wait);
}
}
//Color all the pixels rainbowed-ly, varying over time and along the strand
void rainbow(uint16_t wait) {
for (uint8_t hue = 1; hue; hue++) {
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
for (uint16_t pixel = 0; pixel != NUM_PIXELS; pixel++) strips[strip]->setPixelColor(pixel, wheel((pixel + hue) % 256));
strips[strip]->show();
}
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint16_t wait) {
for (uint16_t j = 0; j != 256 * 5; j++) { // 5 cycles of all colors on wheel
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
for (uint8_t i = 0; i != NUM_PIXELS; i++) {
strips[strip]->setPixelColor(i, wheel(((i * 256 / NUM_PIXELS) + j) % 256));
}
strips[strip]->show();
}
delay(wait);
}
}
//Color all the pixels rainbowed-ly, varying over time
void uniformRainbow(uint16_t wait) {
for (uint8_t hue = 1; hue; hue++) {
setAll(wheel(hue));
delay(wait);
}
}
#define THEATER_INTERVAL 3
//Theater-style crawling lights - color every third pixel, moving along the strand
void theaterChase(uint32_t color, uint16_t wait) {
for (uint8_t q = 0; q != THEATER_INTERVAL; q++) {
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
for (uint8_t i = 0; i < NUM_PIXELS - q; i += THEATER_INTERVAL) strips[strip]->setPixelColor(i + q, color); //turn every third pixel on
strips[strip]->show();
}
delay(wait);
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
for (uint8_t i = 0; i < NUM_PIXELS - q; i += THEATER_INTERVAL) strips[strip]->setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
//Choose 50 pixels with replacement and turns them the requested color
void randomPixels(uint32_t color, uint16_t wait) {
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
for (uint8_t pixel = 0; pixel != NUM_PIXELS; pixel++) strips[strip]->setPixelColor(pixel, 0);
for (uint8_t pixel = 0; pixel != 50; pixel++) strips[strip]->setPixelColor(rand() % NUM_PIXELS, color);
strips[strip]->show();
}
delay(wait);
}
void retract(uint32_t color, uint16_t wait) {
setAll(color);
for (uint8_t strip = 0; strip < NUM_STRIPS; strip++) {
for (int16_t pixel = NUM_PIXELS - 1; pixel != -1; pixel--) {
strips[strip]->setPixelColor(pixel, 0);
strips[strip]->show();
delay(wait);
}
}
}
#define INCREMENT 5
//Pulse the pixels on and off in varying brightnesses
void pulse(uint8_t red, uint8_t green, uint8_t blue, uint16_t wait) {
uint16_t portion;
for (portion = 0; portion < 256; portion += INCREMENT) {
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
for (uint8_t pixel = 0; pixel != NUM_PIXELS; pixel++) strips[strip]->setPixelColor(pixel, red * portion / 256, blue * portion / 256, green * portion / 256);
strips[strip]->show();
}
delay(wait);
}
for (; portion; portion -= INCREMENT) {
for (uint8_t strip = 0; strip != NUM_STRIPS; strip++) {
for (uint8_t pixel = 0; pixel != NUM_PIXELS; pixel++) strips[strip]->setPixelColor(pixel, red * portion / 256, blue * portion / 256, green * portion / 256);
strips[strip]->show();
}
delay(wait);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment