0

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);  }  ```
askedApr 1, 2020 at 0:29
Omasín's user avatar
10
  • 1
    don't use awhile loop .... use anif block .... theloop() function in the sketch already loopsCommentedApr 1, 2020 at 0:47
  • Unfortunately, for delay reasons, i need to use the while loop. I want the system to delay at the end of the big loop after everything is alignedCommentedApr 1, 2020 at 0:51
  • why do you need a delay? .... if you really need a delay, then do a delay only when there is no motor position to be updated .... that is simple to accomplish with the use of aflag variableCommentedApr 1, 2020 at 0:56
  • The delay will allow the system to sleep for a set time to allow the sun to move a reasonable distance before tracking again.CommentedApr 1, 2020 at 0:57
  • 1
    just do something like this pseudo codeif (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}CommentedApr 1, 2020 at 1:18

1 Answer1

1

You're masking your outerdvert 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 donot 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.

answeredApr 1, 2020 at 10:48
Majenko's user avatar
1
  • you are the man. It works now.I nearly started fro scratch, Thank you so muchCommentedApr 1, 2020 at 11:35

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.