Help with code (Stepper Control)

Hi there, im having a bit of trouble with a project, using a Arduino Uno and an adafruit motor shield and a stepper motor.

here is my sketch so far

#include <AFMotor.h>

// Connect a stepper motor with 72 steps per revolution (5 degree)
// to motor port #1 (M1 and M2)
AF_Stepper motor(72, 1);

const int PotPin = A0;         // Change this if pot is connected to another pin
const int goPin = 2;           //the pin that the go switch is connected to
                               // this is switching digital pin 2 from High to Low
const int redPin = A2;         // the pin for the RED LED
const int bluePin = A4;        // the pin for the BLUE LED

int direction = FORWARD;       // Where we are going
int speed = 0;                 // How fast
int potVal = 0;                // Current potvalue
int goState = 0;              //current state of the goPin

void setup() {    
   pinMode (goPin, INPUT);
   pinMode (redPin, OUTPUT);
   pinMode (bluePin, OUTPUT);
   Serial.begin(9600);         // initialize serial communication at 9600 bits per second:
   while (!Serial) { ;         // wait for serial port to connect. Needed for Leonardo only

void loop() {
  potVal = analogRead(PotPin);   // read the input on the pot:
  goState = digitalRead (goPin); 
  if (potVal < 510)  
    direction = FORWARD;        // Smaller than 512 so we're going forward
    speed = map(potVal, 0, 511, 20, 1);  // Map value from pot-range to motorspeed-range
  else if (potVal > 513)
  {                             // Otherwise, we're going backward
    direction = BACKWARD;
                                // Map value from pot-range to motorspeed-range
    speed = map(potVal, 512, 1024, 1, 20);
                                // print out the variables:
  Serial.print("GoPin:  ");
  Serial.print(goState  ); 
  Serial.print(" POT Value: ");
  Serial.print(" direction: "); 
 if (direction==FORWARD)
    Serial.print(" Speed: ");
    Serial.println(speed);   // End of line, so also make it go a line

  // make motor go a step
  motor.setSpeed(speed);      // Set speed of motor
  delay(1);        // delay in between reads for stability


the stepper runs perfectly
I want to add a “go Switch” which is a single throw switch - the center pin is connected to pin 2 and the outer pins are +5v and Ground respectively,
So I don’t want my stepper to run untill pin 2 see’s high voltage and then I want it to stop when pin 2 see’s low voltage.

If someone could show me how to add this to my current sketch that would be super.

Anyone have any ideas, feel free to re write my code, what i'm after is a sketch that can allow me to run a stepper motor in both directions at varying speed based on the reading of the pot, and then I want to be able to stop the stepper by the use of a switch that turns on High and off Low.


I want to add a "go Switch" which is a single throw switch - the center pin is connected to pin 2 and the outer pins are +5v and Ground respectively

No you wire a switch up from the input to ground and either enable the internal pull up resistors or have an external one. You can then read the state of the switch with a digitalRead function. You use the result of the digital read either in an if statement or a while statement to change your program flow. For example

while(digitalRead(5) == HIGH) { }

Will effectively stall the program flow until the switch connected to pin 5 is made, that is the pin is connected to ground through the switch.