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
#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
}
}