How to use limit switch to stop the motor

I want to use a limit switch to stop the motion of the motor in the case of closing via remote
and here is the code , i just want any answer , i have to make the project as soon as possible !

limiitswitchtest22.ino (5.61 KB)

i have to make the project as soon as possible !

Sounds like an assignment, should you be cheating like this?

No need for code just use this:-

limit switches.gif

You would typically use the limit switch rather than a fixed run time:

        motorA(1, 100);  //have motor A turn clockwise at 100% speed
        // delay(5000);  //let motor A run for 5 seconds
        while (digitalRead(limswitch) == LOW);  // Wait for limit switch to trigger (may need HIGH instead of LOW)
        motorA(3, 100);  //brake motor A with 100% braking power

If the same limit switch is used at both ends of travel you will need to wait for the limit switch to release before you look for it to activate again:

        motorA(1, 100);  //have motor A turn clockwise at 100% speed
        // delay(5000);  //let motor A run for 5 seconds
        while (digitalRead(limSwitch) == HIGH);  // Wait for limit switch to un-trigger (may need LOW instead of HIGH)
        delay(5);  // cheap de-bounce
        while (digitalRead(limSwitch) == LOW);  // Wait for limit switch to trigger (may need HIGH instead of LOW)
        motorA(3, 100);  //brake motor A with 100% braking power

These kind of problems are normally best solved using state machines like the one attached

limit switches state diagram.png

thank you man @johnwasser . It works with me , thanks alot