Help with my robot code

int timer;
int starttimer, stoptimer, stopDelay;
int state = 0;
int dir = 0;
boolean flag = false;

int LastStartP;
int LastStartN;

void setup() {}

void loop()
{
  int LimtP = digitalRead(1);
  int LimtN = digitalRead(2);
  int StartP = digitalRead(3);
  int StartN = digitalRead(4);

  if (StartP or StartN)
  {
    timer--;
    if (timer <= 0)
    {
      timer = 0;
    }
    else
      timer = starttimer;
  }

  if (state == 0)
  {
    // wait for command from button
    dir = 0;
    flag = 0;
    if (StartP == 1 and LastStartP == 0 and LimtP == 1)
    {
      dir = 1;
      state = 10;
    }
    else if (StartN == 1 and LastStartN == 0 and LimtN == 1)
    {
      dir = -1;
      state = 10;
    }
  }

  else if (state == 10)  //  got to limit
  {
    if (timer == 0)
    {
      //  check if (press is longer than starttimer
      flag = 1;
    }

    if ((StartP == 1 and LastStartP == 0) or (StartN == 1 and LastStartN == 0) or (flag == 1 and (LimtP == 0 or LimtN == 0)))
    {
      state = 100;
      stoptimer = stopDelay;
    }
    else if ((flag == 0 and (LimtP == 0 or LimtN == 0)))
    {
      state = 20;
      stoptimer = stopDelay;
      dir = -dir;
    }
  }


  else if (state == 20)   //  goto to the 2nd limit
  {
    stoptimer--;
    if (stoptimer <= 0)
    {
      stoptimer = 0;
    }
    if ((StartP == 1 and LastStartP == 0) or (StartN == 1 and LastStartN == 0) or ((LimtP == 0 or LimtN == 0) and stoptimer == 0))
    {
      state = 100;
      stoptimer = stopDelay;
    }
  }

  else if (state == 100)
  {
    // stopping
    stoptimer--;
    if (stoptimer <= 0)
    {
      stoptimer = 0;
      dir = 0;
      state = 0;
    }
  }
}