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);



  }  ```
  • 1
    don't use a while loop .... use an if block .... the loop() function in the sketch already loops – jsotola Apr 1 '20 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 aligned – Omasín Apr 1 '20 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 a flag variable – jsotola Apr 1 '20 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. – Omasín Apr 1 '20 at 0:57
  • 1
    just do something like this pseudo code if (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} – jsotola Apr 1 '20 at 1:18
1

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.

  • you are the man. It works now.I nearly started fro scratch, Thank you so much – Omasín Apr 1 '20 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.