nested if statement not working to call function containing further if statement

Hi there,

I am wondering if I am getting inconsistent behaviour as a result of having too many if statements across my functions.

I am using a rotating laser (rplidar) that continuously measures 2 things in a 2D plain around itself;

  1. the distance of the closest object it can detect (between 10 and 25 cms away from it)
  2. the angle at which the closest object is (between 0-360)

I am trying to call a function if the closest object is both within the range of 10-25 cms AND the angle of that object is between 0-180*. The 10-25 range works fine, but when I try to add the condition of between 0-180 degrees I cannot get it to consistently call or execute twinkle().

----- questions -----

I cannot work out why I cannot write the if statement like this.

Is it having trouble because twinkle() contains further segmentation using if/else?

Any clues as to why its inconsistent?

Thank you!

  if (lidar.getCurrentPoint().startBit)
    {
      if (minDistance < 100000)
      {//if the min distance is not smaller than 10m, proceede
      Serial.println("Start spinning ");
      Serial.println(angleAtMinDist);
           
           if((minDistance > 100 && minDistance < 250) && (angleAtMinDist < 180)) 
           {
              twinkle(); 
                Serial.print("ring breached play sound!");
                Serial.print("minDistance: ");
                Serial.print("minDistance: ");
                Serial.println(minDistance);
                minDistance = 100000;
                angleAtMinDist = 0;
           }
             else //(minDistance >505 && minDistance <100000) 
             { 
                noTone(PIEZO_PIN);
             }
        }
    }

this is the function that's called

 void twinkle()
 {
    if(angleAtMinDist < 60)
    {
        tone(PIEZO_PIN, NOTES[0]); //G
        Serial.print("playing G");
    } 
      else 
         if (angleAtMinDist > 61 && angleAtMinDist < 120) 
         {
            tone(PIEZO_PIN, NOTES[1]); //D
            Serial.print("playing D");
         } 
            else 
              if (angleAtMinDist >121 && angleAtMinDist < 179)
              {
                tone(PIEZO_PIN, NOTES[2]); //E
                Serial.print("playing E");
              }
   }

Where do you give values to angleAtMinDist,minDistance?

Do you have variables that is used for angleAtMaxDist,MaxDistance?

Can you post the complete code.

Any clues as to why its inconsistent?

It (whatever it is) is NOT inconsistent. It is your interpretation of the data that is wrong. Since you didn't share all of your code, or any serial output, it is up to you to determine why you are interpreting the data incorrectly.

There ARE some problems with your snippets:

      Serial.println("Start spinning ");
      Serial.println(angleAtMinDist);
          
           if((minDistance > 100 && minDistance < 250) && (angleAtMinDist < 180))

NOTHING in that code starts anything spinning.