Help! Switch doesn't work when control the motor

Hello, this is my first time using a motor shield and the design simply required the motor to turn clockwise and then anticlockwise when a switch is pressed.

When there is no switch, the motor works fine with the following code:

void setup() {

pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT);

pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
}
void loop() {
Serial.begin (9600);

analogWrite(9,250);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);

delay(1000);

digitalWrite(4, LOW);
digitalWrite(5, HIGH);

delay(1000);

analogWrite(9,0);

delay(1000);
}

The problem now is that it doesn’t work after adding in a push button.

int switchState = 0;

void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT);
}

void loop()
{

switchState = digitalRead (2);

if (switchState == HIGH)
{

analogWrite(9,230);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);

delay(1000);

analogWrite(9,230);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);

delay(1000);

analogWrite(9,0);

delay(1000);

}
}

Please help me to figure out how to make this thing work. Thanks.

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.
It appears you are using Vin and +5V.
Use +5 and GND.

LarryD:
To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.
It appears you are using Vin and +5V.
Use +5 and GND.

The complete code is in the post and connection is in the attachment photo shows the connection.
I’m using Vin and GND. I also tried 5V and GND before, the switch still didn’t work.

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

If you want help, please follow the rules. Otherwise members tend to be less helpful.

You can edit your original post to add code tags.

The photo only shows part of the circuit. A lot of wires go out of the photo.

Weedpharma

Is pin6 important? It is missing in your second code.

You don't stop before reversing? Hmmm, will try that with my car tomorrow. Leo..

Wawa: Is pin6 important? It is missing in your second code.

You don't stop before reversing? Hmmm, will try that with my car tomorrow. Leo..

Adding pin 6 code also didn't help. I modified the code based on the information from the motor shield supplier http://www.robot-r-us.com/vmchk/arduino-stuff/dual-channel-h-bridge-motor-shield-8a-22v.html