Pages: [1]   Go Down
Author Topic: while () as wait  (Read 741 times)
0 Members and 1 Guest are viewing this topic.
United States
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Indubitably
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
I'm using the "while ()" function as a sort of wait function for my buttons:
Code:
while (digitalRead(button1) == HIGH || digitalRead(button2) == HIGH || digitalRead(button3) == HIGH || digitalRead(button4) == HIGH) {
       delay(1);
     }

Unfortunately, the user is required to press all four buttons simultaneously to "shut off" the "wait" function. I only want the requirement to be one button. How might I do this?
Logged

fuzzball27 >>-->

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 223
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Change the ||  (or) to &&  (and).
Logged

0
Offline Offline
Sr. Member
****
Karma: 2
Posts: 360
I'm 15. I like making things. I like breaking things better.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If this is really the way you want to accomplish it, change the logic ORs ( || ) to logic ANDs ( && ) . This will execute the while loop while all of the pins tested are high, but if one of them goes low, it will break. Keep in mind that while waiting with delay, the processor cannot do anything, as this may be a problem later on in development.

Edit: Bobnova got it first. Stupid iphone...
Logged

Alice asked the Chesire Cat, who was sitting in a tree, "What road do I take?"
The cat asked, "Where do you want to go?"
"I don't know," Ali

United States
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Indubitably
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you guys!
Logged

fuzzball27 >>-->

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13834
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Shorter:
- leave out the delay(1); why wait ?
- HIGH == 1 == TRUE

==>

while (digitalRead(button1) && digitalRead(button2) && digitalRead(button3) && digitalRead(button4) ) ;
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use And!! That is '&&' instead of ||.
Logged

Pages: [1]   Go Up
Jump to: