Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * Accelerometer connection pins (I2C) to Arduino are shown below:
- Arduino Accelerometer ADXL345
- A5 SCL
- A4 SDA
- 3.3V CS
- 3.3V VCC
- GND GND
- */
- #include "FastLED.h"
- FASTLED_USING_NAMESPACE
- #include <Wire.h>
- #include <ADXL345.h>
- #if FASTLED_VERSION < 3001000
- #error "Requires FastLED 3.1 or later; check github for latest code."
- #endif
- #define DATA_PIN 6
- //#define CLK_PIN 4
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB
- #define NUM_LEDS 30
- CRGB leds[NUM_LEDS];
- #define BRIGHTNESS 96
- #define FRAMES_PER_SECOND 120
- int ledMode = 1;
- ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
- int x,y,z;
- int rawX, rawY, rawZ;
- float X, Y, Z;
- float rollrad, pitchrad;
- float rolldeg, pitchdeg;
- float totalAccel;
- #define LEDINTERVAL (1000UL)
- unsigned long rolltime = millis() + LEDINTERVAL;
- void setup(){
- Serial.begin(115200);
- adxl.powerOn();
- delay(3000); // 3 second delay for recovery
- // tell FastLED about the LED strip configuration
- FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- // set master brightness control
- FastLED.setBrightness(BRIGHTNESS);
- }
- // List of patterns to cycle through. Each is defined as a separate function below.
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- void loop(){
- // signed comparison for proper handling of timer rollover
- // if((long)(millis() - rolltime) >= 0) {
- ledMode = 1;
- adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z
- // Output (x,y,z) on horizontal plane should be approximately (0,0,255)
- // the following 3 lines is for an offset
- rawX=x-7;
- rawY=y-6;
- rawZ=z+10;
- X = rawX/256.00; // used for angle calculations
- Y = rawY/256.00; // used for angle calculations
- Z = rawZ/256.00; // used for angle calculations
- rollrad = atan(Y/sqrt(X*X+Z*Z)); // calculated angle in radians
- pitchrad = atan(X/sqrt(Y*Y+Z*Z)); // calculated angle in radians
- rolldeg = 180*(atan(Y/sqrt(X*X+Z*Z)))/PI; // calculated angle in degrees
- pitchdeg = 180*(atan(X/sqrt(Y*Y+Z*Z)))/PI; // calculated angle in degrees
- totalAccel = sqrt( sq(x) +sq(y) + sq(z) - sq(9.81) );
- // print out values:
- /*
- Serial.print("x: "); Serial.print(x); // raw data without offset
- Serial.print(" y: "); Serial.print(y); // raw data without offset
- Serial.print(" z: "); Serial.print(z); // raw data without offset
- Serial.print(" rawX = "); Serial.print(rawX); // raw data with offset
- Serial.print(" rawY = "); Serial.print(rawY); // raw data with offset
- Serial.print(" rawZ = "); Serial.print(rawZ); // raw data with offset
- Serial.print(" X = "); Serial.print(X); // raw data with offset and divided by 256
- Serial.print(" Y = "); Serial.print(Y); // raw data with offset and divided by 256
- Serial.print(" Z = "); Serial.print(Z); // raw data with offset and divided by 256
- */
- Serial.print("\t Angle according to x axis (Roll(deg)) = "); Serial.print(rolldeg); // calculated angle in degrees
- Serial.print("\t Angle according to y axis (Pitch(deg)) = "); Serial.println(pitchdeg); // calculated angle in degrees
- // Serial.print(" Roll(rad) = "); Serial.print(rollrad); // calculated angle in radians
- // Serial.print(" Pitch(rad) = "); Serial.print(pitchrad); // calculated angle in radians
- if (( X > .2 ) and (totalAccel > 10 ))
- {
- ledMode = 3;
- }
- else if ( ( X < -.11 ) and (totalAccel > 10 ))
- {
- ledMode= 2;
- }
- Serial.print(" LedMode = "); Serial.print(ledMode);
- Serial.print("Total Accel: "); Serial.print(totalAccel);
- // rolltime += LEDINTERVAL;
- // }
- if (ledMode != 999) {
- switch (ledMode) {
- case 1: sinelon(); break;
- case 2: juggle(); break;
- case 3: bpm(); break;
- }
- }
- show_at_max_brightness_for_power();
- delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
- // FastLED.show();
- // FastLED.delay(1000/FRAMES_PER_SECOND);
- // EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
- }
- void confetti()
- {
- // random colored speckles that blink in and fade smoothly
- fadeToBlackBy( leds, NUM_LEDS, 10);
- int pos = random16(NUM_LEDS);
- leds[pos] += CHSV( gHue + random8(64), 200, 255);
- }
- void sinelon()
- {
- // a colored dot sweeping back and forth, with fading trails
- fadeToBlackBy( leds, NUM_LEDS, 20);
- int pos = beatsin16(13,0,NUM_LEDS);
- leds[pos] += CHSV( gHue, 255, 192);
- }
- void bpm()
- {
- // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
- uint8_t BeatsPerMinute = 62;
- CRGBPalette16 palette = PartyColors_p;
- uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
- }
- }
- void juggle() {
- // eight colored dots, weaving in and out of sync with each other
- fadeToBlackBy( leds, NUM_LEDS, 20);
- byte dothue = 0;
- for( int i = 0; i < 8; i++) {
- leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
- dothue += 32;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment