Feather_32u4_Lights.ino
1 // SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // Adafruit 32u4 Feather Color Sensing Holiday Lights 6 // See the full guide at: 7 // https://learn.adafruit.com/feather-holiday-lights/overview 8 // Author: Tony DiCola 9 // Released under a MIT license: 10 // https://opensource.org/licenses/MIT 11 #include "Adafruit_NeoPixel.h" 12 #include "Adafruit_TCS34725.h" 13 #include "Adafruit_VCNL4000.h" 14 #include "Adafruit_VCNL4010.h" 15 #include "Wire.h" 16 17 18 #define PIXEL_COUNT 60 // The number of NeoPixels connected to the board. 19 20 #define PIXEL_PIN 6 // The pin connected to the input of the NeoPixels. 21 22 #define PIXEL_TYPE NEO_GRB + NEO_KHZ800 // The type of NeoPixels, see the NeoPixel 23 // strandtest example for more options. 24 25 #define ANIMATION_PERIOD_MS 300 // The amount of time (in milliseconds) that each 26 // animation frame lasts. Decrease this to speed 27 // up the animation, and increase it to slow it down. 28 29 #define TCS_LED_PIN 5 // The digital pin connected to the TCS color sensor LED pin. 30 // Will control turning the sensor's LED on and off. 31 32 #define PROXIMITY_THRESHOLD 10000 // The threshold value to consider an object near 33 // and attempt to read its color. This is a good 34 // default but you can modify it to fine tune your 35 // setup (use the serial monitor to review what 36 // proximity values you observe). 37 38 39 // Create NeoPixel strip from above parameters. 40 Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); 41 42 // Create TCS color sensor object. 43 Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); 44 45 // Create VCNL sensor object (defined in this sketch). 46 // If you're using a VCNL4010 sensor use this line: 47 Adafruit_VCNL4010 vcnl = Adafruit_VCNL4010(); 48 // However if you're using a VCNL4000 sensor comment the above line and uncomment this one: 49 //Adafruit_VCNL4000 vcnl = Adafruit_VCNL4000(); 50 51 // Build a gamma correction table for better color accuracy. 52 // Borrowed from TCS library examples. 53 uint8_t gammatable[256]; 54 55 // Global variable to hold the current pixel color. Starts out red but will be 56 // changed by color sensor. 57 int r = 255; 58 int g = 0; 59 int b = 0; 60 61 62 void setup() { 63 Serial.begin(115200); 64 Serial.println(F("Adafruit 32u4 Feather Color Sensing Holiday Lights")); 65 66 // Setup TCS sensor LED pin as an output and turn off the LED. 67 pinMode(TCS_LED_PIN, OUTPUT); 68 digitalWrite(TCS_LED_PIN, LOW); 69 70 // Initialize NeoPixels. 71 strip.begin(); 72 strip.show(); 73 74 // Initialize TCS sensor library. 75 if (tcs.begin()) { 76 Serial.println("Found TCS sensor"); 77 } 78 else { 79 Serial.println("No TCS34725 found ... check your connections"); 80 while (1); 81 } 82 83 // Initialize VCNL sensor library. 84 if (vcnl.begin()) { 85 Serial.println("Found VNCL sensor"); 86 } 87 else { 88 Serial.println("No VNCL found ... check your connections"); 89 while (1); 90 } 91 92 // Generate gamma correction table. 93 // Taken from TCS color sensor examples. 94 for (int i=0; i<256; i++) { 95 float x = i; 96 x /= 255; 97 x = pow(x, 2.5); 98 x *= 255; 99 gammatable[i] = x; 100 } 101 } 102 103 void loop() { 104 // Animate pixels. 105 animatePixels(strip, r, g, b, 300); 106 107 // Grab VCNL proximity measurement. 108 uint16_t proximity = vcnl.readProximity(); 109 Serial.print("\t\tProximity = "); 110 Serial.println(proximity); 111 112 // Take a TCS color sensor reading if an object is near (proximity measurement is larger than threshold). 113 if (proximity > PROXIMITY_THRESHOLD) { 114 // First turn on the LED and wait a bit for a good reading. 115 digitalWrite(TCS_LED_PIN, HIGH); 116 delay(500); 117 // Grab TCS color sensor reading. 118 uint16_t raw_r, raw_g, raw_b, raw_c; 119 tcs.getRawData(&raw_r, &raw_g, &raw_b, &raw_c); 120 // Convert raw values to a value within 0...255, then run it through gamma correction curve. 121 r = gammatable[(int)(((float)raw_r / (float)raw_c)*255.0)]; 122 g = gammatable[(int)(((float)raw_g / (float)raw_c)*255.0)]; 123 b = gammatable[(int)(((float)raw_b / (float)raw_c)*255.0)]; 124 // Print color value. 125 Serial.print("R = "); 126 Serial.print(r); 127 Serial.print(" G = "); 128 Serial.print(g); 129 Serial.print(" B = "); 130 Serial.println(b); 131 // Turn off the LED. 132 digitalWrite(TCS_LED_PIN, LOW); 133 // Pause a bit to prevent constantly reading the color. 134 delay(1000); 135 } 136 137 // Small delay before looping. 138 delay(50); 139 } 140 141 void animatePixels(Adafruit_NeoPixel& strip, uint8_t r, uint8_t g, uint8_t b, int periodMS) { 142 // Animate the NeoPixels with a simple theater chase/marquee animation. 143 // Must provide a NeoPixel object, a color, and a period (in milliseconds) that controls how 144 // long an animation frame will last. 145 // First determine if it's an odd or even period. 146 int mode = millis()/periodMS % 2; 147 // Now light all the pixels and set odd and even pixels to different colors. 148 // By alternating the odd/even pixel colors they'll appear to march along the strip. 149 for (int i = 0; i < strip.numPixels(); ++i) { 150 if (i%2 == mode) { 151 strip.setPixelColor(i, r, g, b); // Full bright color. 152 } 153 else { 154 strip.setPixelColor(i, r/4, g/4, b/4); // Quarter intensity color. 155 } 156 } 157 strip.show(); 158 }