I am trying to get two limits to or on the same while statement. Can this be done?
while (digitalRead (home_limit)) {
This works for one(1) limit, But when I try using two(2) limits is does not work. I can't find the proper configuration. I tried this.
while (digitalRead (home_limit1) || (home_limit2)) {
I also tried other combinations with no luck. Does anyone know how to do an or (||) statement with a while digital read?
1 Answer 1
digitalRead()
is a function. It takes one parameter (the pin number) and returns the state of that pin. It's that return value that you want to OR with another. Which means you have to read the state of both pins, separately, and OR the results together:
while (digitalRead(home_limit1) || digitalRead(home_limit2)) {
....
}
-
Thank you, would it be possible to show me that code?Not sure how to use the return values of both to OR them in the program.Bill Patton– Bill Patton2021年05月25日 06:33:44 +00:00Commented May 25, 2021 at 6:33
-
1@BillPatton You mean aside from the example I've already given you?Majenko– Majenko2021年05月25日 09:08:15 +00:00Commented May 25, 2021 at 9:08
-
Yes please. How would I go about accomplishing this in your estimation.Bill Patton– Bill Patton2021年05月26日 18:15:05 +00:00Commented May 26, 2021 at 18:15
-
@BillPatton by using the code example that I already provided!Majenko– Majenko2021年05月26日 18:15:42 +00:00Commented May 26, 2021 at 18:15
-
@BillPatton: Majenko's line of code in this answer is all you need. digitalRead(home_limit1) returns the state of this pin: a zero or a one. Likewise digitalRead(home_limit2) returns the state of that pin: a zero or a one. The while loop will be executed until both digitalReads return a zero.PimV– PimV2021年05月26日 20:45:49 +00:00Commented May 26, 2021 at 20:45
while(digitalRead(home_limit1) || home_limit2)
, the program will not evaluatehome_limit2
as long asdigitalRead(home_limit1)
return 1, is that what you want? do you really meanswhile(digitalRead(home_limit1) && home_limit2)
? If this is not what you want, you will need to edit your post to explain in plain English of what you want first.