Skip to content

Instantly share code, notes, and snippets.

@jacobjoaquin
Created October 2, 2015 12:58
Show Gist options
  • Save jacobjoaquin/6d0445e5e069feafce5e to your computer and use it in GitHub Desktop.
Save jacobjoaquin/6d0445e5e069feafce5e to your computer and use it in GitHub Desktop.
LED Routines for Fresno Ideaworks ArtHop Sign.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define N_PIXELS 240
#define SCANNER_LENGTH 30
const int nPixels = 240;
int blinkPin = 11;
bool blinkState = true;
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(nPixels, 1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(nPixels, 4, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(nPixels, 7, NEO_GRB + NEO_KHZ800);
//Adafruit_NeoPixel strips[3] = {strip1, strip2, strip3};
uint32_t theBuffer[SCANNER_LENGTH + 1]; // Extra guard point for interpolation
float phase = 0.5;
int direction = 1;
// User defined settings
float phaseInc = 0.01;
void setup() {
pinMode(blinkPin, OUTPUT);
loadBuffer();
strip1.begin();
strip2.begin();
strip3.begin();
}
void loop() {
int d = 30000;
digitalWrite(blinkPin, HIGH);
larson(d / 3);
sparkleFunTwoColor(d, 5, 64, 0xffffff, 0x33ffcc);
triSparkle(d, 0x00ff88, 0x00ff00, 0x0088ff);
sparkleFun(d, 5, 32, 0xff9933);
triSparkle(d, 0xff3388, 0xff4400, 0xff3388);
sparkleFun(d, 5, 32, 0x3399ff);
triSparkle(d, 0xffcc00, 0xff5500, 0xff0000);
sparkleFunTwoColor(d, 5, 32, 0xff6600, 0xff33cc);
sparkleFun(d, 5, 32, 0x66ff66);
sparkle(d);
// delay(1000);
// rgb(1000);
// delay(1000);
// blinkState = !blinkState;
// digitalWrite(blinkPin, blinkState ? HIGH : LOW);
}
void clearStrips() {
strip1.clear();
strip2.clear();
strip3.clear();
}
void showStrips() {
strip1.show();
strip2.show();
strip3.show();
}
void setPixel(int strip, uint32_t i, uint32_t c) {
if (strip == 1) {
strip1.setPixelColor(i, c);
}
else if (strip == 2) {
strip2.setPixelColor(i, c);
}
else {
strip3.setPixelColor(i, c);
}
}
uint32_t getPixel(int strip, uint32_t i) {
if (strip == 1) {
return strip1.getPixelColor(i);
}
else if (strip == 2) {
return strip2.getPixelColor(i);
}
return strip3.getPixelColor(i);
}
void setBufferColor(uint32_t c1, uint32_t c2, uint8_t offset, uint8_t theLength) {
for (int i = 0; i < theLength; i++) {
theBuffer[i + offset] = lerpColor(c1, c2, float(i) / float(theLength));
}
}
void loadBuffer() {
setBufferColor(0, 0xff0000, 0, 15);
setBufferColor(0xff0000, 0, 15, 15);
theBuffer[SCANNER_LENGTH] = 0;
}
uint32_t lerpColor(uint32_t c1, uint32_t c2, float amt) {
uint32_t r1 = (c1 & 0xff0000) >> 16;
uint32_t g1 = (c1 & 0xff00) >> 8;
uint32_t b1 = (c1 & 0xff);
uint32_t r2 = (c2 & 0xff0000) >> 16;
uint32_t g2 = (c2 & 0xff00) >> 8;
uint32_t b2 = (c2 & 0xff);
float r = (float(r1) * (1.0 - amt) + float(r2) * amt) / 2.0;
float g = (float(g1) * (1.0 - amt) + float(g2) * amt) / 2.0;
float b = (float(b1) * (1.0 - amt) + float(b2) * amt) / 2.0;
return (uint32_t(r) << 16) | (uint32_t(g) << 8) | uint32_t(b);
}
void updateScanner() {
clearStrips();
float p = (sin(phase * TWO_PI) + 1.0) / 2.0 * N_PIXELS / 2 + 0.5;
int p0 = p;
float interp = 1 - (p - float(p0));
int offset = SCANNER_LENGTH / 2;
for (int i = 0; i < SCANNER_LENGTH; i++) {
int pos = p0 - offset + i;
uint32_t c = lerpColor(theBuffer[i], theBuffer[i + 1], interp);
if (pos >= 0 && pos < N_PIXELS) {
setPixel(0, pos , c);
setPixel(1, pos , c);
setPixel(2, pos , c);
}
}
showStrips();
phase += phaseInc;
phase -= phase >= 1.0 ? 1 : 0;
delay(5);
}
void larson(unsigned long duration) {
unsigned long endTime = millis() + duration;
while (millis() < endTime) {
updateScanner();
}
}
void sparkleFunTwoColor(unsigned long duration, int rate, int decay, uint32_t theColor, uint32_t theColor2) {
unsigned long endTime = millis() + duration;
while (millis() < endTime) {
uint32_t rgb[3] = {0, 0, 0};
for (int s = 0; s < 3; s++) {
for (int i = 0; i < nPixels; i++) {
uint32_t c = getPixel(s, i);
rgb[0] = (0xff0000 & c) >> 16;
rgb[1] = (0xff00 & c) >> 8;
rgb[2] = 0xff & c;
for (int j = 0; j < 3; j++) {
if (rgb[j] >= decay) {
rgb[j] -= decay;
} else {
rgb[j] = 0;
}
}
c = (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
if (random(255) < rate) {
c = random(2) ? theColor : theColor2;
}
setPixel(s, i, c);
}
}
showStrips();
}
}
void sparkleFun(unsigned long duration, int rate, int decay, uint32_t theColor) {
unsigned long endTime = millis() + duration;
while (millis() < endTime) {
uint32_t rgb[3] = {0, 0, 0};
for (int s = 0; s < 3; s++) {
for (int i = 0; i < nPixels; i++) {
uint32_t c = getPixel(s, i);
rgb[0] = (0xff0000 & c) >> 16;
rgb[1] = (0xff00 & c) >> 8;
rgb[2] = 0xff & c;
for (int j = 0; j < 3; j++) {
if (rgb[j] >= decay) {
rgb[j] -= decay;
} else {
rgb[j] = 0;
}
}
c = (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
if (random(255) < rate) {
c = theColor;
}
setPixel(s, i, c);
}
}
showStrips();
}
}
void sparkle(unsigned long duration) {
unsigned long endTime = millis() + duration;
while (millis() < endTime) {
for (int s = 0; s < 3; s++) {
for (int i = 0; i < nPixels; i++) {
uint32_t c = getPixel(s, i);
c = 0xff & c;
if (c >= 32) {
c -= 32;
} else {
c = 0;
}
c = (c << 16) | (c << 8) | c;
if (random(255) < 5) {
c = 0xffffff;
}
setPixel(s, i, c);
}
}
showStrips();
}
}
void triSparkle(unsigned long duration, uint32_t c1, uint32_t c2, uint32_t c3) {
unsigned long endTime = millis() + duration;
while (millis() < endTime) {
clearStrips();
for (int i = 0; i < nPixels; i++) {
strip1.setPixelColor(i, random(2) ? c1 : 0);
strip2.setPixelColor(i, random(2) ? c2 : 0);
strip3.setPixelColor(i, random(2) ? c3 : 0);
}
showStrips();
delay(1000 / 60);
}
}
// RGB!!!!
void rgb(unsigned long duration) {
unsigned long endTime = millis() + duration;
while (millis() < endTime) {
clearStrips();
for (int i = 0; i < nPixels; i++) {
strip1.setPixelColor(i, random(2) ? 0xff0000 : 0);
strip2.setPixelColor(i, random(2) ? 64 << 8 : 0);
strip3.setPixelColor(i, random(2) ? 64 : 0);
}
showStrips();
delay(1000 / 60);
}
}
/*
void blank(unsigned long duration) {
unsigned long endTime = millis() + duration;
while (millis() < endTime) {
clearStrips();
showStrips();
delay(1000 / 60);
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment