# Simple  do loop problem

**URL:** <https://forum.arduino.cc/t/simple-do-loop-problem/685060>\
**Category:** Programming\
**Created:** [December 1, 2020, 3:51am UTC](https://forum.arduino.cc/t/simple-do-loop-problem/685060 "2020-12-01T03:51:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattbaum](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@mattbaum](https://forum.arduino.cc/u/mattbaum)\
**Post date:** [December 1, 2020, 3:51am UTC](https://forum.arduino.cc/t/simple-do-loop-problem/685060/1 "2020-12-01T03:51:14Z")

</div>

I have a fairly complex program which is running OK, and in which I have several "do loops."  
However I have run into a problem in using one in a particular place.

I wish to have certain functions performed at power turn-on.  
Therefore I have included a switch, which will ground Pin 5 if pressed at turn-on, and the following code just after setup{

delay(50);  
pinMode( 5, INPUT);  
H = digitalRead(5);

/////////// below doesn't work /////////////////  
while ( H == 0 ) {  
delay(50); // just wait until switch is released  
}  
// Nor does this:  
do{  
delay(50);  
} while ( H == 0 );  
/////////////////////////////////////////////////

This hardware pin is shared with the LCD Display "enable" function, and after the above code I initialize the display and all works well from there on. (The display is inoperable during the initial turn-on, as it is initialized only after the initial switch test. )

A test on H later (after display initialization) shows that H follows the switch on/off correctly. However, I would like to have the program wait until the switch is released before proceeding, and I haven't figured out how to do that.

I don't understand why a simple "while" statement won't work. Any suggestions?

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [December 1, 2020, 3:59am UTC](https://forum.arduino.cc/t/simple-do-loop-problem/685060/2 "2020-12-01T03:59:16Z")

</div>

You must read the pin in the loop, otherwise the state of H never change.

For instance, in each loop, replace H with

```arduino
digitalRead (5);

```

> However, I would like to have the program wait until the switch is released before proceeding, and I haven't figured out how to do that.

Look at the[state change detection example](https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection). It shows how to sense the transitions (low to high or high to low).

---

<div class="post-metadata">

**Author:** ![mattbaum](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@mattbaum](https://forum.arduino.cc/u/mattbaum)\
**Post date:** [December 1, 2020, 2:52pm UTC](https://forum.arduino.cc/t/simple-do-loop-problem/685060/3 "2020-12-01T14:52:51Z")

</div>

Thanks a big heap!

I thought it must be something simple, but kept on thinking that being the first lines of the program had something to do with the problem.  
Sometimes we get blind-sided by the obvious!

Thanks again -  
Matt

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:52am UTC](https://forum.arduino.cc/t/simple-do-loop-problem/685060/4 "2021-05-06T00:52:38Z")

</div>


