Controlling Adafruit Color Sensor / Light with Button

Hello,

I'm working with the existing Adafruit example code for the color sensor and trying to alter it to achieve two things:

  1. Have the sensor work as part of my working code that selects different effects with a button press case select
  2. Have the sensor light turn off when the board is powered up.

Currently the light turns on as soon as the board is powered up and doesn't turn off until the sensor function is called upon button press after other effects run. However when the sensor function is part of my button case select, it doesn't sense the color the first go round and I have to cycle through all of the effects (including color sensor) one time before the color sensor works on the second go round.

Here is a simplified version of the code with my case select. The color sensor is inside an if condition that only runs one time until the button is pressed which cycles to the next effect. I tried including the line "tcs.setInterrupt(true); // turn off LED" in the setup function but the light still stays on until the color sense function is called, and again, the sensor doesn't actually sense the color until the second time the function is called. Any thoughts on how to resolve this?

#include <Adafruit_NeoPixel.h>
#include <Wire.h> // Color Sensor
#include "Adafruit_TCS34725.h"  // Color Sensor

#define BUTTON_PIN   12    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT   4   // Number of LEDs in Strip

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); // Color Sensor

bool oldState = HIGH;
bool newState = HIGH;
int InLoopPress = 0;
int showType = 0;
int knightRainbowpress = 0;

// Color Sensor 
byte gammatable[256];
int sensed = 0;
float r, g, b;

// Main Setup
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.setBrightness(100);
  strip.show(); // Initialize all pixels to 'off'
  Serial.begin(9600);
}

// Main Loop
void loop() {
  // Start Effect
  startShow(showType);
}

// Checks if button was pressed and changes variable to go to next effect
void checkInLoopPress() {
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW && oldState == HIGH) {
      InLoopPress = 1; 
      showType++;
      // Change this number according to the number of effects in case switch
      if (showType > 2)
        showType=0; 
     oldState = newState;
     sensed = 0;
    }
}

// Selects which effect to start
void startShow(int i) {
  switch(i){
    case 0: rainbow(20);
            break;
    case 1: colorSense();
            colorWipe(strip.Color(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b]), 0);
            break;   
    case 2: rainbow(5);
            break;           
  }
}

// Effects //

// 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);

    // Check for inloop button press (include this to every effect loop)
    oldState = newState;
    checkInLoopPress();
    if (InLoopPress == 1) {
        InLoopPress = 0;
        break;
    }
  }
}

// Rainbow Fade
void rainbow(uint8_t wait) {
  uint16_t i, j;
  
  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);

    // Check for inloop button press (include this to every effect loop)
    oldState = newState;
    checkInLoopPress();
    if (InLoopPress == 1) {
        InLoopPress = 0;
        break;
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

// Begin Color Sensor Functions
void colorSense() {
  if (sensed == 0) {
    clearStrip();
    
    // 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;      
    }
    
    for (int i=0; i<3; i++){ //this sequence flashes the first pixel three times as a countdown to the color reading.
      strip.setPixelColor (0, strip.Color(75, 75, 75)); //white, but dimmer-- 255 for all three values makes it blinding!
      strip.show();
      delay(1000);
      strip.setPixelColor (0, strip.Color(0, 0, 0));
      strip.show();
      delay(500);
    }
    
    uint16_t clear, red, green, blue;
  
    tcs.setInterrupt(false);   // turn on LED
  
    delay(60);  // takes 50ms to read 
    
    tcs.getRawData(&red, &green, &blue, &clear);
  
    tcs.setInterrupt(true);  // turn off LED
  
    // Figure out some basic hex code for visualization
    uint32_t sum = red;
    sum += green;
    sum += blue;
    //sum += clear; // clear contains RGB already so no need to re-add it
    
    // float r, g, b;
    r = red; r /= sum;
    g = green; g /= sum;
    b = blue; b /= sum;
    r *= 256; g *= 256; b *= 256;
    sensed = 1;
  }
}
    tcs.setInterrupt(false);   // turn on LED

I find it incredibly hard to believe that that is what the code does.

    delay(60);  // takes 50ms to read

I KNOW that that is NOT that the code does.

I tried including the line "tcs.setInterrupt(true); // turn off LED" in the setup function but the light still stays on until the color sense function is called

So, obviously that comment is complete bullshit. Why do you think that calling the setInterrupt() method has ANYTHING to do with turning the LED on or off?