I want to use a combination of all three directions of motion, so the lights come on whenever I move any which way.  Rather than starting from scratch and having to do my own math, I've borrowed some code from the Sparkle Skirt guide and modified it to work with the Circuit Playground.

#include <Adafruit_CircuitPlayground.h>

float X, Y, Z;
#define MOVE_THRESHOLD 3

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

void loop() {
  //CircuitPlayground.clearPixels();
  X = CircuitPlayground.motionX();
  Y = CircuitPlayground.motionY();
  Z = CircuitPlayground.motionZ();
 
   // Get the magnitude (length) of the 3 axis vector
  // http://en.wikipedia.org/wiki/Euclidean_vector#Length
  double storedVector = X*X;
  storedVector += Y*Y;
  storedVector += Z*Z;
  storedVector = sqrt(storedVector);
  Serial.print("Len: "); Serial.println(storedVector);
  
  // wait a bit
  delay(100);
  
  // get new data!
  X = CircuitPlayground.motionX();
  Y = CircuitPlayground.motionY();
  Z = CircuitPlayground.motionZ();
  double newVector = X*X;
  newVector += Y*Y;
  newVector += Z*Z;
  newVector = sqrt(newVector);
  Serial.print("New Len: "); Serial.println(newVector);
  
  // are we moving 
  if (abs(10*newVector - 10*storedVector) > MOVE_THRESHOLD) {
    Serial.println("Twinkle!");
    //CircuitPlayground.setPixelColor(0, 255,   0,   0);
    delay(1000);
  }
  

  delay(100);
}

Upload this code to your circuit playground and watch the serial monitor as you move the board around.  The word "Twinkle" will appear whenever the board moves far enough.  Change the move threshold number to make it more sensitive or less sensitive.

Lastly, let's get out of the serial monitor and into the world.  We can easily make one of the neopixels on the board twinkle whenever the move threshold is reached. 

Uncomment the "clear neopixels" line of code at the beginning of the loop, and the "CircuitPlayground.setPixelColor" line near the bottom.  Comment out the "delay(1000);" line just below the Set Pixel color command.

Upload your code again, and move your circuit playground around to make the light twinkle.

 

Twinkle ALL The Pixels!

Here is the complete Sparkle Skirt tutorial code modified to twinkle all 10 neopixels on the Circuit Playground.  Go nuts!

#include <Adafruit_CircuitPlayground.h>

float X, Y, Z;
#define NUM_LEDS   10

// Here is where you can put in your favorite colors that will appear!
// just add new {nnn, nnn, nnn}, lines. They will be picked out randomly
//                                  R   G   B
uint8_t myFavoriteColors[][3] = {{200,   0, 200},   // purple
                                 {200,   0,   0},   // red 
                                 {200, 200, 200},   // white
                               };
// don't edit the line below
#define FAVCOLORS sizeof(myFavoriteColors) / 3

// mess with this number to adjust TWINklitude :)
// lower number = more sensitive
#define MOVE_THRESHOLD 10

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

void loop() {
  //CircuitPlayground.clearPixels();
  X = CircuitPlayground.motionX();
  Y = CircuitPlayground.motionY();
  Z = CircuitPlayground.motionZ();
 
   // Get the magnitude (length) of the 3 axis vector
  // http://en.wikipedia.org/wiki/Euclidean_vector#Length
  double storedVector = X*X;
  storedVector += Y*Y;
  storedVector += Z*Z;
  storedVector = sqrt(storedVector);
  Serial.print("Len: "); Serial.println(storedVector);
  
  // wait a bit
  delay(100);
  
  // get new data!
  X = CircuitPlayground.motionX();
  Y = CircuitPlayground.motionY();
  Z = CircuitPlayground.motionZ();
  double newVector = X*X;
  newVector += Y*Y;
  newVector += Z*Z;
  newVector = sqrt(newVector);
  Serial.print("New Len: "); Serial.println(newVector);
  
  // are we moving 
  if (abs(10*newVector - 10*storedVector) > MOVE_THRESHOLD) {
    Serial.println("Twinkle!");
    flashRandom(5, 1);  // first number is 'wait' delay, shorter num == shorter twinkle
    flashRandom(5, 3);  // second number is how many neopixels to simultaneously light up
    flashRandom(5, 2);
  }
}

void flashRandom(int wait, uint8_t howmany) {

  for(uint16_t i=0; i<howmany; i++) {
    // pick a random favorite color!
    int c = random(FAVCOLORS);
    int red = myFavoriteColors[c][0];
    int green = myFavoriteColors[c][1];
    int blue = myFavoriteColors[c][2]; 

    // get a random pixel from the list
    int j = random(NUM_LEDS);
    //Serial.print("Lighting up "); Serial.println(j); 
    
    // now we will 'fade' it in 5 steps
    for (int x=0; x < 5; x++) {
      int r = red * (x+1); r /= 5;
      int g = green * (x+1); g /= 5;
      int b = blue * (x+1); b /= 5;
      
      CircuitPlayground.setPixelColor(j, r, g, b);
      delay(wait);
    }
    // & fade out in 5 steps
    for (int x=5; x >= 0; x--) {
      int r = red * x; r /= 5;
      int g = green * x; g /= 5;
      int b = blue * x; b /= 5;
      
      CircuitPlayground.setPixelColor(j, r, g, b);
      delay(wait);
    }
  }
  // LEDs will be off when done (they are faded to 0)
}

This guide was first published on Feb 18, 2017. It was last updated on Feb 18, 2017.

This page (Twinkle) was last updated on Feb 17, 2017.

Text editor powered by tinymce.