#include <Stepper.h>
#define STEPS_PER_MOTOR_REVOLUTION 32
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 480 //2048
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 8, 10, 9, 11);
int Steps2Take;
int dirpin=8;
int steppin=9;
int buttonApin=2;
int buttonBpin=3;
void setup()
{
pinMode(dirpin,OUTPUT);
pinMode(steppin,OUTPUT);
pinMode(buttonApin,INPUT);
pinMode(buttonBpin,INPUT);
}
void loop()
{
int valA=digitalRead(buttonApin);
int valB=digitalRead(buttonBpin);
if(valA==HIGH)
{
digitalWrite(steppin,HIGH);
digitalWrite(dirpin,LOW);
small_stepper.step(40000);
delay(1000);
}
if(valB==HIGH)
{
digitalWrite(steppin,HIGH);
digitalWrite(dirpin,LOW);
small_stepper.step(-40000);
delay(1000);
}
if((valA==LOW)&&(valB==LOW))
{
digitalWrite(steppin,HIGH);
digitalWrite(dirpin,LOW);
small_stepper.step(0);
}
}
Here I have written this code to rotate stepper motor. When the input on digital pin 2 becomebecomes high, then the motor should rotate clockwise. When the digital pin 3 becomebecomes high, it should rotate in a different direction. When the input on both pins the input is low, the motor should not move.
But with this code the motor is moving continuously even if you apply high input atto digital pins.
Please suggest how to fix this problem. For input signal I am taking signal from relays: if relay is in on condition, it should give a high signal and vice versa.