Is a while-loop ending at some point?

I have a sketch that start with a while-loop to check a button to be pressed and if so continues with the program. The arduino is running 24/7 and everything works fine but it appears that at some point (a very long while) the while-loop ends, obviously although the button was not pressed.
Is it possible that the while loop ends after a very long time by itself? I read that there is this problem with millis sometimes when it counts up to a very large number and starts with 0 again. Probably my problem is somehow related with this problem?
Or is it a save fact that a while-loop never (in terms of really never!) ends until a true breaks it? can anyone help with this? thanks in advance

do {
  buttonState = digitalRead(switch);
} while (buttonState == HIGH);

Without code?

Not a hope.

just added the while code at the beginning...

No, it's not possible.

Maybe your input become HIGH for a little time due to disturb or noise or something else.

If that’s the only code executing up to that point, and there’s no interrupts, resets or watchdog events… then there’s noise on the button input.

At the beginning of what?
The code you didn't post?

OP meant in the first post.


to the question, this code

do {
  buttonState = digitalRead(switchPin);
} while (buttonState == HIGH);

will only exit if the condition is true (or the arduino gets rebooted for some external event / watchdog / ...)

how did you wire the button?

... and is the pin configured as INPUT_PULLUP?

Please post your actual code, this snippet is using a reserved word incorrectly:

   buttonState = digitalRead(switch);
exit status 1
expected primary-expression before 'switch'

No, while loops work, you can infer that buttonState wasn't always HIGH.

Also please post the entire sketch, the problem may be somewhere else. For instance does this while-loop ever get executed - can't tell from a snippet.

Thanks for all the quick help!

pin 13 is used for the switch and its configured as input_pullup.
thats why I wonder if it can even be possible to get "low" by any noise or something else.
I thought using it as input_pullup makes it very stable.

the while loop I posted is just to understand the concept as an example ("switch" is not the name it has in the actual sketch).
The while loop works fine with everything that comes after, so I thought it makes no sense to post all the code that comes after the initial while loop concept.

It just seems that for some reason the while loop ends without buttonevent and this only when it has been in the while-loop for a long time (appr. 2 days)

So I guess there seems to be a reason that gives Pin 13 a "low" occasionally.
could this be soldering-issues?

The in-built pullups are very weak, 50k or so, meaning little noise-immunity.

BTW why use pin 13 - it has the on-board LED pulling it down, so it could be problematical using a pull-up on it.

ok, I didn´t think about that.
so you think using another pin than 13 could help? or do you have another suggestion? I first used the pin w/o input_pullup and put a pulldown resistor, but that didn´t seem to work stable, so I decided to use it with the input_pullup.

if you pull the pin HIGH or LOW in the right way, there is no reason why this would not work (besides as mentioned before a possible watchdog biting you)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.