Advertisement
Guest User

WS2812_Tree_93.ino

a guest
Dec 18th, 2016
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. // 6 Tiered WS2812 RGB LED Ring Control
  2.  
  3. #pragma GCC optimize ("-O1") //optimise Compiler Flag | save a few bytes!
  4. #include <Adafruit_NeoPixel.h>
  5.  
  6. // set to pin connected to data input of WS8212 (NeoPixel) strip
  7. #define PIN         1
  8.                          
  9. // pin with analog input (used to initialize random number generator)
  10. #define RNDPIN      5
  11.  
  12. // number of LEDs (NeoPixels) in your strip
  13. // (please note that you need 3 bytes of RAM available for each pixel)
  14. #define NUMPIXELS   93
  15.  
  16. // increase to get narrow spots, decrease to get wider spots
  17. #define FOCUS       200
  18.  
  19. // decrease to speed up, increase to slow down (it's not a delay actually)
  20. #define DELAY       2000
  21.  
  22. //deco_lights pattern 3 color spots (reg, green, blue) oscillating along the strip with different speeds
  23. float spdr, spdg, spdb;
  24. float offset;
  25.  
  26. // the real exponent function is too slow, so I created an approximation (only for x < 0)
  27. float myexp(float x) {
  28.   return (1.0/(1.0-(0.634-1.344*x)*x));
  29. }
  30.  
  31. //Setup button control.
  32. int mode = 0; //Power on mode
  33. int brightness = 10; // Power on brightness
  34. int brightnessToggle = 0; // Counter for brightness button press
  35. unsigned long button_start_time;
  36. unsigned int button_hold_duration;
  37.  
  38. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  39.  
  40. void setup() {
  41.  
  42.   pinMode(2, INPUT); // set INT0 pin to input
  43.   attachInterrupt(0, on_button_press, CHANGE); // attach INT0 interrupt
  44.  
  45.   // initialize pseudo-random number generator with some random value
  46.   randomSeed(analogRead(RNDPIN));
  47.  
  48.   // assign random speed to each spot for deco_lights pattern
  49.   spdr = 1.0 + random(200) / 100.0;
  50.   spdg = 1.0 + random(200) / 100.0;
  51.   spdb = 1.0 + random(200) / 100.0;
  52.  
  53.   // set random offset so spots start in random locations
  54.   offset = random(10000) / 100.0;
  55.  
  56.   // initialize LED strip
  57.   strip.begin();
  58.   strip.show();
  59. }
  60.  
  61. void loop() {
  62. startShow();
  63. }
  64.  
  65. //Mode Menu
  66. void startShow() {
  67.   switch(mode){
  68.     case 0: treeSparkle(); // Chase Lights
  69.             break;
  70.     case 1: decoLights(); // Tree with random sparkle
  71.             break;
  72.   //  case 2: pwrOff(); // All LED blank
  73.   }
  74. }
  75.  
  76. // called whenever INT0 - Pin 2 changes from high to low or low to high
  77. void on_button_press() {
  78.   boolean digital_state = digitalRead(2);
  79.   if (digital_state == LOW) { // button has been pressed down
  80.     button_start_time = millis();
  81.   } else { // button has been released
  82.     button_hold_duration = millis() - button_start_time;
  83.     on_button_pressed(button_hold_duration);
  84.   }
  85. }
  86.  
  87.  
  88. // function runs when user finishes pressing button, and is passed button press duration in milliseconds
  89. void on_button_pressed(unsigned int duration) {
  90.    
  91.   if (duration > 500) { // button held for longer than half second change mode.
  92.       mode++;
  93.       if (mode > 1)
  94.       mode=0;
  95.   }
  96.    
  97.   else { // button held for less than half second change brightness
  98.     brightnessToggle++;
  99.    
  100.     if (brightnessToggle > 1){
  101.       brightnessToggle = 0;
  102.       brightness = 10;
  103.     }
  104.    
  105.     else {
  106.       brightness = 50;
  107.     }
  108.    
  109.   }
  110.  
  111. }
  112.  
  113. //Many thanks to https://github.com/smartynov/iotfun for deco_lights module
  114. void decoLights() {
  115.  
  116.   // use real time to recalculate position of each color spot
  117.   long ms = millis();
  118.   // scale time to float value
  119.   float m = offset + (float)ms/DELAY;
  120.   // add some non-linearity
  121.   m = m - 42.5*cos(m/552.0) - 6.5*cos(m/142.0);
  122.  
  123.   // recalculate position of each spot (measured on a scale of 0 to 1)
  124.   float posr = 0.5 + 0.55*sin(m*spdr);
  125.   float posg = 0.5 + 0.55*sin(m*spdg);
  126.   float posb = 0.5 + 0.55*sin(m*spdb);
  127.  
  128.   // now iterate over each pixel and calculate it's color
  129.   for (int i=0; i<NUMPIXELS; i++) {
  130.    
  131.     // pixel position on a scale from 0.0 to 1.0
  132.     float ppos = (float)i / NUMPIXELS;
  133.  
  134.     // distance from this pixel to the center of each color spot
  135.     float dr = ppos-posr;
  136.     float dg = ppos-posg;
  137.     float db = ppos-posb;
  138.  
  139.     // set each color component from 0 to max BRIGHTNESS, according to Gaussian distribution
  140.     strip.setPixelColor(i,
  141.       constrain((brightness+15)*myexp(-200*dr*dr),0,(brightness+15)),
  142.       constrain((brightness+15)*myexp(-200*dg*dg),0,(brightness+15)),
  143.       constrain((brightness+15)*myexp(-200*db*db),0,(brightness+15))
  144.       );
  145.   }
  146.   strip.show();  
  147. }
  148.  
  149. void treeSparkle() {
  150.   for (int j=0; j<NUMPIXELS-1; j++) {
  151.   strip.setPixelColor(j, strip.Color(5, 25,0 )); //R G B
  152.   }
  153.   for (int i=0; i<30; i++) {
  154.   if (brightnessToggle==0){
  155.   strip.setPixelColor(random(NUMPIXELS-1), strip.Color(random(10-25), 0, random(10-25)));
  156.   }
  157.   if (brightnessToggle==1){
  158.   strip.setPixelColor(random(NUMPIXELS-1), strip.Color(random(30-70), 0, random(30-70)));
  159.   }
  160.   }
  161.   strip.setPixelColor(92, strip.Color(25,15,5 ));
  162.   strip.show();
  163.   delay(random(50-150));
  164. }
  165.  
  166. //void pwrOff() {
  167. //   for (int j=0; j<NUMPIXELS; j++) {
  168. //    strip.setPixelColor(j, strip.Color(0,0,0)); //R G B
  169. //   }
  170. //   strip.show();
  171. //   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement