Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 6 Tiered WS2812 RGB LED Ring Control
- #pragma GCC optimize ("-O1") //optimise Compiler Flag | save a few bytes!
- #include <Adafruit_NeoPixel.h>
- // set to pin connected to data input of WS8212 (NeoPixel) strip
- #define PIN 1
- // pin with analog input (used to initialize random number generator)
- #define RNDPIN 5
- // number of LEDs (NeoPixels) in your strip
- // (please note that you need 3 bytes of RAM available for each pixel)
- #define NUMPIXELS 93
- // increase to get narrow spots, decrease to get wider spots
- #define FOCUS 200
- // decrease to speed up, increase to slow down (it's not a delay actually)
- #define DELAY 2000
- //deco_lights pattern 3 color spots (reg, green, blue) oscillating along the strip with different speeds
- float spdr, spdg, spdb;
- float offset;
- // the real exponent function is too slow, so I created an approximation (only for x < 0)
- float myexp(float x) {
- return (1.0/(1.0-(0.634-1.344*x)*x));
- }
- //Setup button control.
- int mode = 0; //Power on mode
- int brightness = 10; // Power on brightness
- int brightnessToggle = 0; // Counter for brightness button press
- unsigned long button_start_time;
- unsigned int button_hold_duration;
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- void setup() {
- pinMode(2, INPUT); // set INT0 pin to input
- attachInterrupt(0, on_button_press, CHANGE); // attach INT0 interrupt
- // initialize pseudo-random number generator with some random value
- randomSeed(analogRead(RNDPIN));
- // assign random speed to each spot for deco_lights pattern
- spdr = 1.0 + random(200) / 100.0;
- spdg = 1.0 + random(200) / 100.0;
- spdb = 1.0 + random(200) / 100.0;
- // set random offset so spots start in random locations
- offset = random(10000) / 100.0;
- // initialize LED strip
- strip.begin();
- strip.show();
- }
- void loop() {
- startShow();
- }
- //Mode Menu
- void startShow() {
- switch(mode){
- case 0: treeSparkle(); // Chase Lights
- break;
- case 1: decoLights(); // Tree with random sparkle
- break;
- // case 2: pwrOff(); // All LED blank
- }
- }
- // called whenever INT0 - Pin 2 changes from high to low or low to high
- void on_button_press() {
- boolean digital_state = digitalRead(2);
- if (digital_state == LOW) { // button has been pressed down
- button_start_time = millis();
- } else { // button has been released
- button_hold_duration = millis() - button_start_time;
- on_button_pressed(button_hold_duration);
- }
- }
- // function runs when user finishes pressing button, and is passed button press duration in milliseconds
- void on_button_pressed(unsigned int duration) {
- if (duration > 500) { // button held for longer than half second change mode.
- mode++;
- if (mode > 1)
- mode=0;
- }
- else { // button held for less than half second change brightness
- brightnessToggle++;
- if (brightnessToggle > 1){
- brightnessToggle = 0;
- brightness = 10;
- }
- else {
- brightness = 50;
- }
- }
- }
- //Many thanks to https://github.com/smartynov/iotfun for deco_lights module
- void decoLights() {
- // use real time to recalculate position of each color spot
- long ms = millis();
- // scale time to float value
- float m = offset + (float)ms/DELAY;
- // add some non-linearity
- m = m - 42.5*cos(m/552.0) - 6.5*cos(m/142.0);
- // recalculate position of each spot (measured on a scale of 0 to 1)
- float posr = 0.5 + 0.55*sin(m*spdr);
- float posg = 0.5 + 0.55*sin(m*spdg);
- float posb = 0.5 + 0.55*sin(m*spdb);
- // now iterate over each pixel and calculate it's color
- for (int i=0; i<NUMPIXELS; i++) {
- // pixel position on a scale from 0.0 to 1.0
- float ppos = (float)i / NUMPIXELS;
- // distance from this pixel to the center of each color spot
- float dr = ppos-posr;
- float dg = ppos-posg;
- float db = ppos-posb;
- // set each color component from 0 to max BRIGHTNESS, according to Gaussian distribution
- strip.setPixelColor(i,
- constrain((brightness+15)*myexp(-200*dr*dr),0,(brightness+15)),
- constrain((brightness+15)*myexp(-200*dg*dg),0,(brightness+15)),
- constrain((brightness+15)*myexp(-200*db*db),0,(brightness+15))
- );
- }
- strip.show();
- }
- void treeSparkle() {
- for (int j=0; j<NUMPIXELS-1; j++) {
- strip.setPixelColor(j, strip.Color(5, 25,0 )); //R G B
- }
- for (int i=0; i<30; i++) {
- if (brightnessToggle==0){
- strip.setPixelColor(random(NUMPIXELS-1), strip.Color(random(10-25), 0, random(10-25)));
- }
- if (brightnessToggle==1){
- strip.setPixelColor(random(NUMPIXELS-1), strip.Color(random(30-70), 0, random(30-70)));
- }
- }
- strip.setPixelColor(92, strip.Color(25,15,5 ));
- strip.show();
- delay(random(50-150));
- }
- //void pwrOff() {
- // for (int j=0; j<NUMPIXELS; j++) {
- // strip.setPixelColor(j, strip.Color(0,0,0)); //R G B
- // }
- // strip.show();
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement