Motor control limit switch problem

Working on a split swing gate using wheels hubbed to windshield motors. Walks the
gate open.

Anyways was working code for both gates at once and because its my first sketch, i was struggling. So I am just working on ONE gate.

When I have code in to check the limit switches, the motor only twitches.

When I take it out .. everything works good, just no limit switches.

Just need a little nudge, please

const int Request_Button = 4;      // this is the open and close button
const int Pot_Setting = A1;        // these are the pins we get the requested spd
const int Motor_Spd_Pin = 5;       // these are the pins where we send the speed
const int Gate_Direction = 7;      // these are the pins where we send dir info
const int Closed_Sensor = 11;      // these pins are from the limit switches
const int Open_Sensor = 2;
unsigned long Previous_Time = 0;   // the time var's are for timing the opening
unsigned long Current_Time = 0;    // and closing making sure it isn't stuck
int ledpin = 13;
int i = 0;
int Gate_Speed = 0;                // these var's will be assigned the value of the
int far_enough = 0;
// is sent to the PWM motor spd pins 5 & 6
String(Gate_Status) = "Open";
String(Requested_Action) = "Closeit";
bool Time_To_Slowdown = false;
bool Calc_Done = false;
//******************************

void setup()
{
  // note that motor_A&b_Spd_pins are not here because you don't have
  // to declare pinmode when using analogWrite. For reference pin 5 
  Serial.begin(9600);
  pinMode(Gate_Direction, OUTPUT);             // pin 7
  pinMode(Closed_Sensor, INPUT_PULLUP);        // pin 11
  pinMode(Open_Sensor, INPUT_PULLUP);          // pin 2
  pinMode(Request_Button, INPUT_PULLUP);       // pin 4
  pinMode(Pot_Setting, INPUT);                 // pin A1
  pinMode(ledpin, OUTPUT);

}

//****************************************************************

void loop()
{
  if (digitalRead(Request_Button) == 0)     //  Pin 4 grounded
  {
    Move_The_Gate(Requested_Action);
    Flip_Request();
  }
}
//**************************************************************


void Move_The_Gate(String request)
{
  Calc_Done = false;
  Time_To_Slowdown = false;                        // this is for a soft landing
  Previous_Time = millis();
  Gate_Speed = analogRead(Pot_Setting);           // pin A1 - get requested speed
  if (request == "Closeit")
  {
    digitalWrite(Gate_Direction, HIGH);
  }
  else
  {
    digitalWrite(Gate_Direction, LOW);
  }
  do
  {
    Current_Time = millis();
    if (Current_Time - Previous_Time >= 3000)      // After 3 secs, trigger slow down in motor
    {                                               
      Time_To_Slowdown = true;
    }
    if (Current_Time - Previous_Time >= 7000)      // after 7 secs turn motor off and return
    {
      analogWrite(Motor_Spd_Pin, 0);               
      return;
    }
    if (Time_To_Slowdown && !Calc_Done)            // the motor is slowed down to make soft landing
    {                                              // and we only want to change speed once
      Gate_Speed = Gate_Speed * .50;                
      Calc_Done = true;
    }
    analogWrite(Motor_Spd_Pin, Gate_Speed);        // finally the PWM can be sent to the motor

    ////// this next bit of code checks both
    ////// limit switches. sketch doesn't like it because
    /////  the motor only thinks about going (little jiggle)
    ////   with out this code everything works good but no limits
    
    far_enough = Chk_Limit();                      // this looks at all the limit switches 
    if (far_enough == 1)                           
    {                                              // if any limit switch is triggered  
      analogWrite(Motor_Spd_Pin, 0);               // turn motors off and return
      return;
    }
    /////
    /////
  }
  while (true);
}
//**********************************************************

int Chk_Limit()
{
  if (!digitalRead(Closed_Sensor) || !digitalRead(Open_Sensor))
  {
    return 1;
  }
  else;
  {
    return 0;
  }
}

//******** code has changed several times so some
//******** functions may not be used
bool Request_Is_Received()
{
  return !digitalRead(Request_Button); // pin 4
}
bool Gate_Is_Closed()
{
  return !digitalRead(Closed_Sensor);
}
bool Gate_Is_Open()
{
  return !digitalRead(Open_Sensor);
}

//**********  for debugging
void blinkit(int numtimes)
{
  for (int i = 0; i <= numtimes; i++)
  {
    digitalWrite(ledpin, HIGH);
    delay(200);
    digitalWrite(ledpin, LOW);
    delay(100);
  }

}
void Flip_Request()
{
  if (Requested_Action == "Openit")
  {
    Requested_Action = "Closeit";
    return;                             
  }
  if (Requested_Action == "Closeit");
  {
    Requested_Action = "Openit";
  }
}

/* **
  // for(int C = 0;C < 256; C++){                    this is some softstart code
  // analogWrite(MotorPin, C);                       I can use if needed
  // delay(10);                                      delay determinces how slow
*/

When the gate is closed and waiting for an 'open' command, the closed switch is activated isn't it? So how can it open if it chkLimit() says it is on a limit?

Aside: big-S strings as paramters are a very awfully bad idea. You only have two states so you only need a boolean: true and false can be open and close. If you have more states, an enum is the way to go.

thanks Morgan

Understand your confusion on the limit switch. the motor is actually blocked up off the ground in the middle of the cycle, so the limit switches are not activated during this test. Unless I push them with
my finger.

I only did the string thing to make my code super clear. Just to get it working.

Real quick question.

Do I have to have the analogWrite in the loop ... or can I call it once and it will keep going until I turn it off ?

When you use analogWrite() to a pin, that pin will output that level forever, until you tell it to do something different. It doesn't get offended if you keep telling it to do the same thing, so it's very common to just send the same value on every trip through loop().

Thank Morgan ..

This forum is almost toooo busy .. so im thankful I had you to answer my question ..

thanks again
Mike