Guest User

http://www.youtube.com/watch?v=IUhEINPraN8

a guest
Mar 12th, 2012
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. // BrightBall code
  2. // Public domain.
  3. //
  4. // Thanks to:
  5. // LadyAda for the LED strip and tutorial at http://www.ladyada.net/products/rgbledstrip/
  6.  
  7. #include <Wire.h>
  8.  
  9. #define X_PIN A5
  10. #define Y_PIN A4
  11. #define Z_PIN A3
  12.  
  13. #define RED_PIN   3
  14. #define GREEN_PIN 5
  15. #define BLUE_PIN  6
  16.  
  17.  
  18. // min/max accel vals to clamp to
  19. int minVal = 256;
  20. int maxVal = 440;
  21.  
  22. // min color value
  23. const float mVal = 0.02f;
  24.  
  25. #define HUEBUFSIZE 200
  26. float hueBuf[HUEBUFSIZE];
  27. float hueAccum;
  28. int hueBufPos;
  29. bool ready;
  30.  
  31. #define IDLEMAX 10000
  32. long idleCount;
  33.  
  34. void setup()
  35. {  
  36.   Serial.begin(9600);
  37.  
  38.   hueBufPos = 0;
  39.   hueAccum = 0;
  40.   ready = false;
  41.   memset(&hueBuf[0],0,sizeof(hueBuf));
  42.  
  43.   // Set up pins for ADXL335
  44.   pinMode(X_PIN, INPUT);
  45.   pinMode(Y_PIN, INPUT);
  46.   pinMode(Z_PIN, INPUT);
  47.  
  48.   digitalWrite(X_PIN,LOW);
  49.   digitalWrite(Y_PIN,LOW);
  50.   digitalWrite(Z_PIN,LOW);
  51.  
  52.   // Setup PWM pins for LED strip
  53.   pinMode(RED_PIN,   OUTPUT);
  54.   pinMode(GREEN_PIN, OUTPUT);
  55.   pinMode(BLUE_PIN,  OUTPUT);
  56. }
  57.  
  58.  
  59. void loop()
  60. {
  61.   long ax = analogRead(X_PIN);
  62.  
  63.   ax = max(ax,minVal);
  64.   ax = min(ax,maxVal);
  65.  
  66.   // convert X reading to 0-6 for hue
  67.   float hue = ((((float)ax-minVal)) / (maxVal - minVal)) * 6;
  68.  
  69.   // buffer it
  70.   hueAccum -= hueBuf[hueBufPos];
  71.   hueBuf[hueBufPos] = hue;
  72.   hueAccum += hue;
  73.   hueBufPos = (hueBufPos + 1) % HUEBUFSIZE;
  74.  
  75.   unsigned char cR = 0;
  76.   unsigned char cG = 0;
  77.   unsigned char cB = 0;
  78.  
  79.   if (ready)
  80.   {
  81.     float curHue = (hueAccum / (float)HUEBUFSIZE);
  82.     float hueMod2 = curHue - 2*((int)(curHue / 2 ));
  83.     float x = 1.0f - fabs( hueMod2 - 1.0f );
  84.  
  85.     x = max(x,mVal);
  86.  
  87.     float r,g,b;
  88.     switch ( (int) curHue )
  89.     {
  90.       default:
  91.        r = g = b = mVal;
  92.        break;
  93.       case 0: r = 1.0f;   g = x;       b = mVal; break;
  94.       case 1: r = x;      g = 1.0f;    b = mVal; break;
  95.       case 2: r = mVal;   g = 1.0f;    b = x;    break;
  96.       case 3: r = mVal;   g = x;       b = 1.0f; break;
  97.       case 4: r = x;      g = mVal;    b = 1.0f; break;
  98.       case 5: r = 1.0f;   g = mVal;    b = x;    break;
  99.     }
  100.        
  101.     cR = r * 255;
  102.     cG = g * 255;
  103.     cB = b * 255;    
  104.   }
  105.   else
  106.   {    
  107.     cR = cG = cB = hueBufPos;
  108.  
  109.     if (hueBufPos == HUEBUFSIZE - 1)
  110.         ready = true;
  111.        
  112.   }
  113.  
  114.   // send values to the pwm pins to light the LEDs
  115.   analogWrite(RED_PIN,   cR);
  116.   analogWrite(GREEN_PIN, cG);
  117.   analogWrite(BLUE_PIN,  cB);
  118. }
Add Comment
Please, Sign In to add comment