After posting about my ignorance regarding the fact that IF statements are not loops, I thought I would try my hand at a WHILE loop. My expectation of the attached sketch is that as long as digital pin 5 is high, the while loop is engaged and the display reads my text string from the void calibrate() loop. When I allow pin 5 to go low, I expect to break from the WHILE loop and clear the display as long as the input remains low.
What actually happens is that, while pin 5 is high, the while loop ruins but when pin 5 goes low, the display doesn't clear - in other words, I am not breaking out of my WHILE loop when the test condition changes.
If I reset while pin 5 is low, the display remains in the splash screen mode (not even clearing the display) and, upon pin 5 going high, the message in the void calibrate() loop is once again displayed.
Can someone please point out the error in my thinking and, if appropriate, direct me towards a more appropriate solution. I cannot imagine a simple input test could be so challenging.
If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's ok to add it as an attachment.
In this case your sketch is plenty short enough so there's no reason we should have to download a file just to view your code.
The problem is that value is never updated inside the while loop so as soon as you enter the while loop you're stuck there forever.
You could do this:
value=digitalRead(resetButton);
while ((value) == HIGH)
{
value=digitalRead(resetButton);
calibrate();
}
but since you don't use value anywhere else in your code this is more efficient:
while (digitalRead(resetButton) == HIGH)
{
calibrate();
}
Delta_G:
Yes but that has the effect of calling calibrate() once even if the pin read low. With the original while loop there calibrate() would never be called in that case.
True, that. Also you would normally see a sequence like this, where the read is not immediately repeated (more symmetrical (ABAB... not AABABA..)
value=digitalRead(resetButton);
while ((value) == HIGH)
{
calibrate();
value=digitalRead(resetButton);
}
I am grateful to this community for the support I have received. Got it! And, I finally understand "code tags" and will use them in the future. Thanks again to everyone who posted.
Another facet of a WHILE loop that nobody seems to have mentioned is that it blocks the Arduino until it finishes. Most of the time it is better to use IF and allow loop() to do the repetition.
In general I would only use FOR or WHILE if I know they will complete within several microseconds.
calibrate() suggests the function may involve an iterative process, which by definition won’t be ‘fast’ or static...
Combining the knowledge contributed above, it is safe to assume the use of interrupts should be invoked when they’re needed, not when when it looks hard to do something more appropriate!
aarg:
Such situations are a (basically the only) reason why the do-while construct exists...
Absolutely not true. There are plenty of reasons for having BOTH. A while loop will execute ZERO or more times. A do/while loop will execute ONE or more times, NEVER ZERO times. There are plenty of applications for both behaviors.
Regards,
Ray L.
RayLivingston:
Absolutely not true. There are plenty of reasons for having BOTH. A while loop will execute ZERO or more times. A do/while loop will execute ONE or more times, NEVER ZERO times. There are plenty of applications for both behaviors.
Regards,
Ray L.
Of course there are perfectly good reasons for having both. Although the rationale for each is on an equal standing, the frequency of use is very different. I think you would have to agree that the usage of while loops greatly outnumbers the usage of do-while.
aarg:
Of course there are perfectly good reasons for having both. Although the rationale for each is on an equal standing, the frequency of use is very different. I think you would have to agree that the usage of while loops greatly outnumbers the usage of do-while.