We are trying to control the direction of a stepper motor with a three-way switch. Switch 1 should move the stepper motor to the left, switch 2 should move the stepper motor to the right, and the middle switch should cause the stepper motor to stop. We also have two limit switches. When any of the limit switches get turned on, it should not allow the stepper motor to proceed in the direction that it was moving in and should stop. Here is an example for the first case, if switch 1 was turned on and the stepper was moving to the left but then the limit switch got turned on, then it should stop.
However, if the limit switch is turned on and the stepper motor is not moving, and the direction of the stepper motor got changed by the switch whilst the limit switch is being turned on, then it should allow the stepper motor to move in the opposite direction only despite the limit switch. Here is an example of the second case, if switch 1 was turned on and the stepper motor was moving to the left and then the limit switch got turned on, then it should stop. However, whilst the limit switch is turned on, the direction got changed to switch 2, then it should allow it to move in that direction only. For notice, we are using Arduino Rev 3 motor shield. Here is the code: `
#include <Stepper.h>
// Define number of steps per revolution:
const int stepsPerRevolution = 200;
// Define lead screw pitch in mm:
const float leadScrewPitch = 2.0;
// Define switch pin:
const int switch1 = 6;
const int switch2 = 5;
// Define limit switch pin:
const int limit1 = 7;
const int limit2 = 4;
// Give the motor control pins names:
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);
// Declare prevDirection variable to keep track of previous motor direction
void setup() {
// Set the PWM and brake pins so that the direction pins can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
pinMode(dirA, OUTPUT);
pinMode(dirB, OUTPUT);
// Set up limit switch pin:
pinMode(limit1, INPUT_PULLUP);
pinMode(limit2, INPUT_PULLUP);
// Set up switch pin:
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// 1. Check if both limit switches are high
if (digitalRead(limit1) == HIGH && digitalRead(limit2) == HIGH) {
// 2. Check limit switch 1
if (digitalRead(limit1) == HIGH) {
// Move the motor with full speed to the clockwise direction
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, LOW);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
digitalWrite(dirA, HIGH);
digitalWrite(dirB, LOW);
// Move until limit switch 1 is low
while (digitalRead(limit1) == HIGH) {
myStepper.step(1);
}
// Do one step in the opposite direction
myStepper.step(-1);
// Stop the motor
digitalWrite(brakeA, HIGH);
digitalWrite(brakeB, HIGH);
}
// 3. Check limit switch 2
if (digitalRead(limit2) == HIGH) {
// Move the motor with full speed to the counterclockwise direction
digitalWrite(pwmA, LOW);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
digitalWrite(dirA, LOW);
digitalWrite(dirB, HIGH);
// Move until limit switch 2 is low
while (digitalRead(limit2) == HIGH) {
myStepper.step(1);
}
// Do one step in the opposite direction
myStepper.step(-1);
// Stop the motor
digitalWrite(brakeA, HIGH);
digitalWrite(brakeB, HIGH);
}
}
}