-1

This is the code but for some reason, when (distancia<100) the ("ALTO") still appears, and the same with the other while loop

while (distancia<200 && distancia>100)
{
    lcd.print("ALTO");
    delay(1000);
    lcd.clear();
    delay(1000);

  if(distancia<100)
  {
    break;
 }
}
 while(distancia<100)
  {
    lcd.print("INTRUSO EN");
    lcd.setCursor(0,1);
    lcd.print("LA PUERTA");
    tone(11,700,250);
    digitalWrite(8,HIGH);
    delay(500);
    lcd.clear();
    digitalWrite(8,LOW);
    delay(500);

    if(distancia>100);
    {
       break;
    }
}
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • `distancia` does not change at all in the code that is shown so the `break` is not going to cause any loop to exit. If either of the while loops were entered they would be infinite loops. I assume you set `distancia` to some value before this code and you expect it to change somehow in the loop but `c++` does not work that way. The assignment happened 1 time in the place where you set the value. – drescherjm May 07 '22 at 17:12
  • In both loops the break condition and loop condition have empty intersection. Even if the variable is somehow modified, this program may produce unwanted behavior. – Red.Wave May 07 '22 at 17:17

2 Answers2

0

this is your while loop:

while (distancia<200 && distancia>100)

and this is your if:

if(distancia<100);
{
   break;
}

a variable can't be greater and smaller than 100 at the same time so that if is basically useless

  • I think the OP assumes that `distancia` changes when some external hardware sensor on the Arduino changes. However it does not work that way. A variable will retain its same value until you change it in your code. For the value to change you need to read the value from the sensor again. – drescherjm May 07 '22 at 17:22
-1
while (distancia<200 && distancia>100)
{
    lcd.print("ALTO");
    ...
  if(distancia<100)
  {
    break;
  }
}
  1. while condition guarantees that distancia > 100, so your inner if is useless.
  2. Inside the loops you don't change distancia meaning the loops are infinite - seems like a bug to me.