Go Down

Topic: Stepper motor with limit switches does not work (Read 120 times) previous topic - next topic

Binary001

Hello,

I'm using an Arduino Mega for a set up in which I need to control a linear stage actuator to move left and right between two points equipped with limit switches. One point as home and the other is the end of travel. I use a third switch to activate the actuator. I wrote a sketch but when I execute it nothing happens. I don't know what is wrong.

The actuator is connected to a digital driver (DM542T) and set up worked with a generic sketch moving it back and forth. The problem arose when I added the limit switches to the set up. The following is the Sketch. Any help would be appreciated.

#include <AccelStepper.h>
#include <MultiStepper.h>

// Motor Control Parameters
#define MotorMaxSpeed 500 // This is the maximum speed for the motor movement in steps/second.  Note: speeds over 1000 are unreliable according to the AccelStepper Class Reference document.
#define MotorSpeed 100 // This is the maximum speed for the motor movement in steps/second.  Note: speeds over 1000 are unreliable according to the AccelStepper Class Reference document.
#define MotorAcceleration 500 // This is the motor accelration in steps/second^2

// Motor Position Parameters
#define MotorPositionAtBottom 1000 // This is the setpoint position of the motor at the lowest point
#define MotorPositionAtTop 0 // This is the setpoint position of the motor at the highest point
#define MotorPositionAtHome 100 // This is the setpoint position of the motor at the home point (slightly below the top point)

// Digital input pins
#define PinLimitSwitchBottom 22 // This is the analog input pin for the top movement limit switch
#define PinLimitSwitchTop 23 // This is the analog input pin for the bottom movement limit switch
#define PinStartButton 53 // This is the analog input pin for the motor start button press

// Digital output pins
#define PinMotorDirection 9 // This is the digital output pin for controlling the motor direction (CW)
#define PinMotorPulse 8 // This is the digital output pin for controlling the motor control pulses (CLK)

// Define a Accel stepper motor 1 for arduino
AccelStepper stepper(1, PinMotorPulse, PinMotorDirection);

// Stepper Motor Statuses
enum RepeatBlockTypes {MotorAtHome, MotorMovingDown, MotorMovingUp, MotorHoming}; // These are the possible states that the stepper motor can be in

// Prepare the Arduino Mega 2560 board
void setup()
{
  // Loop over the digital pins to set most pins as digital outputs
  for (int i = 2; i < 53; i++)
  {
    if ((i != PinLimitSwitchBottom) && (i != PinLimitSwitchTop) && (i != PinStartButton))
    {
      pinMode(i, OUTPUT); // Declare these pins as digital output
    }
  }

  // Declare these pins as digital inputs
  pinMode(PinLimitSwitchBottom, INPUT);
  pinMode(PinLimitSwitchTop, INPUT);
  pinMode(PinStartButton, INPUT);

  // Set up the motor settings
  stepper.setMaxSpeed(MotorMaxSpeed);
  stepper.setAcceleration(MotorAcceleration);
  stepper.moveTo(MotorPositionAtHome); // Start the motor moving to the home position, slightly below the top limit switch
  stepper.setSpeed(MotorSpeed);
}

// Loop indefinitely
void loop()
{
  // Read the digital input values
  int BottomLimitSwitchValue = digitalRead(PinLimitSwitchBottom); // Read the value of the bottom limit switch (LOW = the switch is open, HIGH = the switch is closed)
  int TopLimitSwitchValue = digitalRead(PinLimitSwitchTop); // Read the value of the top limit switch (LOW = the switch is open, HIGH = the switch is closed)
  int MotorStartButtonValue = digitalRead(PinStartButton); // Read the value of the motor start button (LOW = the button is pressed, HIGH = the button is not pressed)

  // Choose what to do based on the limit switch and start button press values

  // BottomLimitSwitchValue is a safety switch, reverse the motor to move up
  if (BottomLimitSwitchValue == HIGH) // HIGH means the bottom limit switch pin is reading the 5 V state (limit switch is pressed)
  {
    stepper.stop();
    stepper.moveTo(MotorPositionAtTop); // Tell the motor to go up to the top position, it should hit the top limit switch and then reverse to home
  }

  // TopLimitSwitchValue is a safety switch, reverse the motor to move down
  else if (TopLimitSwitchValue == HIGH) // HIGH means the top limit switch pin is reading the 5 V state (limit switch is pressed)
  {
    stepper.stop();
    stepper.setCurrentPosition(0); // Sets the position counter of the motor to zero in this home position
    stepper.setSpeed(MotorSpeed); // Setting the current position has a side effect of setting the speed to 0, so reset it.
    stepper.moveTo(MotorPositionAtHome); // Tell the motor to go down to the home position
  }

  // Only if no limit switch is pressed, check to see if the start button is pressed
  else if (MotorStartButtonValue == HIGH) // HIGH means the start button pin is reading the 5 V state (start button is pressed)
  {
    if (stepper.distanceToGo() == 0)
    {
      stepper.moveTo(MotorPositionAtBottom); // Tell the motor to go down to the bottom position
    }
  }
 
  // Tell the motor to continue running, if necessary, to its next set point location.  This run command must be called frequently in a loop to keep the motor running
  if (stepper.distanceToGo() != 0)
  {
    stepper.run();
  }
}


Robin2

@Binary001, you have told us there is a problem but you have not described what actually happens - give as much detail as possible.


To make it easy for people to help you please modify your post and use the code button </>


Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

Also please use the AutoFormat tool to indent your code for easier reading.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Binary001

The code loads onto the board but when the driver an the actuator are powered on nothing happens (no movement in the actuator and pressing the switches does not do anything). As I mentioned before the same setup without the limit switches (Arduino board, digital driver, power supply, and the linear actuator) work fine with a generic bounce sketch from AccelStepper librarie.

Robin2

The code loads onto the board  ...
Please see the second part of my Reply #2

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up