can we do this with the while function while(digitalRead(button)==HIGH && digitalRead(pin1)==LOW)
or does it only apply for one condition ?
can we do this with the while function while(digitalRead(button)==HIGH && digitalRead(pin1)==LOW)
or does it only apply for one condition ?
does it only apply for one condition
You can have as many conditions as you like combined with && and ||
It is the combined result, true or false, that matters.
I would recommend separating the reading from the testing as it will make it easier to debug by allowing you to print the values that are read - something like this
buttonVal = digitalRead(buttonPin);
pin2Val = digitalRead(pin2);
while (buttonVal == HIGH && pin2Val == LOW) {
Note that I have changed pin1. It is generally not a good idea to use Pins 0 and 1 as they are used for Serial communications.
...R
Edited to change IF to WHILE in response to Reply #3
...R
@robin, you've changed a while to an if. You need to add some advice about how to integrate that logic into the original while.
aarg:
@robin, you've changed a while to an if. You need to add some advice about how to integrate that logic into the original while.
Thanks for pointing that out. I will correct the code in Reply #2
...R
buttonVal = digitalRead(buttonPin);
pin2Val = digitalRead(pin2);
while (buttonVal == HIGH && pin2Val == LOW) {
//do stuff here
...
buttonVal = digitalRead(buttonPin);
pin2Val = digitalRead(pin2);
}
Otherwise test result won't change
Closer to @Robin2's suggestion would be:
boolean buttonVal() { return digitalRead(buttonPin); }
boolean pin2Val { return digitalRead(pin2); }
while (buttonVal() == HIGH && pin2Val() == LOW)
{...}
Personally I don’t like the idea of boolean variables being compared with HIGH/LOW. I know that it works but what is wrong with using byte variables and HIGH/LOW ? To me it makes more sense and eliminates the need to remember whether true/false is the same as HIGH/LOW or is it the other way round ?
UKHeliBob:
Personally I don't like the idea of boolean variables being compared with HIGH/LOW. I know that it works but what is wrong with using byte variables and HIGH/LOW ? To me it makes more sense and eliminates the need to remember whether true/false is the same as HIGH/LOW or is it the other way round ?
Okay, then:
typedef boolean TTL;
TTL buttonVal() { return digitalRead(buttonPin); }
//...etc.
A byte can't be high or low...
To confuse things further digitalRead() returns an int.