sparkle_skirt.ino
1 // SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #include <Wire.h> 6 #include <Adafruit_Sensor.h> 7 #include <Adafruit_LSM303DLH_Mag.h> 8 #include <Adafruit_NeoPixel.h> 9 10 // Parameter 1 = number of pixels in strip 11 // Parameter 2 = pin number (most are valid) 12 // Parameter 3 = pixel type flags, add together as needed: 13 // NEO_RGB Pixels are wired for RGB bitstream 14 // NEO_GRB Pixels are wired for GRB bitstream 15 // NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels) 16 // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip) 17 Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, 6, NEO_GRB + NEO_KHZ800); 18 Adafruit_LSM303DLH_Mag_Unified accel = Adafruit_LSM303DLH_Mag_Unified(54321); 19 20 // Here is where you can put in your favorite colors that will appear! 21 // just add new {nnn, nnn, nnn}, lines. They will be picked out randomly 22 // R G B 23 uint8_t myFavoriteColors[][3] = {{200, 0, 200}, // purple 24 {200, 0, 0}, // red 25 {200, 200, 200}, // white 26 }; 27 // don't edit the line below 28 #define FAVCOLORS sizeof(myFavoriteColors) / 3 29 30 // mess with this number to adjust TWINklitude :) 31 // lower number = more sensitive 32 #define MOVE_THRESHOLD 45 33 34 void setup() 35 { 36 Serial.begin(9600); 37 38 // Try to initialise and warn if we couldn't detect the chip 39 if (!accel.begin()) 40 { 41 Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); 42 while (1); 43 } 44 strip.begin(); 45 strip.show(); // Initialize all pixels to 'off' 46 } 47 48 void loop() 49 { 50 /* Get a new sensor event */ 51 sensors_event_t event; 52 accel.getEvent(&event); 53 Serial.print("Accel X: "); Serial.print(event.acceleration.x); Serial.print(" "); 54 Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" "); 55 Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" "); 56 57 // Get the magnitude (length) of the 3 axis vector 58 // http://en.wikipedia.org/wiki/Euclidean_vector#Length 59 double storedVector = event.acceleration.x*event.acceleration.x; 60 storedVector += event.acceleration.y*event.acceleration.y; 61 storedVector += event.acceleration.z*event.acceleration.z; 62 storedVector = sqrt(storedVector); 63 Serial.print("Len: "); Serial.println(storedVector); 64 65 // wait a bit 66 delay(100); 67 68 // get new data! 69 accel.getEvent(&event); 70 double newVector = event.acceleration.x*event.acceleration.x; 71 newVector += event.acceleration.y*event.acceleration.y; 72 newVector += event.acceleration.z*event.acceleration.z; 73 newVector = sqrt(newVector); 74 Serial.print("New Len: "); Serial.println(newVector); 75 76 // are we moving 77 if (abs(newVector - storedVector) > MOVE_THRESHOLD) { 78 Serial.println("Twinkle!"); 79 flashRandom(5, 1); // first number is 'wait' delay, shorter num == shorter twinkle 80 flashRandom(5, 3); // second number is how many neopixels to simultaneously light up 81 flashRandom(5, 2); 82 } 83 } 84 85 void flashRandom(int wait, uint8_t howmany) { 86 87 for(uint16_t i=0; i<howmany; i++) { 88 // pick a random favorite color! 89 int c = random(FAVCOLORS); 90 int red = myFavoriteColors[c][0]; 91 int green = myFavoriteColors[c][1]; 92 int blue = myFavoriteColors[c][2]; 93 94 // get a random pixel from the list 95 int j = random(strip.numPixels()); 96 //Serial.print("Lighting up "); Serial.println(j); 97 98 // now we will 'fade' it in 5 steps 99 for (int x=0; x < 5; x++) { 100 int r = red * (x+1); r /= 5; 101 int g = green * (x+1); g /= 5; 102 int b = blue * (x+1); b /= 5; 103 104 strip.setPixelColor(j, strip.Color(r, g, b)); 105 strip.show(); 106 delay(wait); 107 } 108 // & fade out in 5 steps 109 for (int x=5; x >= 0; x--) { 110 int r = red * x; r /= 5; 111 int g = green * x; g /= 5; 112 int b = blue * x; b /= 5; 113 114 strip.setPixelColor(j, strip.Color(r, g, b)); 115 strip.show(); 116 delay(wait); 117 } 118 } 119 // LEDs will be off when done (they are faded to 0) 120 }