Stap 1: De code
#include <Wire.h> #include <Adafruit_TCS34725.h> #include <Adafruit_LSM303.h> #include <Adafruit_NeoPixel.h> // 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) #define NUM_PIXELS 4 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, 6, NEO_GRB + NEO_KHZ800); Adafruit_TCS34725 color_sensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); Adafruit_LSM303 accel; #define STILL_LIGHT // define if light is to be on when no movement. // Otherwise dark // our RGB -> eye-recognized gamma color byte gammatable[256]; int g_red, g_green, g_blue; // global colors read by color sensor int j; // mess with this number to adjust TWINklitude :) // lower number = more sensitive #define MOVE_THRESHOLD 45 #define FADE_RATE 5 int led = 7; double newVector; void flash(int times) { for (int i = 0; i < times; i++) { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(150); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(150); } } float r, g, b; double storedVector; void setup() { pinMode(led, 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) { flash(4); delay(1000); }; } strip.begin(); strip.show(); // Initialize all pixels to 'off' if (!color_sensor.begin()) { Serial.println("No TCS34725 found ... check your connections"); while (1) { flash(3); delay(1000); }; } // thanks PhilB for this gamma table! // it helps convert RGB colors to what humans see for (int i = 0; i < 256; i++) { float x = i; x /= 255; x = pow(x, 2.5); x *= 255; gammatable[i] = x; } //this sequence flashes the first pixel three times // as a countdown to the color reading. for (int i = 0; i < 3; i++) { //white, but dimmer-- 255 for all three values makes it blinding! strip.setPixelColor(0, strip.Color(188, 188, 188)); strip.show(); delay(1000); strip.setPixelColor(0, strip.Color(0, 0, 0)); strip.show(); delay(500); } uint16_t clear, red, green, blue; color_sensor.setInterrupt(false); // turn on LED delay(60); // takes 50ms to read color_sensor.getRawData(&red, &green, &blue, &clear); color_sensor.setInterrupt(true); // turn off LED // Figure out some basic hex code for visualization uint32_t sum = red; sum += green; sum += blue; sum = clear; r = red; r /= sum; g = green; g /= sum; b = blue; b /= sum; r *= 256; g *= 256; b *= 256; g_red = gammatable[(int)r]; g_green = gammatable[(int)g]; g_blue = gammatable[(int)b]; // Get the magnitude (length) of the 3 axis vector // http://en.wikipedia.org/wiki/Euclidean_vector#Length accel.read(); storedVector = accel.accelData.x*accel.accelData.x; storedVector += accel.accelData.y*accel.accelData.y; storedVector += accel.accelData.z*accel.accelData.z; storedVector = sqrt(storedVector); } void loop() { // get new data accel.read(); double newVector = accel.accelData.x*accel.accelData.x; newVector += accel.accelData.y*accel.accelData.y; newVector += accel.accelData.z*accel.accelData.z; newVector = sqrt(newVector); // are we moving if (abs(newVector - storedVector) > MOVE_THRESHOLD) { colorWipe(strip.Color(0, 0, 0), 0); flashRandom(10, 25); // first number is 'wait' delay, // shorter num == shorter twinkle // second number is how many neopixels to // simultaneously light up } #ifdef STILL_LIGHT else { colorWipe(strip.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0); storedVector = newVector; } #endif } void flashRandom(int wait, uint8_t howmany) { for (uint16_t i = 0; i < howmany; i++) { for (int simul_pixels = 0; simul_pixels < 8; simul_pixels++) { // get a random pixel from the list j = random(strip.numPixels()); strip.setPixelColor(j, strip.Color(g_red, g_green, g_blue)); } strip.show(); delay(wait); colorWipe(strip.Color(0, 0, 0), 0); // now we will 'fade' it in FADE_RATE steps for (int x = 0; x < FADE_RATE; x++) { int r = g_red * (x + 1); r /= FADE_RATE; int g = g_green * (x + 1); g /= FADE_RATE; int b = g_blue * (x + 1); b /= FADE_RATE; strip.setPixelColor(j, strip.Color(r, g, b)); strip.show(); delay(wait); } // & fade out for (int x = FADE_RATE; x >= 0; x--) { int r = g_red * x; r /= FADE_RATE; int g = g_green * x; g /= FADE_RATE; int b = g_blue * x; b /= FADE_RATE; strip.setPixelColor(j, strip.Color(r, g, b)); strip.show(); delay(wait); } } #ifdef STILL_LIGHT colorWipe(strip.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0); #endif } // Fill the dots one after the other with a color void colorWipe(uint32_t c, uint8_t wait) { for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); delay(wait); } }