Stap 6: Code voor de Flora
Ik heb gebruikt een gemodificeerde versie van de code die is geschreven voor het project Adafruit Sparkle rok. Deze code is goed gedocumenteerd en geannoteerde waardoor het vrij eenvoudig te wijzigen. Ik heb geprobeerd om mijn wijzigingen omvatten het toevoegen van de favoriete kleuren, aantekeningen te wijzigen flash duur en timing, en extra willekeurigheid voor flash patronen.
#include #include #include // Parameter 1 = number of pixels in strip // Parameter 2 = pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_RGB Pixels are wired for RGB bitstream // NEO_GRB Pixels are wired for GRB bitstream // NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels) // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip) Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, 6, NEO_GRB + NEO_KHZ800); Adafruit_LSM303 lsm; // 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 // * Changed and added colors // R G B uint8_t myFavoriteColors[][5] = {{175, 0, 175}, // purple {0, 175, 175}, // yellow {175, 175, 175}, // white { 0, 0, 175}, // blue {175, 75, 75}, // pink }; // 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 125 void setup() { Serial.begin(9600); // Try to initialise and warn if we couldn't detect the chip if (!lsm.begin()) { Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); while (1); } strip.begin(); strip.show(); // Initialize all pixels to 'off' } void loop() { // Take a reading of accellerometer data lsm.read(); Serial.print("Accel X: "); Serial.print(lsm.accelData.x); Serial.print(" "); Serial.print("Y: "); Serial.print(lsm.accelData.y); Serial.print(" "); Serial.print("Z: "); Serial.print(lsm.accelData.z); Serial.print(" "); // Get the magnitude (length) of the 3 axis vector // http://en.wikipedia.org/wiki/Euclidean_vector#Length double storedVector = lsm.accelData.x*lsm.accelData.x; storedVector += lsm.accelData.y*lsm.accelData.y; storedVector += lsm.accelData.z*lsm.accelData.z; storedVector = sqrt(storedVector); Serial.print("Len: "); Serial.println(storedVector); // wait a bit delay(100); // get new data! lsm.read(); double newVector = lsm.accelData.x*lsm.accelData.x; newVector += lsm.accelData.y*lsm.accelData.y; newVector += lsm.accelData.z*lsm.accelData.z; newVector = sqrt(newVector); Serial.print("New Len: "); Serial.println(newVector); // are we moving if (abs(newVector - storedVector) > MOVE_THRESHOLD) { Serial.println("Twinkle!"); // 'twink' is 'wait' delay, shorter num == shorter twinkle // 'numPix' is how many neopixels to simultaneously light up // Added a simple for loop with several random variables to increase // the variation in flash patterns and simultaneous pixels int numLoop = random(3)+2; for(int t=0; tvoid flashRandom(int wait, uint8_t howmany) { for(uint16_t i=0; i= 0; x--) { int r = red * x; r /= 5; int g = green * x; g /= 5; int b = blue * x; b /= 5; strip.setPixelColor(j, strip.Color(r, g, b)); strip.show(); delay(wait); } } // LEDs will be off when done (they are faded to 0) }