LuminousElement

PulseWave.ino

May 23rd, 2013
7,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1.  
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <math.h>
  5. #include "WS2811.h"
  6.  
  7. #define BIT(B)           (0x01 << (uint8_t)(B))
  8. #define SET_BIT_HI(V, B) (V) |= (uint8_t)BIT(B)
  9. #define SET_BIT_LO(V, B) (V) &= (uint8_t)~BIT(B)
  10.  
  11. #define PAUSE  1000     // msec
  12. #define DELAY    10 // msec
  13. #define CONST_E 2.71828
  14.  
  15. // Define the output function, using pin 0 on port b.
  16. DEFINE_WS2811_FN(WS2811RGB, PORTB, 0)
  17.  
  18. void setup()
  19. {
  20.     // Configure pin for output.
  21.     SET_BIT_HI(DDRB, 0);
  22.     SET_BIT_LO(PORTB, 0);
  23. //  Serial.begin(9600);
  24. }
  25.  
  26. float   cR = 1,
  27.         cG = 0,
  28.         cB = 0;
  29.  
  30. // Drive the three pixels in an infinit loop.
  31. void loop(void)
  32. {
  33.     // off->red, off->green, off->blue
  34.     RGB_t rgb[240] = {
  35.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  36.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  37.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  38.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  39.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  40.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  41.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  42.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  43.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  44.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  45.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  46.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  47.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  48.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  49.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  50.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  51.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  52.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  53.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  54.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  55.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  56.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  57.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  58.     {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}
  59.     };
  60.     const int Elements = ARRAYLEN(rgb);
  61.  
  62.     WS2811RGB(rgb, Elements);
  63.     _delay_ms(PAUSE);
  64.  
  65.     int col = 0;
  66.    
  67.     // loop forever.
  68.     for (;;) {
  69.         for(int k=0; k<10; k++) {
  70.             for(int i = Elements - 1; i >= 1; i--)
  71.             {
  72.                 rgb[i].r = rgb[i - 1].r;
  73.                 rgb[i].g = rgb[i - 1].g;
  74.                 rgb[i].b = rgb[i - 1].b;
  75.             }
  76.            
  77.             long micLevel = analogRead(0);
  78.             int log4Level = log(micLevel) / log(4) * 12.5;
  79.             int squareLevel = micLevel * micLevel * micLevel / ((long)4 * 1024 * 1024);
  80.             float sCurveLevel = (int)(sCurve(micLevel, 4) * 255);
  81.            
  82.             float fMicLevel = micLevel / 4.0f / 200.0f;
  83.             float quadraticLevel = pow(fMicLevel, 2);
  84.            
  85.             //Serial.println(squareLevel);
  86.            
  87.             const float one = 1.f;
  88.            
  89.             rgb[0].r = rgb[0].g = rgb[0].b = 0;
  90.            
  91.             setRgb(&(rgb[0]), one);
  92.            
  93.             //if(log4Level > squareLevel && log4Level > sCurveLevel)
  94.                 //rgb[0].r = log4Level;
  95.             //else if(squareLevel > log4Level && squareLevel > sCurveLevel)
  96.                 //rgb[0].g = quadraticLevel * 255;
  97.             //else if(sCurveLevel > log4Level && sCurveLevel > squareLevel)
  98.                 //rgb[0].b = sCurveLevel;
  99.                
  100.             WS2811RGB(rgb, Elements);
  101.             //_delay_ms(5);
  102.             fadeRgb();
  103.         }
  104.        
  105.         //_delay_ms(500);
  106.        
  107.         col = ++col % 6;
  108.         //cycleRgb(col);
  109.     }
  110. }
  111.  
  112. const int phaseLength = 30;
  113. const int period = phaseLength * 6;
  114. int iteration = 0;
  115. void fadeRgb() {
  116.    
  117.     // Determine which phase we are in
  118.     int phase = iteration / phaseLength;
  119.     int step = iteration % phaseLength;
  120.    
  121.     switch(phase)
  122.     {
  123.         case 0: // Red on, green increasing, blue off
  124.         cR = 1;
  125.         cG = step / ((float)phaseLength);
  126.         cB = 0;
  127.         break;
  128.        
  129.         case 1: // Red decreasing, green on, blue off
  130.         cR = (phaseLength - step) / ((float)phaseLength);
  131.         cG = 1;
  132.         cB = 0;
  133.         break;
  134.        
  135.         case 2: // Red off, green on, blue increasing
  136.         cR = 0;
  137.         cG = 1;
  138.         cB = step / ((float)phaseLength);
  139.         break;
  140.        
  141.         case 3: // Red off, green decreasing, blue on
  142.         cR = 0;
  143.         cG = (phaseLength - step) / ((float)phaseLength);
  144.         cB = 1;
  145.         break;
  146.        
  147.         case 4: // Red increasing, green off, blue on
  148.         cR = step / ((float)phaseLength);
  149.         cG = 0;
  150.         cB = 1;
  151.         break;
  152.        
  153.         case 5: // Red on, green off, blue decreasing
  154.         cR = 1;
  155.         cG = 0;
  156.         cB = (phaseLength - step) / ((float)phaseLength);
  157.         break;
  158.     }
  159.    
  160.     iteration = ++iteration % period;
  161. }
  162.  
  163. void cycleRgb(int col) {
  164.     switch(col)
  165.     {
  166.         case 0:
  167.         cR = 1;
  168.         cG = 0;
  169.         cB = 0;
  170.         break;
  171.        
  172.         case 1:
  173.         cR = 0.5;
  174.         cG = 0.5;
  175.         cB = 0;
  176.         break;
  177.        
  178.         case 2:
  179.         cR = 0;
  180.         cG = 1;
  181.         cB = 0;
  182.         break;
  183.        
  184.         case 3:
  185.         cR = 0;
  186.         cG = 0.5;
  187.         cB = 0.5;
  188.         break;
  189.        
  190.         case 4:
  191.         cR = 0;
  192.         cG = 0;
  193.         cB = 1;
  194.         break;
  195.        
  196.         case 5:
  197.         cR = 0.5;
  198.         cG = 0;
  199.         cB = 0.5;
  200.         break;
  201.     }
  202. }
  203.  
  204. void setRgb(RGB_t* rgb, float val)
  205. {
  206.     if (val > 1.0f) val = 1.0f;
  207.     rgb->r = (int)(val * cR * 255);
  208.     rgb->g = (int)(val * cG * 255);
  209.     rgb->b = (int)(val * cB * 255);
  210. }
  211.  
  212. float sCurve(int micInput, float power)
  213. {
  214.     float normalised = micInput / 1024.0f;
  215.    
  216.     return normalised / (pow(1 + pow(normalised, power), 1.0f / power) );
  217. }
Advertisement
Add Comment
Please, Sign In to add comment