Color Sensor serial moniter always stays at 255

Hello everybody,I have a color sensor and when i put a rgb(red,green,blue) color in front, the led lights up perfectly,but when i look at my serial moniter the three numbers always stay at 255

arduino_buzzer_with_PIR_sensor.ino (1.01 KB)

int ledPin = 4;                // Pin LED is connected to
int piezoBuzzerPin = 3;     // Pin Piezo Buzzer is connected to
int pirSensorPin = 2;               // PIN PIR Sensor is connected to

int motionDetected = LOW;             // Start MotionDetected as low (No motion detected)


void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(pirSensorPin, INPUT);     // declare the PIR sensor as input
  pinMode(piezoBuzzerPin, OUTPUT); //declare buzzer as output
  Serial.begin(9600); //Set serial out if we want debugging
  delay(5000); //Allow time for the PIR Sensor to calibrate
}


void loop(){
  motionDetected = digitalRead(pirSensorPin);  // Read the PIR sensor
  if(motionDetected == HIGH) //If motion detected
  {            
    digitalWrite(ledPin, HIGH);
    analogWrite(piezoBuzzerPin, 200);
    delay(100);
    analogWrite(ledPin, LOW);
    analogWrite(piezoBuzzerPin, 25);
    delay(100);
  }
  digitalWrite(ledPin, LOW);
  digitalWrite(piezoBuzzerPin,LOW);
}

when i look at my serial moniter the three numbers always stay at 255

That's quite remarkable.

I am so sorry i sent the wrong file

arduino_color_sensor.ino (1.61 KB)

I already downloaded your code and posted it in code tags.
This time, it's down to you.

Please post the correct file using code tags, as described in the "How to use this forum" post.

ok here is my code using code tags:

int redPin=11;
int greenPin=10;
int bluePin=6;

int s2=7;
int s3=8;
int outPin = 4;

int rColorStrength;
int gColorStrength;
int bColorStrength;


unsigned int pulseWidth;

void setup() {
  Serial.begin(9600);

pinMode(redPin,OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(outPin, INPUT);
  

}

void loop() {
// RED
digitalWrite(s2,LOW);
digitalWrite(s3,LOW);

pulseWidth = pulseIn(outPin,LOW);

rColorStrength = (pulseWidth/400.) -1;

rColorStrength = (255- rColorStrength);
//     GREEN
digitalWrite(s2,HIGH);
digitalWrite(s3,HIGH);

pulseWidth = pulseIn(outPin,LOW);

gColorStrength = (pulseWidth/400.) -1;

gColorStrength = (255- gColorStrength);
// BLUE

digitalWrite(s2,LOW);
digitalWrite(s3,HIGH);

pulseWidth = pulseIn(outPin,LOW);

bColorStrength = (pulseWidth/400.) -1;


bColorStrength = (255- bColorStrength);

Serial.print(rColorStrength);
Serial.print(" , ");
Serial.print(gColorStrength);
Serial.print(" , ");
Serial.println(bColorStrength);
Serial.println("");

if(rColorStrength > bColorStrength && rColorStrength > gColorStrength){
  digitalWrite(redPin, HIGH);
  digitalWrite(bluePin,LOW);
  digitalWrite(greenPin,LOW);
}

if(gColorStrength > bColorStrength && gColorStrength > rColorStrength){
  digitalWrite(redPin, LOW);
  digitalWrite(bluePin,LOW);
  digitalWrite(greenPin,HIGH);
}

if(bColorStrength > rColorStrength && bColorStrength > gColorStrength){
  digitalWrite(redPin, LOW);
  digitalWrite(bluePin,HIGH);
  digitalWrite(greenPin,LOW);
}

delay(250);
}

No obvious problem with the code but I do wonder what color sensor you have.

Do you think that information could be useful?

It is a Tcs3200, the rgb led is lighting up to the good color but in the serial moniter it still says 255

So you made some significant changes from the reference code. Why?

/*__________________________________________________________
 
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
14CORE.com ARDUINO COLOR SENSOR TCS230/TCS3200
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
____________________________________________________________
*/
 
enum Colours {RED,GREEN,BLUE,CLEAR}; //Unumerate Colours RED,GREEN,BLUE and ClEAR
#define s2Pin 2 //S2 Colour Selection inputs define and connected to arduino pin 2
#define s3Pin 3 //S3 Colour Selection inputs define and connected to arduino pin 3
#define outPin 4  //Output define in Arduino pin 4
 
 
void setup()
{
  Serial.begin(9600); //Serial begin at baudrate 9600
  pinMode(s2Pin, OUTPUT); //Pin Mode s2Pin as Output
  pinMode(s3Pin, OUTPUT); //Pin Mode s2Pin as Output
  pinMode(outPin, INPUT); //Pin Mode s2Pin as Input
}
void loop()
{
 
  Serial.print(ReadColour(RED)); //Read colors value to serial communication 
  Serial.print(" : "); 
  Serial.print(ReadColour(GREEN));
  Serial.print(" : "); 
  Serial.print(ReadColour(BLUE));
  Serial.print(" : "); 
  Serial.println(ReadColour(CLEAR));
}
 
// Function reads colours and returns the a value betwenn 0 and 255 
byte ReadColour(byte Colour)
{  
  switch(Colour)
  {
    case RED:
      digitalWrite(s2Pin, LOW);
      digitalWrite(s3Pin, LOW);
      break;
     
    case GREEN:
      digitalWrite(s2Pin, HIGH);
      digitalWrite(s3Pin, HIGH);
      break;    
       
    case BLUE:
      digitalWrite(s2Pin, LOW);
      digitalWrite(s3Pin, HIGH);
      break;
       
    case CLEAR:
      digitalWrite(s2Pin, HIGH);
      digitalWrite(s3Pin, LOW);
      break; 
  }
     
  return map(pulseIn(outPin, HIGH), 30, 2500, 255, 0); //PWM map to 30,2500,255,0
}