0

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?

3
  • Firstly, when posting code select it and press ctrl+k to formatt it properly. Try putting digitalRead in for both conditions and also specify what digitalRead value it should be
    – Coder9390
    May 24, 2021 at 2:22
  • In while(digitalRead(home_limit1) || home_limit2), the program will not evaluate home_limit2 as long as digitalRead(home_limit1) return 1, is that what you want? do you really means while(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.
    – hcheung
    May 24, 2021 at 2:35
  • I have a slide with two limits on it. One at each end of the slide. I'm doing experiments with this slide. At one time during programming I want home_limit1 to operate as a home switch for the CW direction. In a different program I want the slide to run in the opposite direction, CCW and now home_limit2 will now act as a home switch. I just wanted to know if I could or 2 digital reads together as shown above. while(digitalRead(home_limit1) || home_limit2). This way I could change the logic below the statement and make the motor run to the counterclockwise Direction and trip home_limit2. May 25, 2021 at 6:25

1 Answer 1

2

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)) {
    ....
}
6
  • 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. May 25, 2021 at 6:33
  • 1
    @BillPatton You mean aside from the example I've already given you?
    – Majenko
    May 25, 2021 at 9:08
  • Yes please. How would I go about accomplishing this in your estimation. May 26, 2021 at 18:15
  • @BillPatton by using the code example that I already provided!
    – Majenko
    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
    May 26, 2021 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.