Stoping rgb sensor at specific rgb value

Hello,
I trying to program the code to stop tsc34725 at R 47~48, G 89~90, B 99~100. What code should I use to achieve it?

#include <Wire.h>
#include <Adafruit_TCS34725.h>
#define redpin 3
#define greenpin 5
#define bluepin 6
#define commonAnode true
byte gammatable [256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS,TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
if (tcs.begin()){
} else {
Serial.println("NO TCS34725 found ... check your connections");
while (1);
}
#if defined (ARDUINO_ARCH_ESP32)
ledAttachPin (redpin, 1);
ledSetup(1, 12000, 8);
ledAttachPin (greenpin, 2);
ledSetup(2, 12000, 8);
ledAttachPin ( bluepin, 3);
ledSetup(3, 12000, 8);
#else
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
#endif
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *=255;
if (commonAnode) {
gammatable[i] = 255-x;
} else {
gammatable[i] = x;
}
}
}
void loop () {
float red,green,blue;
tcs.setInterrupt(false);
delay(1000);
tcs.getRGB(&red, &green, &blue);
tcs.setInterrupt(true);
Serial.println("R:\t"); Serial.print(int(red));
Serial.println("\tG:\t"); Serial.print(int(green));
Serial.println("\tB:\t"); Serial.print(int(blue));
Serial.print("\n");
#if defined(ARDUINO_ARCH_ESP32)
ledWrite(1, gammatable[(int)red]);
ledWrite(2, gammatable[(int)green]);
ledWrite(3, gammatable[(int)blue]);
#else
analogWrite(redpin, gammatable[(int)red]);
analogWrite(greenpin, gammatable[(int)green]);
analogWrite(bluepin, gammatable[(int)blue]);
#endif
}

Why does nobody read and follow the advice that they are directed to when joining the forum ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Help us help you.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Please post a schematic.

Please post an image of your project.

Please describe the problem better then you just did.

It sounds to me like you want to search for the RGB PWM settings that will produce a particular color as seen by the color sensor. I think this will get you close.

void loop() {
  static int redPWM = 0, greenPWM = 0, bluePWM = 0;

  float red, green, blue;
  tcs.getRGB(&red, &green, &blue);

  // R 47~48, G 89~90, B 99~100.
  if (red < 47.0)
    redPWM++;
  if (red > 48.0)
    redPWM--;

  if (green < 89.0)
    greenPWM++;
  if (green > 90.0)
    greenPWM--;

  if (blue < 99.0)
    bluePWM++;
  if (blue > 100.0)
    bluePWM--;

  Serial.print("Red: ");
  Serial.print(red);
  Serial.print(" PWM: ");
  Serial.print(redPWM);
  Serial.print("\tGreen: ");
  Serial.print(green);
  Serial.print(" PWM: ");
  Serial.print(greenPWM);
  Serial.print("\tBlue: ");
  Serial.print(blue);
  Serial.print(" PWM: ");
  Serial.println(bluePWM);

#if defined(ARDUINO_ARCH_ESP32)
  ledWrite(1, redPWM);
  ledWrite(2, greenPWM);
  ledWrite(3, bluePWM);
#else
  analogWrite(redpin, redPWM);
  analogWrite(greenpin, greenPWM);
  analogWrite(bluepin, bluePWM);
#endif
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.