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 > avgbot)
{ Serial.println("enter avgtop > avgbot");
Serial.println( "");
while (abs(dvert) > 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);
} ```
whileloop .... use anifblock .... theloop()function in the sketch already loopsflagvariableif (go left) {runMotor(left); moving = true;} else {moving = false;}...if (go right) {runMotor(right); moving = true;}....if (!moving) { pause 10 seconds} else {pause 1 second}