Currently im running an 2 phase stepper motor connected to an Easy Stepper Driver with two limit switches on either ends of a rail. A platform moves up and down the rail until it hits one of the limit switches.
The problem i currently have is that when it reaches a limit switch the stepper motor stop as it should, then when i tell it to move backwards to the other limit switch, the stepper moves forward a few steps and then goes backwards. It does this all my functions. I found that reducing the difference between the moveTo() distance and the limit switch position reduces the number of steps that it takes forward. Is there a way of removing this permanently. Like to set the stepper motor has arrived at its destination when a limit switch is pressed.
//-------------------------INCLUDED LIBRARIES--------------------------//
//Make sure all librarys are installed on computer before running.
//Default save to this folder "C:\Users\[USER NAME]\Documents\Arduino\libraries\"
#define DEBUG //Comment out this to remove debug print out
#include <DebugUtils.h>
#include <AccelStepper.h>
//------------------------INITIALISATION OF VARIABLES--------------------//
// Define a stepper and the pins it will use
//name(using driver, step pin, direction pin)
AccelStepper stepper(1, 23, 25);
const int mS1Pin = 29; //MS1 pin on the stepper motor driver that controls step size
const int mS2Pin = 31; //MS2 pin on the stepper motor driver that controls step size
//Stepper motor enable PIN
const int stepperEnablePin = 27;
//Limit Switch Pin
const int limitSwitchR = 20; //PIN on Arduino to which limit switch rear connected to
const int limitSwitchF = 21; //PIN on Arduino to which limit switch front connected to
//LED on Audrino
const int led = 13; //PIN on Arduino of built in LED
//Hair hook pins
const int hook1 = 8; //input pin for hook 1.
//Variables
long maxDistance = 7670; //Number of microsteps from limit switch to limit switch, half step 2000steps
int i = 0;
int s4DistanceMM = 0; //Holds the user input for stage 4 distance of the hair to cut
int dcMotorCount = 10;
int frontLimitState = 0; //front limit switch state
int rearLimitState = 0; //rear limit switch state
int hairState = 0; //varaible to store the state of the hair hook (hair in hook or not)
int cutterHeadPos = 0; //position of the cutter head at the front of the device
long hairLength = 0; //stores the position of the cutter head for hair length calculations
float hairCal = 0; //variable for hair length calculations
float hairCalMM = 0; //calculated hair length in cm for the hair length function
long cutterPostion = 0;
long currentStep = 0;
//variables used in the interrupt service routine
volatile int stage = 0; //variable to set which event the user wants
//volatile boolean loopBreak = 0; //variable to break loop in the initialise function when limit switch pressed
//---------------------------SETUP------------------------------//
void setup()
{
//Setup of the serial communication
Serial.begin(9600); //make sure the value in the brackets matches the baud rate on the
//serial monitor and "No line ending" is selected
//interrupt setup for limit switchs
//attachInterrupt(2, motorStopISR, RISING); //Limit Switch 1 limitSwitchR
//attachInterrupt(3, motorStopISR, RISING); //Limit Switch 2 limitSwitchL
stepper.setMaxSpeed(12000); //Max Speed for stepper motor
stepper.setAcceleration(12000); //Max acceleration for stepper motor
//Limit Switch
pinMode(limitSwitchF, INPUT); //Limit switch front
pinMode(limitSwitchR, INPUT); //Limit switch rear
//pin13 LED
pinMode(led, OUTPUT);
//Stepper Motor Enable
pinMode(stepperEnablePin, OUTPUT); //Enable pin of stepper motor to turn on and off
pinMode(mS1Pin, OUTPUT); //Pins to control step size
pinMode(mS2Pin, OUTPUT);
//Hair pins
pinMode(hook1, INPUT); //SET PIN for hair hook 1 as an INPUT
//Run initialisation of device at the very beginning
//initialisation();
digitalWrite(mS1Pin, HIGH); //Writes both MS1 and MS2 pins to low to full step stepper motor
digitalWrite(mS2Pin, HIGH);
//Instructions
Serial.println("Instructions: Enter one of the following numbers ");
Serial.println("1: Initialise");
Serial.println("2: Grab hair");
Serial.println("3: Move Back");
Serial.println("4: 2 & 3 together");
stage = 0;
}
void loop()
{
//Opens the ability for serial terminal sending and recieving
// reads data only when you receive data:
if (Serial.available())
{
// read the incoming byte:
//converts the ASCII keyboard input through the serial monitor
//to an integer
stage = Serial.parseInt();
Serial.print("I received: ");
Serial.print(stage);
Serial.print('\n');
//time delay of half a second
delay(500);
}
//wait for next commard and turn off the stepper motor
if (stage == 0)
{
digitalWrite(stepperEnablePin, HIGH);
//Serial.print("Please enter the desired stage");
}
//initialise
//if stage variable is equal to 1 then this statement is true
if (stage == 1){
//Enables the stepper motor
digitalWrite(stepperEnablePin, LOW);
//reads the current state of the rear limit switch, LOW means not been activated
rearLimitState = digitalRead(limitSwitchR);
//checks to see if the rear limit switch isn't ready pressed. If not, then it's ok
// to move back (calls the initialise function)
if (rearLimitState == LOW){
//restores value of the loop break variable when limit switch pressed
//calls the initialise fucntion
initialise();
}
//reset the postion counter
positionReset();
//If already at rear limit, you want it to return to waiting mode.
stage = 0;
}
//move to the font of the device
if (stage == 2)
{
//Enables the stepper motor
digitalWrite(stepperEnablePin, LOW);
delay(500);
grabHair();
stage = 0;
}
if (stage == 3)
{
//Enables the stepper motor
digitalWrite(stepperEnablePin, LOW);
delay(500);
moveBack();
stage = 0;
}
}
//------------------------POSITION RESET-------------------------//
//Resets the stepper counter so current position is step 0. Called in the
//initialise function
void positionReset(){
DEBUG_PRINT("Inside positionRest Function");
stepper.setCurrentPosition(0);
}
//------------------------INITIALISATION FUNCTION--------------------//
//Initialise function takes the cutter head to the back of the device
//then sets the stepper counter to zero.
void initialise(){
stepper.moveTo(-maxDistance);
//If the speed and the acceleration of the stepper was to be slowed during intialise
//stepper.setMaxSpeed(10000);
//stepper.setAcceleration(5000);
DEBUG_PRINT("Entering initialise function");
//Moves the cutter head to the rear of the device so it hits the limit switch
stepper.moveTo(-maxDistance);
//While the stepper hasnt reach the distance, move stepper at full speed
while (rearLimitState == LOW)
//if limit switch is pressed, the cutter has reached the end of the device
{
stepper.run();
rearLimitState = digitalRead(limitSwitchR);
}
DEBUG_PRINT("loop break started");
//Stop the stepper motor
stepper.stop();
DEBUG_PRINT(stepper.distanceToGo());
stage = 0;
DEBUG_PRINT("Out of initialise function");
}
//
void grabHair(){
DEBUG_PRINT("Grab Hair");
//Moves the cutter head to the rear of the device so it hits the limit switch
stepper.moveTo(maxDistance);
frontLimitState = digitalRead(limitSwitchF);
stepper.moveTo(maxDistance);
while (frontLimitState == LOW)
//if limit switch is pressed, the cutter has reached the end of the device
{
stepper.run();
frontLimitState = digitalRead(limitSwitchF);
}
DEBUG_PRINT("Hit front limit switch");
//Stop the stepper motor
stepper.stop();
DEBUG_PRINT(stepper.distanceToGo());
}
void moveBack(){
stepper.moveTo(0);
DEBUG_PRINT(stepper.distanceToGo());
DEBUG_PRINT("Move Back");
i = 0;
cutterHeadPos = stepper.currentPosition();
rearLimitState = digitalRead(limitSwitchR);
stepper.moveTo(0);
DEBUG_PRINT(stepper.distanceToGo());
while (rearLimitState == LOW)
{
if (i == 0){
hairState = digitalRead(hook1);
if (hairState == HIGH){
hairLength = stepper.currentPosition();
DEBUG_PRINT("hair length recorded");
i = 1;
}
}
stepper.run();
rearLimitState = digitalRead(limitSwitchR);
}
stepper.stop();
if (i == 0)
{
Serial.println("Hair too long");
return;
}
}