0
#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.

0

3 Answers 3

1

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");

  }
 }
2
  • 2
    Please, expand your answer to explain what your doing and why. Teach the man how to fish.
    – user31481
    Oct 21, 2017 at 14:55
  • copy the code that i was give .. change the pin of stepper motor to your own pin .. then upload to the arduino board and see waht happend at serial monitor
    – Mohd Zubir
    Oct 21, 2017 at 16:49
0

Disclaimer: I've never used this library. The following is my thoughts from looking at the code.

This example seems to do basically what you want. It doesn't seem to be necessary to do any digitalWrites yourself - you just need to call call .step(x).

There also doesn't seem to be any need to anything in the "do nothing" case - you don't need to tell it not to step, as far as I can see.

I couldn't work out why you were stepping 40000 steps at a time. I've changed it to 1. You can change it back if I missed something.

Also note I've used else if so that if both inputs are high, the motor doesn't go forward AND backwards. Maybe that's the behaviour you want, and if not, again, you can change it.

#include <Stepper.h>
#define STEPS_PER_MOTOR_REVOLUTION 32   
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 8, 10, 9, 11);
int buttonApin=2;
int buttonBpin=3;

void setup()
{
    pinMode(buttonApin,INPUT);
    pinMode(buttonBpin,INPUT);
}

void loop()
{
    int valA=digitalRead(buttonApin);
    int valB=digitalRead(buttonBpin);
    if(valA==HIGH)
    {
        small_stepper.step(1);
        delay(1000);
    }
    else if(valB==HIGH)
    {
        small_stepper.step(-1);
        delay(1000);
    }
} 
0

Does the relay actually connect the pin to ground when it is off? Or does it leave the input floating? If it leaves it floating then you should use a pull-down resistor to make sure the pin actually sees ground and not stray interference.

If you can rewire the relays you can use an active low configuration and save the resistor since the Arduino has built-in pullup resistors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.