Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BrightBall code
- // Public domain.
- //
- // Thanks to:
- // LadyAda for the LED strip and tutorial at http://www.ladyada.net/products/rgbledstrip/
- #include <Wire.h>
- #define X_PIN A5
- #define Y_PIN A4
- #define Z_PIN A3
- #define RED_PIN 3
- #define GREEN_PIN 5
- #define BLUE_PIN 6
- // min/max accel vals to clamp to
- int minVal = 256;
- int maxVal = 440;
- // min color value
- const float mVal = 0.02f;
- #define HUEBUFSIZE 200
- float hueBuf[HUEBUFSIZE];
- float hueAccum;
- int hueBufPos;
- bool ready;
- #define IDLEMAX 10000
- long idleCount;
- void setup()
- {
- Serial.begin(9600);
- hueBufPos = 0;
- hueAccum = 0;
- ready = false;
- memset(&hueBuf[0],0,sizeof(hueBuf));
- // Set up pins for ADXL335
- pinMode(X_PIN, INPUT);
- pinMode(Y_PIN, INPUT);
- pinMode(Z_PIN, INPUT);
- digitalWrite(X_PIN,LOW);
- digitalWrite(Y_PIN,LOW);
- digitalWrite(Z_PIN,LOW);
- // Setup PWM pins for LED strip
- pinMode(RED_PIN, OUTPUT);
- pinMode(GREEN_PIN, OUTPUT);
- pinMode(BLUE_PIN, OUTPUT);
- }
- void loop()
- {
- long ax = analogRead(X_PIN);
- ax = max(ax,minVal);
- ax = min(ax,maxVal);
- // convert X reading to 0-6 for hue
- float hue = ((((float)ax-minVal)) / (maxVal - minVal)) * 6;
- // buffer it
- hueAccum -= hueBuf[hueBufPos];
- hueBuf[hueBufPos] = hue;
- hueAccum += hue;
- hueBufPos = (hueBufPos + 1) % HUEBUFSIZE;
- unsigned char cR = 0;
- unsigned char cG = 0;
- unsigned char cB = 0;
- if (ready)
- {
- float curHue = (hueAccum / (float)HUEBUFSIZE);
- float hueMod2 = curHue - 2*((int)(curHue / 2 ));
- float x = 1.0f - fabs( hueMod2 - 1.0f );
- x = max(x,mVal);
- float r,g,b;
- switch ( (int) curHue )
- {
- default:
- r = g = b = mVal;
- break;
- case 0: r = 1.0f; g = x; b = mVal; break;
- case 1: r = x; g = 1.0f; b = mVal; break;
- case 2: r = mVal; g = 1.0f; b = x; break;
- case 3: r = mVal; g = x; b = 1.0f; break;
- case 4: r = x; g = mVal; b = 1.0f; break;
- case 5: r = 1.0f; g = mVal; b = x; break;
- }
- cR = r * 255;
- cG = g * 255;
- cB = b * 255;
- }
- else
- {
- cR = cG = cB = hueBufPos;
- if (hueBufPos == HUEBUFSIZE - 1)
- ready = true;
- }
- // send values to the pwm pins to light the LEDs
- analogWrite(RED_PIN, cR);
- analogWrite(GREEN_PIN, cG);
- analogWrite(BLUE_PIN, cB);
- }
Add Comment
Please, Sign In to add comment