Need help with limit switch programing

Hi:
What I am working on is driving a 12vdc gear motor using an Arduino Uno r3 and a Arduino Motor Shield.I am new to Arduino and I am having a problem getting this to do what I want it to do.
Basicly what it needs to do is: when a start button is pressed the motor should rotate clockwise from home position (limit swith A) aprox. 180 deg. until it hits (limit switch B).Then the motor brakes and motor rotates counterclockwise until it hits (limit switch A) and then it stops motor and waits until the start button is pressed again.

The way I have the limit switches wired up is on a breadboard with a 10k resistor from the +5vdc then a leed going to the proper pin on the Arduino then a leed going to the pin on limit switch marked (com) then a leed from ground to the pin on limit switch marked (no).All on same row of breadboard.

What happens is: I press the start button and the motor starts rotating it hits (limit switch B) and motor stops like it should.But it does not reverse direction.If I release the arm on (limit switch B) then motor starts up in reverse and runs until it hits (limit switch A) and it stops like it should.Then if I press start button it starts up again with same results.
IF anyone can see whats wrong it would be greatly appreciated.

const int buttonPin = 2;
const int swPin = 4;
const int swPinb = 7;
const int motor = 12;
const int brake = 9;





void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(swPin, INPUT);
  pinMode(swPinb, INPUT);
  pinMode(motor, OUTPUT);
  pinMode(brake, OUTPUT);

  Serial.begin(9600);


}

void loop() {
  int swbState = digitalRead(swPinb);   // Reads state of switch B
  int swState = digitalRead(swPin);   // Reads state of switch A
  int buttonState = digitalRead(buttonPin);    // Reads state of start button
  if (buttonState == 0 ) {    // Reads start button if pressed then continues with sketch
    digitalWrite(motor, HIGH);   // Sets motor to turn clockwise
    digitalWrite(brake, LOW);    // Turns off motor brake
    analogWrite(3, 255);   // Runs motor at max rpm
  }
  if(swState == 0){//   Turns motor clockwise until it hits switch A
    digitalWrite(brake, HIGH);    // Brakes motor 
    delay(1000);
    digitalWrite(motor, LOW);   // Sets motor to turn counterclockwise
    digitalWrite(brake, LOW);   // Turns off motor brake
    analogWrite(3, 123);    // Runs motor at half speed
  }
  if(swbState == 0){  // Motor runs ccw until it hits switch B
    digitalWrite(brake, HIGH);  // Stops motor
  }
  Serial.print("BUTTON: ");
  Serial.print(buttonState);
  Serial.print("swPin: ");
  Serial.print(swState);
  Serial.print("swbpin: ");
  Serial.println(swbState);

}

For this to work you need to know when the switch goes from high to low, not when it is low. The state change detection example in the IDE shows how to do this (Fike, Examples, Digital). So when the motor turns and switch A goes from high to low (the transition) you reverse the motor.

like this:

  int buttonState = digitalRead(buttonPin);    // Reads state of start button
  if (buttonState == LOW && lastButtonState == HIGH) 
  {    // Reads start button if pressed then continues with sketch
    digitalWrite(motor, HIGH);   // Sets motor to turn clockwise
    digitalWrite(brake, LOW);    // Turns off motor brake
    analogWrite(3, 255);   // Runs motor at max rpm
  }
  lastButtonState = buttonState;// lastButtonState is a global variable
  int swbState = digitalRead(swPinb);   // Reads state of switch B
  int swState = digitalRead(swPin);   // Reads state of switch A

This just annoys the hell out of me. If one set of names contains b to refer to the B switch, then the other set should have an a in the names to refer to the A switch.

Using the internal pullup resistors is so much simpler. Connect one leg to ground. Connect the other leg to the digital pin. Use pinMode(pinNumber, INPUT_PULLUP);. Then, HIGH is not pressed (like the top of the switch) and LOW is pressed (like the top of the switch).