Pages: [1]   Go Down
Author Topic: Limit switch control of stepper motor direction  (Read 2452 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I am trying to control a stepper using the stepper.h library on the Arduino UNO.
The only additional thing is the control of direction of rotation of the stepper with a limit switch.
For either of the two conditions (HIGH and LOW) with the if statement in the code, the stepper motor works as intended.
However, when the two conditions are put together, only the HIGH condition is recognized.
Does anybody have any suggestions.
The code is
Code:
#include <Stepper.h> //stepper library

const int stepback = 2; //motorpin
const int stepforw = 3; //motorpin
const int Switch1= 4;  // pin from limit switch
int one_rotation=6400; //number of steps required for easydriver at 1/8 step
//to complete one revolution.

int n=1;
int Steps=8; //change the number of steps to achieve resolution and speed
int rotation=one_rotation/Steps;
int j = 0;
int k;
int Distance = n*rotation; //number of revolution times step in a revolution
int RPM = 20;
Stepper mystepper= Stepper (rotation, stepforw, stepback);

void setup()
{
  Serial.begin(9600);
  pinMode(Switch1, INPUT);  //pin from limit switch
  digitalWrite(Switch1, LOW); //limit switch pin; not necessary
  digitalWrite(stepback,LOW); // stepper motor pin1
  digitalWrite(stepforw,LOW); // stepper motor pin2
  analogReference(DEFAULT);
  mystepper.setSpeed(RPM);  // set the speed of the stepper

}

void loop()
{
 
    if (digitalRead(Switch1)==LOW) //limit switch is off
    {
    mystepper.step(Steps);    //steps in forward direction
   
    }
        if (digitalRead(Switch1)==HIGH) //limit switch is on
        {
         mystepper.step(-Steps);   //reverse direction
       
         }
}

Logged

0
Offline Offline
Shannon Member
****
Karma: 214
Posts: 12406
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have only one limit switch?  How is that supposed to work?

Also your code is stateless, you need to have the current direction as a variable (which has state).

Currently it will go till it hits the switch and then oscillate as the switch goes on and off in rapid succession.

You need each limit switch to trigger a change in state in the correct direction - not one switch trying to
control both directions.
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you MarkT for your reply.
My understanding is that the direction of the current through the stepper is controlled by the sign of the 'step' in the stepper.h sketch. As I mentioned, the first if statement in the sketch, when the limit switch is LOW, turns the stepper in the CW. And when the limit switch is HIGH, it turns CCW without problem. But, when the two if statements are used in the same sketch, the first if statement is not recognized (LOW). Although I hear there is a load on the stepper, as I hear a humming sound.
Yes, you are right in mentioning the need for two limit switch for practical purpose. I was trying out the control of two logical states of rotation with two logical states of the limit switch (HIGH and LOW).
Any insight is greatly appreciated.
Logged

Pages: [1]   Go Up
Jump to: