Limit switches not stopping motors

Hello everyone I'm working on a project to basically build a pick and place system. At the end of each axes I have a limit switch to stop the pick and place moving when hit. I have wired up the limit switches correctly and I know they work correctly. The code should be pretty simple but for some reason it's not working to stop the motors when the switches are pressed. I am also interfacing Arduino with Matlab so I'm not sure if that would be causing any problems.

There's too much code to post the whole thing so I'll just show what I'm working with:

AccelStepper stm1(1,11,10); // stepper motor 1 declared

void setup() {
pinMode(6,INPUT); // limit switch 2
pinMode(5,INPUT); // limitswitch 1
}

if (stm==1) { // if stepper motor 1 is called from matlab

if ((digitalRead(5) ==1) && (digitalRead(6) ==1)){ //if switches not pressed motor runs where it has to as told my Matlab
stm1.moveTo(val2dir90); //Changed here

if ((stm1.distanceToGo() != 0)) {

stm1.runToNewPosition(val2*90);

stm1.run();
}
}
else if ((digitalRead(5) ==0) || (digitalRead(6) ==0)) { //if either switch is pressed
stm1.setSpeed(0);
}
}

So basically I'm telling the motors to move from Matlab which works fine but the code for the limit switches doesn't work.
Limit switches are hooked up to pins 5 and 6 so while its not pressed the motor should go where it has to go but if either one is pressed it should stop the motor.
I've tried many different things along these lines but all that happens is the motors just run and when I press the button nothing happens.
If anyone would be able to help me in any way I'd appreciate it so much thank you!

I suspect the problem is hardware in that you need to provide pull-up or pull-down resistors for the limit switch digital input pins. I you could draw out the complete wiring from the switches to the arduino pins this may become more clear.

Lefty

It's time to put some Serial.print("Here I am") markers in there too maybe, so you can see where the path through the code is actually taking you.

Basic button code. You should be able to load the below code and toggle the LED using the limit switch on pin 5.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}