mercredi 19 avril 2017

Boolean value switching in Arduino code

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?

  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...

Aucun commentaire:

Enregistrer un commentaire