problem with the code of color sensor

i have made this code to simply blink each led ie. red, green,blue and to read values of each color and display the values on serial monitor but after uploading the code only red and blue led blinks and green stays off :frowning: .
can someone help me with the code thanx in advance

int leds[] ={2,3,4};
int red ;
int green ;
int blue ;

void setup() {
pinMode(leds[0],OUTPUT);  
pinMode(leds[1],OUTPUT);
pinMode(leds[2],OUTPUT);
Serial.begin(9600);

}

void loop() {
  for (int i = 0; i<=2;i++){
 digitalWrite(leds[i],HIGH);
 delay(500);
 if(i=0){
   red = analogRead(A0);
       digitalWrite(leds[i],LOW);
 }
 else if(i=1){
   green = analogRead(A0);
       digitalWrite(leds[i],LOW);
  }
  else if(i=2){
    blue = analogRead(A0);
        digitalWrite(leds[i],LOW);
        
  }
   
  }
   Serial.print("red ");
   Serial.println(red);
   Serial.print("green ");
   Serial.println(green);
   Serial.print("blue ");
   Serial.println(blue);
       
}
 if(i=0){

Using the assignment operator (=) in an if statement is highly unusual. Using the equality operator (==) is far more normal.

thanx for your help now my code is working fine :slight_smile: .

Using the assignment operator (=) in an if statement is highly unusual

It is not that unusual to see here in code that does not do what is intended.

It is on my list of things to check in conditional code that is not working, along with that other old favourite, the semicolon after the if statement.

one way to tackle these if statements is to write

if ( i = 0 ) {

as

if (0 = i) {

As the compiler cannot assign to a const, it will fail.

Also a good tool to check your code is - http://cppcheck.sourceforge.net/