Stuck in While loop! I am using 4 LDR To track the sun for solar panels. Not using servos, but linear actuators(a little irrelevant but just to give scope)

my code gets stuck in this while loop as soon as it enters it, even tho the conditions are changed to break the loop. I want to leave the motors running in one direction until the "dvert"=Vertical difference is less then "tol"=tolerance. there are 4 of these loops. there is an if condition before each. so no adjustment is made if its already in tolerance.

Segment of code

  if (avgtop &gt; avgbot)
  {  Serial.println("enter avgtop &gt; avgbot");
    Serial.println( "");

   while (abs(dvert) &gt; tol)
     {
      Serial.println("Enter While loop vert up....");
      Serial.println("Moving Vertical Axis begin...Moving UP  ");

                   digitalWrite(vrelay1, HIGH);              // Setting frame down
                   digitalWrite(vrelay2, LOW);              // Setting frame down  
      delay(200);
      int topl = analogRead(ldrtopl);
      int topr = analogRead(ldrtopr);
      int botl = analogRead(ldrbotl);
      int botr = analogRead(ldrbotr);
      int avgtop = (topl + topr) / 2; //average of top LDRs
      int avgbot = (botl + botr) / 2; //average of bottom LDRs
      int dvert = avgtop - avgbot; // check the diffirence of up and down  

      Serial.print("NEW DVERT:"  );
      Serial.println(dvert);



  }  ```
You're masking your outer dvert variable (and potentially others that you want to keep) with local variables inside the loop:

  int dvert = avgtop - avgbot; // check the diffirence of up and down  


If you're just assigning a new value to an existing variable you do not want to declare a new variable!

Instead it should be just:

  dvert = avgtop - avgbot; // check the diffirence of up and down  


And the same for anything else inside the loop that you want to assign to a variable that you have created outside the loop.