user29162: #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 becomes high, then the motor should rotate clockwise. When the digital pin 3 becomes high, it should rotate in a different direction. When the input on both pins is low, the motor should not move.
But with this code the motor is moving continuously even if you apply high input to 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.

user39122: I think its better to use count function .. you need to test yor stepepr motor first ..here the code to test .. i hope its will help :)
#define IN1  50
#define IN2  51
#define IN3  52
#define IN4  53
int Steps = 4096; //4096(full rotation 360 degree )
int cstep = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 

 Serial.println("Enter character for control option:");
 Serial.println("h : anticlockwise"); 
 Serial.println("i : clockwise");
 }

 void loop()
{

   if (Serial.available()>0)
   switch(Serial.read())
 {
   case 'h':
   Serial.println("anticlockwise");
   for(int x=0;x<Steps;x++)
{

     step1();
     delayMicroseconds(1000);
}
  break;

   case 'i':
   Serial.println("clockwise");
     for(int x=0;x<Steps;x++)
   {  

       step2();
       delayMicroseconds(900);
   }

   //delay(1000);
    break;

  }



  }

   void step1()
  {
    //stepp
    switch(cstep)
 {
   case 0:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
 break; 
 case 1:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, HIGH);
 break; 
 case 2:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 break; 
 case 3:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 break; 
 case 4:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 5:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 6:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 7:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
 break; 
 default:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 }

 cstep=cstep+1;
 if(cstep==8)
  {cstep=0;}
 }


  void step2() 
{
//stepp
switch(cstep)
{
 case 0:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
 break; 
 case 1:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
 break; 
 case 2:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 3:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 4:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 5:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 break; 
 case 6:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 break; 
 case 7:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, HIGH);
 break; 
 default:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 

 }

  cstep=cstep+1;
 if(cstep==8)
 {
  cstep=0;
  //Serial.println("habis");

  }
 }


29162: <pre><code>#include &lt;Stepper.h&gt;
#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)&amp;&amp;(valB==LOW)) {
    digitalWrite(steppin,HIGH);
    digitalWrite(dirpin,LOW);
    small_stepper.step(0);
  }
}
</code></pre>

<p>Here I have written this code to rotate stepper motor. When the input on digital pin 2 becomes high, then the motor should rotate clockwise. When the digital pin 3 becomes high, it should rotate in a different direction. When the input on both pins is low, the motor should not move.</p>

<p>But with this code the motor is moving continuously even if you apply high input to digital pins.</p>

<p>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.</p>
 39122: <p>I think its better to use count function .. you need to test yor stepepr motor first ..here the code to test .. i hope its will help :)</p>

<pre><code>#define IN1  50
#define IN2  51
#define IN3  52
#define IN4  53
int Steps = 4096; //4096(full rotation 360 degree )
int cstep = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 

 Serial.println("Enter character for control option:");
 Serial.println("h : anticlockwise"); 
 Serial.println("i : clockwise");
 }

 void loop()
{

   if (Serial.available()&gt;0)
   switch(Serial.read())
 {
   case 'h':
   Serial.println("anticlockwise");
   for(int x=0;x&lt;Steps;x++)
{

     step1();
     delayMicroseconds(1000);
}
  break;

   case 'i':
   Serial.println("clockwise");
     for(int x=0;x&lt;Steps;x++)
   {  

       step2();
       delayMicroseconds(900);
   }

   //delay(1000);
    break;

  }



  }

   void step1()
  {
    //stepp
    switch(cstep)
 {
   case 0:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
 break; 
 case 1:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, HIGH);
 break; 
 case 2:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 break; 
 case 3:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 break; 
 case 4:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 5:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 6:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 7:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
 break; 
 default:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 }

 cstep=cstep+1;
 if(cstep==8)
  {cstep=0;}
 }


  void step2() 
{
//stepp
switch(cstep)
{
 case 0:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
 break; 
 case 1:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
 break; 
 case 2:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 3:
 digitalWrite(IN1, HIGH); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 4:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 
 case 5:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 break; 
 case 6:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 break; 
 case 7:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, HIGH);
 break; 
 default:
 digitalWrite(IN1, LOW); 
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 break; 

 }

  cstep=cstep+1;
 if(cstep==8)
 {
  cstep=0;
  //Serial.println("habis");

  }
 }
</code></pre>
