Can two "While" conditions co-exist?

I build a function to control 2 Dc motors from two momentary 2 way switches:

void Motor (int MotNo, char DIR, int Force)
{ if (MotNo == 1)
  { if (DIR == 'U')
    { digitalWrite ( M1_A1, 0);
      digitalWrite ( M1_A2, 1);
    }
    if (DIR == 'D')
    { digitalWrite ( M1_A1, 1);
      digitalWrite ( M1_A2, 0);
    }
    analogWrite ( M1_PA , Force);
  }
  if (MotNo == 2)
  { if (DIR == 'U')
    { digitalWrite ( M2_B1, 0);
      digitalWrite ( M2_B2, 1);
    }
    if (DIR == 'D')
    { digitalWrite ( M2_B1, 1);
      digitalWrite ( M2_B2, 0);
    }
    analogWrite ( M2_PB , Force);
  }

}

Then in the loop I wrote the conditions:
(using "IF" did not work, motors start and stop with each refresh of the loop)

  while (digitalRead(M1_UP_Pin) == LOW)
  { Motor(1, 'U', 144); //Motor 1, going UP, at 144 PWM
  }
  while (digitalRead(M1_DN_Pin) == LOW)
  { Motor(1, 'D', 244); //Motor 1, going DOWN, at 244 PWM
  }

  while (digitalRead(M2_UP_Pin) == LOW)
  { Motor(2, 'U', 244); //Motor 1, going UP, at 144 PWM
  }
  while (digitalRead(M2_DN_Pin) == LOW)
  { Motor(2, 'D', 144); //Motor 1, going DOWN, at 244 PWM
  }

Everything works great unless I try to run the motors at the same time.
Whoever gets the signal first starts, while the other one does nothing.
What to do,
Thanks
Mitch

No. When control reaches the end of a while loop it then checks the condition for that while loop. If that condition is true then it goes back and repeats that while loop. Code execution can't be in two places at the same time in your code.

You need to use if statements here and be careful about the logic so that you don't turn your motors off every loop.

So why can't 2 while conditions be true a the same time? I can't believe all microcontrollers are limited to that....

I can have 20 "IF"s be true or not true in one loop...

They can both be true, but if one us true, the other will never be tested...
Take it or leave it..
While is blocking code (like delay)...

Take your right pointer finger.
Follow the program flow with your finger.
In how many while loops can you put your right pointer finger at the same time?

One Finger = One Core on the MCU.

If you do want to control several things "in parallel" don't block your code with while loops.

replace them with IF ... THEN ... ELSE

OK
I will get to work to make it conditional . I had no idea that WHILE is blocking....
The problem with if, is that is re-starting the motor with each loop,

I will try the bounce library, maybe that does better.

Trying to keep it simple.
Any other ideas?

  if (digitalRead(M1_UP_Pin) == LOW)
  { Motor(1, 'U', 144); //Motor 1, going UP, at 144 PWM
  }
  else if (digitalRead(M1_DN_Pin) == LOW)
  { Motor(1, 'D', 244); //Motor 1, going DOWN, at 244 PWM
  }
  else
  {
    Motor(1, 'D', 0);
  }

They can be...

But as long as you are in a loop you are stuck in the loop; and only the code inside that loop will execute.

If you write

while(1){}

you create an infinite do-nothing loop that you can't get out of, and your program will appear to freeze until you reset. :wink:

You can have nested loops (on loop inside another) or a sequence of loops.

I didn't really study your code but MAYBE what you want is AND or OR so the loop will run only when both conditions are true, or when either condition is true?

Seems logical to me that it is.

Perhaps there should be a command WhileUnless() ?

1 Like

It isn't a limitation of the microcontroller. It isn't a limitation at all. That's what a while loop is supposed to do. If you want something different then don't use a while loop. A while loop is for blocking.

2 Likes

To move on, you could focus on explaining how you want the motors and buttons to work

All clear now, While is for blocking.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.