Skip to content

Instantly share code, notes, and snippets.

@electronicwhisper
Created June 27, 2018 19:54
Show Gist options
  • Save electronicwhisper/2526ba9569d975fbcd4024054d539225 to your computer and use it in GitHub Desktop.
Save electronicwhisper/2526ba9569d975fbcd4024054d539225 to your computer and use it in GitHub Desktop.
priceless 2018 lantern
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
Adafruit_NeoPixel bigStrip = Adafruit_NeoPixel(150, 6, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel smallStrip = Adafruit_NeoPixel(21, 7, NEO_GRB + NEO_KHZ800);
void setup() {
bigStrip.begin();
bigStrip.show();
smallStrip.begin();
smallStrip.show();
}
void loop() {
int s = analogRead(A0);
if (s < 341) {
sunrise(bigStrip);
sunrise(smallStrip);
} else if (s < 682) {
ocean(bigStrip);
ocean(smallStrip);
} else {
rainbow(bigStrip);
rainbow(smallStrip);
}
}
void sunrise(Adafruit_NeoPixel &strip) {
float t = float(millis())/3000;
int n = strip.numPixels();
for (int i = 0; i < n; i++) {
strip.setPixelColor(i, blend2(float(i) / n - t, 9,0,14, 255,33,0));
}
strip.show();
}
void ocean(Adafruit_NeoPixel &strip) {
float t = float(millis())/2600;
int n = strip.numPixels();
for (int i = 0; i < n; i++) {
strip.setPixelColor(i, blend(float(i) / n - t, 0,0,80, 0,255,70));
}
strip.show();
}
void rainbow(Adafruit_NeoPixel &strip) {
float t = float(millis())/8500;
int n = strip.numPixels();
for (int i = 0; i < n; i++) {
float bright = constrain(abs(fract(float(i)/(n*2)-t*1.57)*2-1)*1.5-0.34, 0.14, 1);
strip.setPixelColor(i, Wheel(fract(float(i) / (n*3) - (t*0.7))*335, bright));
}
strip.show();
}
float fract(float t) {
return t - floor(t);
}
uint32_t blend(float t, int r1, int g1, int b1, int r2, int g2, int b2) {
t = abs(fract(t/2)*2-1);
t = constrain(t*5-2, 0, 1);
int r = r1 * (1-t) + r2 * t;
int g = g1 * (1-t) + g2 * t;
int b = b1 * (1-t) + b2 * t;
return bigStrip.Color(r, g, b);
}
uint32_t blend2(float t, int r1, int g1, int b1, int r2, int g2, int b2) {
t = abs(fract(t/2)*2-1);
t = constrain(t*4-1.9, 0, 1);
int r = r1 * (1-t) + r2 * t;
int g = g1 * (1-t) + g2 * t;
int b = b1 * (1-t) + b2 * t;
return bigStrip.Color(r, g, b);
}
// 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(float WheelPos, float bright) {
WheelPos = constrain(WheelPos, 0, 255);
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return bigStrip.Color((255 - WheelPos * 3) * bright, 0, WheelPos * 3 * bright);
}
if(WheelPos < 170) {
WheelPos -= 85;
return bigStrip.Color(0, WheelPos * 3 * bright, (255 - WheelPos * 3) * bright);
}
WheelPos -= 170;
return bigStrip.Color(WheelPos * 3 * bright, (255 - WheelPos * 3) * bright, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment