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