How to use SPDT in conjuntion with Two Stepper Movements

Setup: Arduino UNO + DC Motor Drivers Shield L293D
Stepper Motor = Nema 17
Laptop to Upload code: Windows 7 (to program the UNO)

My Application: Two modes required to be operated via one SPDT switch for the same Stepper (Nema17)

(1) On one position of the SPDT, Operation “A” as per the below code for the Stepper Motor, should run in a defined sequence in an infinite loop until such time when the SPDT is switched to other position.

(2) On the other Position of SPDT, Operation “B” the same stepper motor should now run continuously at a given speed of 100 in an infinite loop until SPDT is flipped again.

While I have managed to successfully write my first sketch for “A” , being very new to this, I am struggling with how to incorporate the two codes with respect to SPDT switch.

Operation “A” that I have written given below, while Operation “B” is directly from Arduino Examples for constant speed.

(A) Operation “A”:

// Anjeel’s First Sketch
#include <AFMotor.h>
// Connect motor to port #2 (M3 and M4)

AF_Stepper motor1(48, 2);

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Stepper test!”);
motor1.setSpeed(50); // 50 Step Speed corresponding rpm
}
void loop() {
{
Serial.println(“Double coil steps”);
motor1.step(100, FORWARD, DOUBLE);
delay(1000); // add delay
}
{
motor1.step(100, BACKWARD, DOUBLE);
delay(1000); // add delay
}

{
Serial.println(“Double coil steps”);
motor1.step(50, FORWARD, DOUBLE);
delay(500); // add delay
}
{
motor1.step(100, BACKWARD, DOUBLE);
delay(500); // add delay
}

{
motor1.step(50, BACKWARD, DOUBLE);
delay(1000); // add delay
}

{
motor1.step(100, BACKWARD, DOUBLE);
delay(1000); // add delay
}
}

(B) Operation “B” (This is adapted from examples library)

// ConstantSpeed.pde
// -- mode: C++ --
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
// Requires the AFMotor library (GitHub - adafruit/Adafruit-Motor-Shield-library: Adafruit Motor shield V1 firmware with basic Microstepping support. Works with all Arduinos and the Mega)
// And AccelStepper with AFMotor support (GitHub - adafruit/AccelStepper: A small fork of AccelStepper v1.3 with AF_motor (Adafruit motor shield) support!)
// Public domain!

#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(200, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {
motor1.onestep(FORWARD, SINGLE);
}
void backwardstep() {
motor1.onestep(BACKWARD, SINGLE);
}

AccelStepper stepper(forwardstep, backwardstep); // use functions to step

void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Stepper test!”);

stepper.setSpeed(50);
}

void loop()
{
stepper.runSpeed();
}


Both above need to work with SPDT. Which ping to attach the switch to the motor Shield and how to write If, else statement.

Kindly assist.

You need to put the code for the two different stepper movements into two separate functions - let's call them operationA() and operationB().

The your code in loop() will be something like

void loop() {
   switchState = digitalRead(switchPin) {
   if (switchState == HIGH) {
      operationA();
   }
   else {
      operationB();
   }
}

Have a look at Planning and Implementing a Program

...R