jeudi 20 avril 2017

Arduino(C++) first If always passing even if condition is false

I'm working with some code where I define something(a boolean), then go to an if statement to print to serial according to whichever boolean(s) are set to true. I tried printing out which bool is true in the defining stage and it was correct. Then when i went to the serial printing stage, the if() at the top overtakes (runs as if it was true) even if in the defining stage it said otherwise. Basically, the Serial output does not correspond to what the sensor actually sensed..
I've looked all around for different methods of using the sensor and even tried to make the if portion for where I print an if and only if but nothing worked.....
What could be going wrong?

//At the top, i set all Ds(BD, GD, RD) to false.

     // checkred pulse
      digitalWrite(s2, LOW);
      digitalWrite(s3, LOW);
      unsigned int RW = 255 - (pulseIn(OUTpin, LOW)/ 400 - 1);  // turns 
                                                               into 0-255
      delay(300);

  // =checkgreen pulse
  digitalWrite(s2,LOW);
  digitalWrite(s3,HIGH);
  unsigned int BW =  255 - (pulseIn(OUTpin, LOW)/  400 - 1);
  delay(300);

    // checkblue pulse
    digitalWrite(s2, HIGH);
digitalWrite( s3, HIGH);
  unsigned int GW = 255 - (pulseIn(OUTpin, LOW) / 400 - 1);
  delay(300);



  if (RW > BW && RW > GW){
    RD = true;    
    delay(1000);
  } else if  (GW > RW && GW > BW){
    GD = true;
    delay(1000);
  } else {
    BD = true;
    delay(1000);
  }

  if(RD){
    Serial.println("RED DETECTED");
    digitalWrite(5, LOW);
  } else if(GD){
    Serial.println("GREEN DETECTED");
    digitalWrite(5, LOW);
  } else if(BD){
    Serial.println("BLUE DETECTED");
    digitalWrite(5, LOW);

In this situation, if at first I got GD to be true,
"RED DETECTED" would be printed... That's what I mean by 'overtakes', even if it's false, the top one always runs as if it were to be true.

Aucun commentaire:

Enregistrer un commentaire