Hi Team
I am using Color checking program using Two TCS3475 Color sensors
Both sensors are connected I2C multiplexer
Both Sensors are working and detecting and reading are coming with my another program
But when following program lines activated gest stuck
int color_index_1 = detectColorIndex(red_adjusted_1, green_adjusted_1, blue_adjusted_1);
if (color_index_1 != -1) {
sensor1_counts[color_index_1]++;
}
above code is inside the ReadColor() function
Full Code
#include <Wire.h>
#include <Adafruit_TCS34725.h>
// Define the multiplexer address
#define TCAADDR 0x70
// Define pin for the calibration switch
#define CALIBRATION_SWITCH_PIN 2
// Initialize the sensors
Adafruit_TCS34725 tcs1 = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_1X);
Adafruit_TCS34725 tcs2 = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_1X);
// Color thresholds for detection (adjust according to your requirements)
#define COLOR_THRESHOLD 20
// Number of different colors
#define NUM_COLORS 3
// Define variables to store calibrated values
uint16_t red_calibrated_1 = 0;
uint16_t green_calibrated_1 = 0;
uint16_t blue_calibrated_1 = 0;
uint16_t clear_calibrated_1 = 0;
uint16_t red_calibrated_2 = 0;
uint16_t green_calibrated_2 = 0;
uint16_t blue_calibrated_2 = 0;
uint16_t clear_calibrated_2 = 0;
// Counter arrays for detected colors by each sensor
int sensor1_counts[NUM_COLORS] = {0};
int sensor2_counts[NUM_COLORS] = {0};
// Define colors for calibration
#define CALIBRATION_COLORS 3
enum CalibrationColor { RED, RED2 , GREEN, GREEN2, BLUE, BLUE2, STOP };
// Color detection function
bool detectColor(uint16_t red, uint16_t green, uint16_t blue);
// Current color being calibrated
CalibrationColor current_color = RED;
uint16_t STEP = 1;
void setup() {
Serial.begin(9600);
// Initialize the multiplexer
Wire.begin();
selectChannel(0);
// Initialize the sensors
if (!tcs1.begin() || !tcs2.begin()) {
Serial.println("Error initializing TCS34725 sensors!");
while (1);
}
// Initialize the calibration switch pin
pinMode(CALIBRATION_SWITCH_PIN, INPUT_PULLUP);
Serial.println("TCS34725 sensors found. Press the switch to calibrate each color.");
Serial.println("TCS34725 sensors found. Press the switch to calibrate each color.");
}
void loop() {
Serial.println("TCS34725 sensors found. Press the switch to calibrate each color.");
// Calibration();
Serial.println("Calibration completed.");
while (1) {
Serial.println("Coloer Reading");
ReadColor();
}
}
void ReadColor() {
Serial.println("red_1");
// // Select and read data from sensor 1
// selectChannel(0);
uint16_t red_1, green_1, blue_1, clear_1;
tcs1.getRawData(&red_1, &green_1, &blue_1, &clear_1);
Serial.println("red_1");
// // Adjust raw values using calibrated values for sensor 1
uint16_t red_adjusted_1 = red_1 * 255 / red_calibrated_1;
uint16_t green_adjusted_1 = green_1 * 255 / green_calibrated_1;
uint16_t blue_adjusted_1 = blue_1 * 255 / blue_calibrated_1;
uint16_t clear_adjusted_1 = clear_1 * 255 / clear_calibrated_1;
Serial.println(red_1);
// // Select and read data from sensor 2
selectChannel(1);
uint16_t red_2, green_2, blue_2, clear_2;
Serial.println("red_2");
delay(200);
// tcs2.getRawData(&red_2, &green_2, &blue_2, &clear_2);
// Adjust raw values using calibrated values for sensor 2
uint16_t red_adjusted_2 = red_2 * 255 / red_calibrated_2;
uint16_t green_adjusted_2 = green_2 * 255 / green_calibrated_2;
uint16_t blue_adjusted_2 = blue_2 * 255 / blue_calibrated_2;
uint16_t clear_adjusted_2 = clear_2 * 255 / clear_calibrated_2;
Serial.println("Test_3");
delay(400);
//
// // Check for color detection in sensor 1
int color_index_1 = detectColorIndex(red_adjusted_1, green_adjusted_1, blue_adjusted_1);
if (color_index_1 != -1) {
sensor1_counts[color_index_1]++;
}
Serial.println("Test_3");
delay(400);
//
// // Check for color detection in sensor 2
// int color_index_2 = detectColorIndex(red_adjusted_2, green_adjusted_2, blue_adjusted_2);
// if (color_index_2 != -1) {
// sensor2_counts[color_index_2]++;
// }
// Print the count of detected color boxes for each sensor
Serial.println("Sensor 1 Counts:");
for (int i = 0; i < NUM_COLORS; i++) {
Serial.print("Color ");
Serial.print(i);
Serial.print(": ");
Serial.println(sensor1_counts[i]);
}
Serial.println("Sensor 2 Counts:");
for (int i = 0; i < NUM_COLORS; i++) {
Serial.print("Color ");
Serial.print(i);
Serial.print(": ");
Serial.println(sensor2_counts[i]);
}
delay(1000); // Adjust delay according to your application
}
void Calibration() {
while (STEP < 7) {
// Check if the calibration switch is pressed
if (digitalRead(CALIBRATION_SWITCH_PIN) == LOW) {
// Calibrate the current color
calibrateCurrentColor();
// Move to the next color for calibration
switch (current_color) {
case RED:
current_color = RED2;
break;
case RED2:
current_color = GREEN;
break;
case GREEN:
current_color = GREEN2;
break;
case GREEN2:
current_color = BLUE;
break;
case BLUE:
current_color = BLUE2;
break;
case BLUE2:
current_color = STOP;
break;
}
STEP++;
//Serial.println("Calibration completed.");
delay(1000); // Delay to avoid multiple calibration triggers
}
// Add your color detection and counting logic here
delay(2000); // Adjust delay according to your application
}
}
void calibrateCurrentColor() {
// Read raw data from the sensor for the current color
uint16_t red, green, blue, clear;
readRawDataForColor(red, green, blue, clear);
// Store raw data as calibrated values based on the current color
switch (current_color) {
case RED:
red_calibrated_1 = red;
Serial.println("RED Left Calibration values:");
break;
case RED2:
red_calibrated_2 = red;
Serial.println("RED Right Calibration values:");
break;
case GREEN:
green_calibrated_1 = green;
Serial.println("GREEN Left Calibration values:");
break;
case GREEN2:
green_calibrated_2 = green;
Serial.println("GREEN2 Right Calibration values:");
break;
case BLUE:
green_calibrated_1 = blue;
Serial.println("BLUE Left Calibration values:");
break;
case BLUE2:
green_calibrated_2 = blue;
Serial.println("BLUE2 Right Calibration values:");
break;
}
Serial.print("Red: "); Serial.println(red);
Serial.print("Green: "); Serial.println(green);
Serial.print("Blue: "); Serial.println(blue);
Serial.print("Clear: "); Serial.println(clear);
}
void readRawDataForColor(uint16_t &red, uint16_t &green, uint16_t &blue, uint16_t &clear) {
// Select the appropriate sensor and channel based on the current color
switch (current_color) {
case RED:
selectChannel(0);
tcs1.getRawData(&red, &green, &blue, &clear);
break;
case RED2:
selectChannel(1);
tcs2.getRawData(&red, &green, &blue, &clear);
break;
case GREEN:
selectChannel(0);
tcs1.getRawData(&red, &green, &blue, &clear);
break;
case GREEN2:
selectChannel(1);
tcs1.getRawData(&red, &green, &blue, &clear);
break;
case BLUE:
selectChannel(0);
tcs1.getRawData(&red, &green, &blue, &clear);
break;
}
}
void selectChannel(uint8_t bus) {
Wire.beginTransmission(0x70);
Wire.write(1 << (bus + 2) ); // will be using 2-7 instead of 0-5 because of convience (placed better on the breadboard)
Wire.endTransmission();
}
int detectColorIndex(uint16_t red, uint16_t green, uint16_t blue) {
// Add color detection logic here based on RGB values and thresholds
if (red > 255 - COLOR_THRESHOLD && green < COLOR_THRESHOLD && blue < COLOR_THRESHOLD) {
return 0; // Detected red color
}
// Add more color detection conditions as needed
return -1; // No color detected
}
bool detectColor(uint16_t red, uint16_t green, uint16_t blue) {
// Add color detection logic here based on RGB values and thresholds
if (red > 255 - COLOR_THRESHOLD && green < COLOR_THRESHOLD && blue < COLOR_THRESHOLD) {
return true; // Detected red color
}
// Add more color detection conditions as needed
return false;
}
The program is getting stuck at the forth line of the ReadColor() function
Please advise
Thanks in advanced