Advertisement
teleias

Sona

Apr 26th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.37 KB | None | 0 0
  1. #include "LPD8806.h"
  2. #include "SPI.h"
  3.  
  4. // Example to control LPD8806-based RGB LED Modules in a strip!
  5. // NOTE: WILL NOT WORK ON TRINKET OR GEMMA due to floating-point math
  6. /*****************************************************************************/
  7.  
  8. #if defined(USB_SERIAL) || defined(USB_SERIAL_ADAFRUIT)
  9. // this is for teensyduino support
  10. int dataPin = 2;
  11. int clockPin = 1;
  12. #else
  13. // these are the pins we use for the LED belt kit using
  14. // the Leonardo pinouts
  15. int dataPin = 16;
  16. int clockPin = 15;
  17. #endif
  18.  
  19. // Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
  20. // The LED strips are 32 LEDs per meter but you can extend/cut the strip
  21. LPD8806 strip = LPD8806(32, dataPin, clockPin);
  22. const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
  23. unsigned int sample;
  24. void setup() {
  25.   // Start up the LED strip
  26.   strip.begin();
  27.  
  28.   // Update the strip, to start they are all 'off'
  29.   strip.show();
  30.  
  31.   //
  32.   Serial.begin(9600);
  33. }
  34.  
  35. // function prototypes, do not remove these!
  36. void colorChase(uint32_t c, uint8_t wait);
  37. void colorWipe(uint32_t c, uint8_t wait);
  38. void dither(uint32_t c, uint8_t wait);
  39. void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait);
  40. void wave(uint32_t c, int cycles, uint8_t wait);
  41. void rainbowCycle(uint8_t wait);
  42. uint32_t Wheel(uint16_t WheelPos);
  43.  
  44. void loop() {
  45.   /*colorWipe(strip.Color(0,analogRead(0)/2,analogRead(0)), 20);
  46.   int i = 0;
  47.   for(int k = 0; k < 7*5*8; k++)
  48.   {
  49.     int num;
  50.     num = 7;
  51.     bump(4,  (k%(8*num))/num);
  52.     num = 5;
  53.     bump(14, (k%(8*num))/num);
  54.     num = 7;
  55.     bump(24, (k%(8*num))/num);
  56.     strip.show();
  57.     delay(10);
  58.   }*/
  59.  
  60.   //for(int a = 0; a < 2; a++)
  61.   int a = 0; int w = 0;
  62.   {
  63.     for(int b = 0; b < 48*8*4; b++)
  64.     {
  65.       w = 3;
  66.       a = (b/(8*w)%8)/4;
  67.       bump2(0,  4, a, b/(8*w)%4);
  68.       bump2(1, 12, a, b/(8*w)%4);
  69.       w = 2;
  70.       a = (b/(8*w)%8)/4;
  71.       bump2(0, 14, a, b/(8*w)%4);
  72.       bump2(1, 22, a, b/(8*w)%4);
  73.       w = 3;
  74.       a = (b/(8*w)%8)/4;
  75.       bump2(0, 24, a, b/(8*w)%4);
  76.       bump2(1, 32, a, b/(8*w)%4);
  77.      
  78.       strip.show();
  79.     }
  80.   }
  81.   //Serial.println(c);
  82. }
  83.  
  84. void bump(int i, int k)
  85. {
  86.   if(true)
  87.   {
  88.     for(int j = 0; j < 8; j++)
  89.     {
  90.       if(k < 8)
  91.       {
  92.         int iteration = 0;
  93.         if(j < 4)
  94.         {
  95.           iteration = j;
  96.           if(4-iteration <= k%8)
  97.           {
  98.             strip.setPixelColor(i+j, strip.Color(0,0,0));
  99.           }
  100.           else
  101.           {
  102.             strip.setPixelColor(i+j, strip.Color(0,50+(iteration)*25,120-iteration*25));  
  103.           }
  104.         }
  105.         else
  106.         {
  107.           iteration = 8-j-1;
  108.           if(4-iteration <= k%8)
  109.           {
  110.             strip.setPixelColor(i+j, strip.Color(0,0,0));
  111.           }
  112.           else
  113.           {
  114.             strip.setPixelColor(i+j, strip.Color(0,50+(iteration)*25,120-(iteration)*25));  
  115.           }
  116.         }
  117.       }
  118.     }
  119.   }
  120. }
  121.  
  122. //w: reverse 0~1
  123. //x: init
  124. //y: 0~2
  125. //z: 0~4
  126. void bump2(int w, int x, int y, int z)
  127. {
  128.   if(w == 0)
  129.   {
  130.     if(y == 0)
  131.     {
  132.       strip.setPixelColor(x+z, strip.Color(1,50+z*25,120-z*25));
  133.     }
  134.     else if(y == 1)
  135.     {
  136.       strip.setPixelColor(x+4-1-z, strip.Color(0,0,0));
  137.     }
  138.   }
  139.   else
  140.   {
  141.     x-=1;
  142.     if(y == 0)
  143.     {
  144.       strip.setPixelColor(x-z, strip.Color(1,50+z*25,120-z*25));
  145.     }
  146.     else if(y == 1)
  147.     {
  148.       strip.setPixelColor(x+z-4+1, strip.Color(0,0,0));
  149.     }
  150.   }
  151.  
  152.  
  153.  
  154. }
  155. void loop2()
  156. {
  157.   unsigned long startMillis= millis();  // Start of sample window
  158.   unsigned int peakToPeak  = 0;   // peak-to-peak level
  159.  
  160.   unsigned int signalMax = 0;
  161.   unsigned int signalMin = 1024;
  162.  
  163.   // Send a simple pixel chase in...
  164.    colorChase(strip.Color(127,127,127), 20); // white
  165.    colorChase(strip.Color(127,0,0), 20);     // red
  166.    colorChase(strip.Color(127,127,0), 20);   // yellow
  167.    colorChase(strip.Color(0,127,0), 20);     // green
  168.    colorChase(strip.Color(0,127,127), 20);   // cyan
  169.    colorChase(strip.Color(0,0,127), 20);     // blue
  170.    colorChase(strip.Color(127,0,127), 20);   // magenta
  171.    
  172.    // Fill the entire strip with...
  173.    colorWipe(strip.Color(127,0,0), 20);      // red
  174.    colorWipe(strip.Color(0, 127,0), 20);     // green
  175.    colorWipe(strip.Color(0,0,127), 20);      // blue
  176.    colorWipe(strip.Color(0,0,0), 20);        // black
  177.    
  178.    // Color sparkles
  179.    dither(strip.Color(0,127,127), 50);       // cyan, slow
  180.    dither(strip.Color(0,0,0), 15);           // black, fast
  181.    dither(strip.Color(127,0,127), 50);       // magenta, slow
  182.    dither(strip.Color(0,0,0), 15);           // black, fast
  183.    dither(strip.Color(127,127,0), 50);       // yellow, slow
  184.    dither(strip.Color(0,0,0), 15);           // black, fast
  185.    
  186.    // Back-and-forth lights
  187.    scanner(127,0,0, 30);        // red, slow
  188.    scanner(0,0,127, 15);        // blue, fast
  189.    
  190.    // Wavy ripple effects
  191.    wave(strip.Color(127,0,0), 4, 20);        // candy cane
  192.    wave(strip.Color(0,0,100), 2, 40);        // icy
  193.    
  194.    // make a pretty rainbow cycle!
  195.    rainbowCycle(0);  // make it go through the cycle fairly fast
  196.    
  197.    // Clear strip data before start of next effect
  198.    for (int i=0; i < strip.numPixels(); i++) {
  199.    strip.setPixelColor(i, 0);
  200.    }
  201.    
  202. }
  203.  
  204. // Cycle through the color wheel, equally spaced around the belt
  205. void rainbowCycle(uint8_t wait) {
  206.   uint16_t i, j;
  207.  
  208.   for (j=0; j < 384 * 5; j++) {     // 5 cycles of all 384 colors in the wheel
  209.     for (i=0; i < strip.numPixels(); i++) {
  210.       // tricky math! we use each pixel as a fraction of the full 384-color
  211.       // wheel (thats the i / strip.numPixels() part)
  212.       // Then add in j which makes the colors go around per pixel
  213.       // the % 384 is to make the wheel cycle around
  214.       strip.setPixelColor(i, Wheel(((i * 384 / strip.numPixels()) + j) % 384));
  215.     }
  216.     strip.show();   // write all the pixels out
  217.     delay(wait);
  218.   }
  219. }
  220.  
  221. // fill the dots one after the other with said color
  222. // good for testing purposes
  223. void colorWipe(uint32_t c, uint8_t wait) {
  224.   int i;
  225.  
  226.   for (i=0; i < strip.numPixels(); i++) {
  227.     strip.setPixelColor(i, c);
  228.     strip.show();
  229.     delay(wait);
  230.   }
  231. }
  232.  
  233. // Chase a dot down the strip
  234. // good for testing purposes
  235. void colorChase(uint32_t c, uint8_t wait) {
  236.   int i;
  237.  
  238.   for (i=0; i < strip.numPixels(); i++) {
  239.     strip.setPixelColor(i, 0);  // turn all pixels off
  240.   }
  241.  
  242.   for (i=0; i < strip.numPixels(); i++) {
  243.     strip.setPixelColor(i, c); // set one pixel
  244.     strip.show();              // refresh strip display
  245.     delay(wait);               // hold image for a moment
  246.     strip.setPixelColor(i, 0); // erase pixel (but don't refresh yet)
  247.   }
  248.   strip.show(); // for last erased pixel
  249. }
  250.  
  251. // An "ordered dither" fills every pixel in a sequence that looks
  252. // sparkly and almost random, but actually follows a specific order.
  253. void dither(uint32_t c, uint8_t wait) {
  254.  
  255.   // Determine highest bit needed to represent pixel index
  256.   int hiBit = 0;
  257.   int n = strip.numPixels() - 1;
  258.   for(int bit=1; bit < 0x8000; bit <<= 1) {
  259.     if(n & bit) hiBit = bit;
  260.   }
  261.  
  262.   int bit, reverse;
  263.   for(int i=0; i<(hiBit << 1); i++) {
  264.     // Reverse the bits in i to create ordered dither:
  265.     reverse = 0;
  266.     for(bit=1; bit <= hiBit; bit <<= 1) {
  267.       reverse <<= 1;
  268.       if(i & bit) reverse |= 1;
  269.     }
  270.     strip.setPixelColor(reverse, c);
  271.     strip.show();
  272.     delay(wait);
  273.   }
  274.   delay(250); // Hold image for 1/4 sec
  275. }
  276.  
  277. // "Larson scanner" = Cylon/KITT bouncing light effect
  278. void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
  279.   int i, j, pos, dir;
  280.  
  281.   pos = 0;
  282.   dir = 1;
  283.  
  284.   for(i=0; i<((strip.numPixels()-1) * 8); i++) {
  285.     // Draw 5 pixels centered on pos.  setPixelColor() will clip
  286.     // any pixels off the ends of the strip, no worries there.
  287.     // we'll make the colors dimmer at the edges for a nice pulse
  288.     // look
  289.     strip.setPixelColor(pos - 2, strip.Color(r/4, g/4, b/4));
  290.     strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2));
  291.     strip.setPixelColor(pos, strip.Color(r, g, b));
  292.     strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2));
  293.     strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/4));
  294.  
  295.     strip.show();
  296.     delay(wait);
  297.     // If we wanted to be sneaky we could erase just the tail end
  298.     // pixel, but it's much easier just to erase the whole thing
  299.     // and draw a new one next time.
  300.     for(j=-2; j<= 2; j++)
  301.       strip.setPixelColor(pos+j, strip.Color(0,0,0));
  302.     // Bounce off ends of strip
  303.     pos += dir;
  304.     if(pos < 0) {
  305.       pos = 1;
  306.       dir = -dir;
  307.     }
  308.     else if(pos >= strip.numPixels()) {
  309.       pos = strip.numPixels() - 2;
  310.       dir = -dir;
  311.     }
  312.   }
  313. }
  314.  
  315. // Sine wave effect
  316. #define PI 3.14159265
  317. void wave(uint32_t c, int cycles, uint8_t wait) {
  318.   float y;
  319.   byte  r, g, b, r2, g2, b2;
  320.  
  321.   // Need to decompose color into its r, g, b elements
  322.   g = (c >> 16) & 0x7f;
  323.   r = (c >>  8) & 0x7f;
  324.   b =  c        & 0x7f;
  325.  
  326.   for(int x=0; x<(strip.numPixels()*5); x++)
  327.   {
  328.     for(int i=0; i<strip.numPixels(); i++) {
  329.       y = sin(PI * (float)cycles * (float)(x + i) / (float)strip.numPixels());
  330.       if(y >= 0.0) {
  331.         // Peaks of sine wave are white
  332.         y  = 1.0 - y; // Translate Y to 0.0 (top) to 1.0 (center)
  333.         r2 = 127 - (byte)((float)(127 - r) * y);
  334.         g2 = 127 - (byte)((float)(127 - g) * y);
  335.         b2 = 127 - (byte)((float)(127 - b) * y);
  336.       }
  337.       else {
  338.         // Troughs of sine wave are black
  339.         y += 1.0; // Translate Y to 0.0 (bottom) to 1.0 (center)
  340.         r2 = (byte)((float)r * y);
  341.         g2 = (byte)((float)g * y);
  342.         b2 = (byte)((float)b * y);
  343.       }
  344.       strip.setPixelColor(i, r2, g2, b2);
  345.     }
  346.     strip.show();
  347.     delay(wait);
  348.   }
  349. }
  350.  
  351. /* Helper functions */
  352.  
  353. //Input a value 0 to 384 to get a color value.
  354. //The colours are a transition r - g - b - back to r
  355.  
  356. uint32_t Wheel(uint16_t WheelPos)
  357. {
  358.   byte r, g, b;
  359.   switch(WheelPos / 128)
  360.   {
  361.   case 0:
  362.     r = 127 - WheelPos % 128; // red down
  363.     g = WheelPos % 128;       // green up
  364.     b = 0;                    // blue off
  365.     break;
  366.   case 1:
  367.     g = 127 - WheelPos % 128; // green down
  368.     b = WheelPos % 128;       // blue up
  369.     r = 0;                    // red off
  370.     break;
  371.   case 2:
  372.     b = 127 - WheelPos % 128; // blue down
  373.     r = WheelPos % 128;       // red up
  374.     g = 0;                    // green off
  375.     break;
  376.   }
  377.   return(strip.Color(r,g,b));
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement