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); } ```- 1don't use a
whileloop .... use anifblock .... theloop()function in the sketch already loopsjsotola– jsotola2020-04-01 00:47:38 +00:00CommentedApr 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 alignedOmasín– Omasín2020-04-01 00:51:59 +00:00CommentedApr 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 a
flagvariablejsotola– jsotola2020-04-01 00:56:09 +00:00CommentedApr 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.Omasín– Omasín2020-04-01 00:57:51 +00:00CommentedApr 1, 2020 at 0:57
- 1just 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– jsotola2020-04-01 01:18:31 +00:00CommentedApr 1, 2020 at 1:18
1 Answer1
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 downIf 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 downAnd 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 muchOmasín– Omasín2020-04-01 11:35:25 +00:00CommentedApr 1, 2020 at 11:35
Explore related questions
See similar questions with these tags.