0
int i=0;
while(i == 0){
 if(digitalRead(saveSwitch) == HIGH){
 Serial.print("New coordinates saved \n");
 lat1 = gps.location.lat();
 Serial.print("Latitude");
 Serial.println(lat1);
 lng1 = gps.location.lng();
 Serial.print("Longitude");
 Serial.println(lng1);
 i = 1;
 }
}

Why isn't leaving the while loop?

Dave X
2,35015 silver badges29 bronze badges
asked May 17, 2016 at 15:59
6
  • 2
    Is it ever triggering the if() and printing? If it is, what's outside this code? and does it dump it right back into the while(){}? Maybe put a Serial.println() after the while(){} to prove you aren't misdiagnosing the unseen code. Commented May 17, 2016 at 16:08
  • I've printed the value of i and it's 1 after my switch goes high. I even used i++; to see if it increases, but it only increases once and after that it just prints out the lat and lng every time the switch goes high. Edit: nothing outside the while loop gets executed after i is set to 1. Commented May 17, 2016 at 16:44
  • Then it is getting out of the while loop. Your problem is in the unseen, surrounding code. Commented May 17, 2016 at 16:55
  • All right, I'll look into the whole code and tell you what I find by tomorrow. Commented May 17, 2016 at 17:24
  • If you want i to maintain its state outside of the posted code, perhaps you should move the i=0; statement outside of its current scope. Commented May 17, 2016 at 17:27

2 Answers 2

0

Why isn't leaving the while loop?

There's a good chance that the code is leaving the while loop, repeatedly. However, if the code that's shown is in (eg) the loop() function, i will be reset to 0 each time the function runs, which will cause the while loop to start again.

As noted in Dave X's comment, you can move int i=0; to a higher scope. Moving it to file scope, for example, will allow it to retain its value through repeated function calls. For example, if the code shown is in loop(), you could put int i=0; just before void loop() {.

Note, having generically-named variables like i declared at a high level is a bad idea, a blunder, a programming faux pas. An alternative is to leave its declaration within loop() but add the static keyword so that i is allocated only once, rather than being reallocated and reinitialized at each execution of loop(). For example: static int i=0;.

answered May 17, 2016 at 19:37
0

Instead of setting i=1, you could use a 'break' command to break out of the while loop. That way you don't need 'i' at all, you can just do 'while(true)'

answered May 17, 2016 at 18:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy 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.