The concept of loop, while and do...while

Hello and thank you for reading this!

I have a program which drives a motor 2 ways with and IC and I had no problems with it up until I needed to add 2 switches to control its movement. This is the original program (segment of it):

  void setup()
{
  digitalWrite(motorpin1,LOW);   //direction up
  digitalWrite(motorpin2,HIGH);
  digitalWrite(pmwpin,HIGH);    //start
  
  delay(5000);  //move for 5 seconds
  
  digitalWrite(pmwpin,LOW);   //stop
  
  delay (50000);  //wait 50 seconds 
  
  digitalWrite(motorpin1,HIGH);  //change the motor direction
  digitalWrite(motorpin2,LOW);
  digitalWrite(pmwpin,HIGH);  //start
  
  delay(140);   //goes for 140 ms
  digitalWrite(pmwpin,LOW);    //it stops, end of the code
}

As you can see from my program it used to run in the setup segment but because from now I need to check always the state of the button it was moved there (switch1 and switch2; true means it's pushed

  void loop()
{
  digitalWrite(motorpin1,LOW);   //direction up
  digitalWrite(motorpin2,HIGH);
  digitalWrite(pmwpin,HIGH);    //start
  
  while ( switch1 == true) {
    digitalWrite(pwmpin, HIGH);  //it's turned on until the switch one's state equals to true
}
 
  
  digitalWrite(pmwpin,LOW);   //stop
  
  delay (50000);  //wait 50 seconds 
  
  digitalWrite(motorpin1,HIGH);  //change the motor direction
  digitalWrite(motorpin2,LOW);
  digitalWrite(pmwpin,HIGH);  //start
  
  while (switch2 == true) {
    digitalWrite(pwmpin, HIGH);  //it's turned on until the switch two's state equals to true
}
  digitalWrite(pmwpin,LOW);    //it stops, end of the code
}

as far as I know, according to the flowchart and the documentation it supposed to do what I've commented there ( move up until s1 pushed, changes direction, waits, then moves down until s2 is pushed then ends the program) but it moves pretty random instead.
I feel like I don't 100% got the concept of looping but I really tried to understand it. If someone could help me I'd appreciate it! Thank you!

A while loop will only run what is inside of it until the condition is no longer true.

 while ( switch1 == true) {
    digitalWrite(pwmpin, HIGH);  //it's turned on until the switch one's state equals to true
}

All this will do is write the pwmpin HIGH. Nothing inside this while loop will change switch1 to false. Nothing inside this while loop ever touches switch1. Nothing outside of this while loop will run until it is false. So this while loop will keep writing that pin HIGH over and over and over forever.

This doesn't look like a place for a while loop. The loop function is already repeating over and over. You can check the switch1 variable on each iteration and take the appropriate action with an if statement and the rest of the code can still run if you get that stupid 50 second delay out of there.

You also seem to be missing all the pinMode calls. Pins are by default inputs, so if you don't set them to outputs all you are doing is turning on the pullup resistors. That's probably not what you meant to be doing.

But what you stated in the first paragraph is what I’d like to achieve: to keep the variable pwmpin = high up until the switch1 switch gets pushed by the motor’s kinetic motion

I made a flowchart of my program, and because I need to test the switch1 's state over and over again I think I have a loop in the good place. And I need the 50 seconds delay because it’ll stay there for 50 seconds until it moves back to the next state.

pinMode calls are correct in the original program, I just cut out them from here because they are OK as the “original” program without the switches works properly, the switch1 and switch2 are setted to output at the beginning

geli:
But what you stated in the first paragraph is what I'd like to achieve: to keep the variable pwmpin = high up until the switch1 switch gets pushed by the motor's kinetic motion

You don't have to keep continually writing it HIGH. Write it HIGH once and it will stay HIGH until you write it LOW.

You don't need a 50 second delay. You need to wait for 50 seconds. There is a difference. delay stops everything and leaves you blind for 50 seconds. You could record millis() when you start and compare to it on each pass of loop to see if 50 seconds have passed before doing some other action. See the "Blink Without Delay" example with the IDE for some inspiration.

Your flow chart is just begging for a state machine approach. I see three states, looking for switch 1, changing direction and waiting, and looking for switch 2.

Can you clarify this a bit more? I feel like I can't constantly test the states of switch1 and switch2 with an if/else statement because I need to "stop" the program up until the switch changes and makes the program flow forward.

You don't need to stop the whole program. You just need to not do anything during that time.

pseudocode:

int state = 0;
int startTime = 0;


void loop(){
    
switch (state) {
    

case 0:
     if(switch 1 is made){
          state = 1;
          change direction
          startTime = millis();
     }
     break;

case 1: 
     if(millis() - startTime >= 50000){
          state = 2;
     }
     break;
case 2:
     if(switch 2 is made) {
         either go back to state 0 or go to a fourth state, whatever you need
     }
     else {
         do what you need to do until switch 2 is closed
     }
     break;
}
}

Now everything doesn't have to stop and wait. We could have other things in the loop outside this switch / case that would continue to run.

I get it, thank you.