Created
January 18, 2017 11:27
-
-
Save t27/1482cd3062d4d7e7d3a8fdcb61dc3ae4 to your computer and use it in GitHub Desktop.
SparkleSkirt arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_LSM303.h> | |
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321); | |
// mess with this number to adjust TWINklitude :) | |
// lower number = more sensitive | |
#define MOVE_THRESHOLD 45 | |
int brightness = 0; | |
int ledPin = 9; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
// Try to initialise and warn if we couldn't detect the chip | |
if (!accel.begin()) | |
{ | |
Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); | |
while (1); | |
} | |
} | |
void loop() | |
{ | |
/* Get a new sensor event */ | |
sensors_event_t event; | |
accel.getEvent(&event); | |
Serial.print("Accel X: "); Serial.print(event.acceleration.x); Serial.print(" "); | |
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" "); | |
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" "); | |
// Get the magnitude (length) of the 3 axis vector | |
// http://en.wikipedia.org/wiki/Euclidean_vector#Length | |
double storedVector = event.acceleration.x*event.acceleration.x; | |
storedVector += event.acceleration.y*event.acceleration.y; | |
storedVector += event.acceleration.z*event.acceleration.z; | |
storedVector = sqrt(storedVector); | |
Serial.print("Len: "); Serial.println(storedVector); | |
// wait a bit | |
delay(100); | |
// get new data! | |
accel.getEvent(&event); | |
double newVector = event.acceleration.x*event.acceleration.x; | |
newVector += event.acceleration.y*event.acceleration.y; // Since Y axis is downwards, ignore movements in gravity direction | |
newVector += event.acceleration.z*event.acceleration.z; | |
newVector = sqrt(newVector); | |
Serial.print("New Len: "); Serial.println(newVector); | |
// are we moving | |
if (abs(newVector - storedVector) > MOVE_THRESHOLD) { | |
Serial.println("Twinkle!"); | |
fadeLeds(); | |
// OR | |
// blinkLeds(); | |
} | |
} | |
void fadeLeds() { | |
int fadeAmount = 5; // Increase this to fade faster, should be a divisor of 255 | |
for(int i = 0; i < 50;i++) { | |
// set the brightness of pin 9: | |
analogWrite(ledPin, brightness); | |
// change the brightness for next time through the loop: | |
brightness = brightness + fadeAmount; | |
// reverse the direction of the fading at the ends of the fade: | |
if (brightness == 0 || brightness == 255) { | |
fadeAmount = -fadeAmount ; | |
} | |
// wait for 30 milliseconds to see the dimming effect | |
delay(30); | |
// LEDs will be off when done (they are faded to 0) | |
} | |
} | |
void blinkLeds() { | |
for(int i = 0;i < 10; i++) { | |
digitalWrite(ledPin, HIGH); | |
delay(200); | |
digitalWrite(ledPin, LOW); | |
delay(200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment