Loop not breaking for some reason on Arduino

I created a code that had the task of lighting up a 7 segment display to show a number when an object was detected by the sensor. Now, this worked well as it could display the number but could not go beyond 9. So I added another transistor to it. However, to even go beyond in a similar fashion as I could previously required me to create a loop, to simultaneously show different numbers on the two 7 segments. It worked but nothing would happen after moving further away until I reset it. I tried to add conditions to the loop but for some reason, it would not break the while loop. I know I did something wrong, I just want to see how I can correct this mess. I will only post a section of the code which is where the loop is. Everything else works apart from this troublesome while loop.

else if (distance == 5) {
  functionxyz();
  digitalWrite(11, HIGH);
  bool wan = true;
  while (wan == true) {
    digitalWrite(10, HIGH);
    delay(10);
    digitalWrite(11, LOW);
    zero();
    delay(10);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    five();
    delay(10);
    if (distance > 5) {
      wan = false;
    }
}

Thank you in advance

How can distance == 5 and at the same time > 5 ?

Hi, Welcome to the forum.

We need your complete code to make a better solution. Also before posting your code. Press CTRL +T buttons, it will format you code to make reading your code easier.

If your code is too big, you can attach it as an ino file to your post.

Thanks.. Tom... :)

guix: How can distance == 5 and at the same time > 5 ?

This is a section of my entire code, when the distance goes over 5 it's supposed to set wan to false and break the loop

You enter the while-loop when distance equals 5. Next you never update distance inside the while-loop so it will never be greater than 5.

Unless that is done in either zero() or five() or in an ISR. That's why we ask for complete code.

jojoku: This is a section of my entire code, when the distance goes over 5 it's supposed to set wan to false and break the loop

Distance does not change inside the loop.

TomGeorge:
Hi,
Welcome to the forum.

We need your complete code to make a better solution.
Also before posting your code. Press CTRL +T buttons, it will format you code to make reading your code easier.

If your code is too big, you can attach it as an ino file to your post.

Thanks… Tom… :slight_smile:

At the moment my code is somewhat incomplete but here:

#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor;

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, INPUT_PULLUP);
  digitalWrite(12, HIGH);
  Serial.begin(9600);
  Wire.begin();

  sensor.init();
  sensor.setTimeout(500);

  // Start continuous back-to-back mode (take readings as
  // fast as possible).  To use continuous timed mode
  // instead, provide a desired inter-measurement period in
  // ms (e.g. sensor.startContinuous(100)).
  sensor.startContinuous();
}

void zero()
{
  digitalWrite(2, LOW);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, HIGH);

  digitalWrite(6, LOW);

  digitalWrite(7, LOW);

  digitalWrite(8, LOW);

  digitalWrite(9, LOW);

}

void one()
{
  digitalWrite(2, HIGH);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, HIGH);

  digitalWrite(6, LOW);

  digitalWrite(7, HIGH);

  digitalWrite(8, HIGH);

  digitalWrite(9, HIGH);
}

void two()
{
  digitalWrite(2, LOW);

  digitalWrite(3, HIGH);

  digitalWrite(4, HIGH);

  digitalWrite(5, LOW);

  digitalWrite(6, LOW);

  digitalWrite(7, LOW);

  digitalWrite(8, HIGH);

  digitalWrite(9, LOW);


}

void three()
{
  digitalWrite(2, LOW);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, LOW);

  digitalWrite(6, LOW);

  digitalWrite(7, LOW);

  digitalWrite(8, HIGH);

  digitalWrite(9, HIGH);


}

void four()
{
  digitalWrite(2, HIGH);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, LOW);

  digitalWrite(6, LOW);

  digitalWrite(7, HIGH);

  digitalWrite(8, LOW);

  digitalWrite(9, HIGH);

}

void five()
{
  digitalWrite(2, LOW);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, LOW);

  digitalWrite(6, HIGH);

  digitalWrite(7, LOW);

  digitalWrite(8, LOW);

  digitalWrite(9, HIGH);

}

void six()
{
  digitalWrite(2, LOW);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, LOW);

  digitalWrite(6, HIGH);

  digitalWrite(7, LOW);

  digitalWrite(8, LOW);

  digitalWrite(9, LOW);
}

void seven()
{
  digitalWrite(2, HIGH);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, HIGH);

  digitalWrite(6, LOW);

  digitalWrite(7, LOW);

  digitalWrite(8, LOW);

  digitalWrite(9, HIGH);

}

void eight()
{
  digitalWrite(2, LOW);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, LOW);

  digitalWrite(6, LOW);

  digitalWrite(7, LOW);

  digitalWrite(8, LOW);

  digitalWrite(9, LOW);
}

void nine()
{
  digitalWrite(2, LOW);

  digitalWrite(3, HIGH);

  digitalWrite(4, LOW);

  digitalWrite(5, LOW);

  digitalWrite(6, LOW);

  digitalWrite(7, LOW);

  digitalWrite(8, LOW);

  digitalWrite(9, HIGH);
}

void loop()
{
  int distance = sensor.readRangeContinuousMillimeters() / 10;
  //int distance =sensor.startContinuous(100);

  //distance = distance;
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print("cm");
  if (sensor.timeoutOccurred()) {
    Serial.print(" TIMEOUT");
  }

  Serial.println();
  delay(100);
  if (distance == 1) {
    one();
    bool wan = true;
    digitalWrite(11, HIGH);
    while (wan == 1) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      one();
      delay(10);
      if (distance > 1) {
        wan = false;
      }
    }
  } else if (distance == 2) {
    two();
    int wan2 = 1;
    digitalWrite(11, HIGH);
    while (wan2 == 1) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      two();
      delay(10);
      if (distance > 2) {
        wan2 = wan2 - 1;
        printf(wan2);
      }
    }
  } else if (distance == 3) {
    three();
    bool wan = true;
    digitalWrite(11, HIGH);
    while (wan == true) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      three();
      delay(10);
      if (distance > 3) {
        wan = false;
      }
    }
  } else if (distance == 4) {
    four();
    bool wan = true;
    digitalWrite(11, HIGH);
    while (wan == true) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      four();
      delay(10);
      if (distance > 4) {
        wan = false;
      }
    }
  } else if (distance == 5) {
    five();
    digitalWrite(11, HIGH);
    bool wan = true;
    while (wan == true) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      five();
      delay(10);
      if (distance > 5) {
        break;
      }
    }
  } else if (distance == 6) {
    six();
    bool wan = true;
    digitalWrite(11, HIGH);
    while (wan == true) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      six();
      delay(10);
      if (distance > 6) {
        wan = false;
      }
    }
  } else if (distance == 7) {
    seven();
    bool wan = true;
    digitalWrite(11, HIGH);
    while (wan == 1) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      seven();
      delay(10);
      if (distance > 7) {
        wan = false;
      }
    }
  } else if (distance == 8) {
    eight();
    bool wan = true;
    digitalWrite(11, HIGH);
    while (wan == 1) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      eight();
      delay(10);
      if (distance > 8) {
        wan = false;
      }
    }
  } else if (distance == 9) {
    nine();
    bool wan = true;
    digitalWrite(11, HIGH);
    while (wan == 1) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      nine();
      delay(10);
      if (distance > 9) {
        wan = wan - 1;
      }
    }
  } else if (distance == 10) {
    bool wan = true;
    digitalWrite(11, HIGH);
    while (wan == 1) {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      one();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      zero();
      delay(10);
      if (distance > 10) {
        wan = false;
      }
    }
  } else if (distance == 11) {
    four();
  } else if (distance == 12) {
    four();
  } else if (distance == 13) {
    four();
  } else if (distance == 14) {
    four();
  } else if (distance == 15) {
    four();
  } else if (distance == 16) {
    four();
  } else if (distance == 17) {
    four();
  } else if (distance == 18) {
    four();
    }
  }
}

In all your while-loops, you will need to read the sensor to update the distance variable.

distance = sensor.readRangeContinuousMillimeters() / 10;

E.g.

    while (wan == 1) 
    {
      digitalWrite(10, HIGH);
      delay(10);
      digitalWrite(11, LOW);
      zero();
      delay(10);
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      one();
      delay(10);
      distance = sensor.readRangeContinuousMillimeters() / 10;
      if (distance > 1)
      {
        wan = false;
      }
    }

Thank you very much, I appreciate the help