Problem with TCS34725 Colour Sensor.

Good day,

I am writing a code that turns on a relay when I push a button. The next part of this code requires the use on a colour sensor( I have the TCS34725). I need to continuously read the ‘RED’(r in the variable) value of the colour sensor and based on that value, I have to activate the relay again.

The problem I am encountering is when I push the button, the colour sensor stops sensing the colour and is stuck on 1 value or r, and it just compares that value.

I would like the sensor to run continuously in the background to actively the red value so it cam compare that value. not the value red it stopped at.

This problem only occurs for the duration the relay is on and the which off. when this step is completed, the sensor starts again.

I think it is due to the conditional if statement which activates when the button is pressed.

Below is my code

#include <Wire.h>
#include <Adafruit_TCS34725.h>

// Define the variables:
uint16_t r, g, b, c, colorTemp, lux;

// Initialise with specific int time and gain values:
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);


#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Setting up Constants


const int Button1             = 25;
const int Red                 = 23;


void setup() {

 Serial.begin (9600);

 pinMode (Button1, INPUT);
 pinMode (Red   , OUTPUT);

  if (tcs.begin()) 
   {
   Serial.println("Found sensor");
   }
 else 
   {
   Serial.println("No TCS34725 found ... check your connections");
   while (1);
   }
 
}

void loop()

{
 int b1 = digitalRead (Button1);

 // Get the raw sensor data for the red, green, blue and clear photodiodes:
 tcs.getRawData(&r, &g, &b, &c);
 // Calculate the color temperature using all the sensor data:
 colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
 // Calculate lux using red, green and blue sensor data:
 lux = tcs.calculateLux(r, g, b);

 // Print the data to the Serial Monitor:
 Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
 Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
 Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
 Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
 Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
 Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
 Serial.println(" ");


if (b1 == LOW)
  {
  digitalWrite (Red         , HIGH);   // Keeps the relay off until the button is pressed
 }

 else
  {
   digitalWrite (Red, LOW);           // Opens the relay 
   delay(10000);
   digitalWrite (Red, HIGH);          // Closes the relay
   delay(10000);

 if (r > 10 && r < 20)

 {
    Serial.print ("Colour is RED");
   
 }

 else
 {
   digitalWrite (Red, LOW);           // Opens the relay 
   delay(10000);
   digitalWrite (Red, HIGH);          // Closes the relay
   delay(10000);
 }
  }


}

I’d get rid of the delays.

I’d also use code tags.

Please post your wiring diagram. I guess you make a common mistake when using button, see Button FAQ: common mistake - button does NOT work as expected. - Introductory Tutorials - Arduino Forum

And also, use timestamp instead of delay() see BlinkWithoutDelay