/ Fiber_Optic_Pixie_Skirt / Fiber_Optic_Pixie_Skirt.ino
Fiber_Optic_Pixie_Skirt.ino
  1  // SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  #include <Adafruit_Pixie.h>
  6  #include "SoftwareSerial.h"
  7  #include <Wire.h>
  8  #include <Adafruit_Sensor.h>
  9  #include <Adafruit_LSM303DLH_Mag.h>
 10  #include <Adafruit_LSM303_Accel.h>
 11  
 12  int ledMode = 0;  //FIRST ACTIVE MODE
 13  
 14  #define NUMPIXIES 5 // Number of Pixies in the fiber optics
 15  #define PIXIEPIN 6
 16  
 17  
 18  SoftwareSerial pixieSerial(-1, PIXIEPIN);
 19  
 20  Adafruit_Pixie strip = Adafruit_Pixie(NUMPIXIES, &pixieSerial);
 21  
 22  Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);
 23  Adafruit_LSM303DLH_Mag_Unified mag = Adafruit_LSM303DLH_Mag_Unified(12345);
 24  
 25  const float twirl = 7; // accelerometer threshold for toggling modes -- change this number to change sensitivity
 26  long twirlStart = 0;
 27  long twirlTime = 2000;
 28  
 29  
 30  // Set your colors for RandomFlash mode here.
 31  // just add new {nnn, nnn, nnn}, lines. They will be picked out randomly
 32  //                                  R   G   B
 33  uint8_t myFavoriteColors[][3] = {{255,   0,   0},   // red
 34                                   {255,   150,   0},   // orange 
 35                                   {251, 255, 0},   // yellow
 36                                 };
 37  // don't edit the line below
 38  #define FAVCOLORS sizeof(myFavoriteColors) / 3
 39  
 40  void setup() 
 41  {
 42    Serial.begin(9600);
 43    // Initialize the sensors
 44    accel.begin();
 45    mag.begin();
 46     
 47    pixieSerial.begin(115200);
 48    strip.show(); // Initialize all pixels to 'off
 49  }
 50  
 51  
 52  #define NUM_MODES 4  //change this if you add more modes
 53  
 54  //------------------MAIN LOOP------------------
 55  void loop() {
 56      switch (ledMode) {
 57         case 0: colorWipe(strip.Color(200, 20, 20), 20); break;   //  red
 58         case 1: colorWipe(strip.Color(20, 200, 50), 20); break;   //   yellow
 59         case 2: colorWipe(strip.Color(200, 0, 200), 20); break;  // purple
 60         case 3: rainbowfill(); break;              //rainbow
 61         case 4: flashRandom(5, 4); break;      // flash      
 62      
 63      }  
 64  
 65     // Now read the accelerometer to control the motion.
 66     sensors_event_t event; 
 67     accel.getEvent(&event);
 68   
 69     // Check for mode change commands
 70     CheckFortwirls(event);
 71  
 72    }
 73  
 74  // monitor orientation for mode-change 'gestures'
 75  void CheckFortwirls(sensors_event_t event)
 76  {
 77     if (event.acceleration.x > twirl)
 78     {
 79       if (millis() - twirlStart > twirlTime)
 80       {
 81      Serial.println("Twirl!");
 82      colorWipe(strip.Color(0, 0, 0), 10);
 83      ledMode++;
 84      if (ledMode > NUM_MODES){
 85      ledMode=0; }
 86       }
 87     }
 88     else if (event.acceleration.x < -(twirl + 1))
 89     {
 90       if (millis() - twirlStart > twirlTime)
 91       {
 92         Serial.println("Twirl Back!");
 93      colorWipe(strip.Color(0, 0, 0), 10);
 94      ledMode--;
 95      if (ledMode < 0){
 96      ledMode=NUM_MODES;     
 97       }
 98     }
 99     else // no nods in progress
100     {
101       twirlStart = millis(); // reset timer
102     }
103  }
104  }
105  
106  void flashRandom(int wait, uint8_t howmany) {
107  
108    for(uint16_t i=0; i<howmany; i++) {
109      // pick a random favorite color!
110      int c = random(FAVCOLORS);
111      int red = myFavoriteColors[c][0];
112      int green = myFavoriteColors[c][1];
113      int blue = myFavoriteColors[c][2]; 
114  
115      // get a random pixel from the list
116      int j = random(strip.numPixels());
117      //Serial.print("Lighting up "); Serial.println(j); 
118      
119      // now we will 'fade' it in 5 steps
120      for (int x=0; x < 5; x++) {
121        int r = red * (x+1); r /= 5;
122        int g = green * (x+1); g /= 5;
123        int b = blue * (x+1); b /= 5;
124        
125        strip.setPixelColor(j, strip.Color(r, g, b));
126        strip.show();
127        delay(wait);
128      }
129      // & fade out in 5 steps
130      for (int x=5; x >= 0; x--) {
131        int r = red * x; r /= 5;
132        int g = green * x; g /= 5;
133        int b = blue * x; b /= 5;
134        
135        strip.setPixelColor(j, strip.Color(r, g, b));
136        strip.show();
137        delay(wait);
138      }
139    }
140  
141  }
142  
143  // Fill the dots one after the other with a color
144  void colorWipe(uint32_t c, uint8_t wait) {
145    for(uint16_t i=0; i<strip.numPixels(); i++) {
146      strip.setPixelColor(i, c);
147      strip.show();
148      delay(wait);
149    }
150  }
151  
152  // Fill the dots one after the other with a color
153  void rainbowfill() {
154    strip.setPixelColor(0, strip.Color(255, 0, 0));
155    strip.setPixelColor(1, strip.Color(200, 100, 0));
156    strip.setPixelColor(2, strip.Color(0, 255, 0));
157    strip.setPixelColor(3, strip.Color(50, 50, 200));
158    strip.setPixelColor(4, strip.Color(255, 0, 255));
159    strip.show();
160    delay(100);
161  }