Arduino Code for Linear Slider and Controlling Linear Slider

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);
    }
  }
}

Do your limit switches have NC contacts? I think you could do this without connecting the limit switches to the Arduino. Just put them in series with the control switches.

What does the code do now? You don't seem to be using the switches yet.

Those two places you step in the opposite direction - is that motion sufficient to release the corresponding limit switch?

You need to "know" which way you are going. Knowing will allow you to consult only the relevant limit switch.

Use a variable and assign it values from an anonymous enum

enum {MOVING_LEFT, STOPPED, MOVING_RIGHT};

Use that variable as a state variable.

In you loop() function

  • read the switches and the limit switches
  • base on all the switches and the state, calculate new motor drive
  • actually tell the motor what to do, or not do.

Rinse and repeat. You may need to denounce the switches when you read them at the top of the loop. There are libraries for that. On the other hand it is nice to know how to do that "by hand", so to speak, even if you never do it yourself again.

See

Arduino finite state machine

and this elaboration on the IPO model I used in the code description.


HTH

a7

This type of switch will make the motor stop as you select the opposite direction. Maybe use a DPDT for direction and a NOSPST for run/stop. ** unless you want the middle switch to override any left/right switch

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.