How to rotate stepper motor infinitely

1,779 views
Skip to first unread message

Basyir Burhanuddin

unread,
May 5, 2016, 1:10:53 PM5/5/16
to accelstepper
Hello, I wanna use 3 way rocker switch to make my stepper motor can have switch move clockwise and anticlockwise. However I can't manage to find the code. Can u guys help me?

#include <AccelStepper.h>
#define HALFSTEP 8

boolean start=true;
volatile boolean e_stop=false; //initialization for emergency button

int IRcw=8; //IR sensor clockwise
int IRccw=9; //IR sensor counter clockwise

int switchUP=20;
int switchDOWN=21;

// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper(HALFSTEP, motorPin1, motorPin2, motorPin3, motorPin4);

void setup() 
{
  
  pinMode(IRcw,INPUT); 
  pinMode(IRccw,INPUT);

  pinMode(switchUP,INPUT);
  pinMode(switchDOWN,INPUT);
  
  Serial.begin(9600);
  attachInterrupt(0,e_stop_ISR,RISING);
  
  stepper.setMaxSpeed(1000.0);
  stepper.setAcceleration(400.0);
  stepper.setSpeed(500);

}

//--(end setup )---

void loop() 
{
  int valueCW=digitalRead(IRcw); //read value from IR sensor HIGH or LOW
  int valueCCW=digitalRead(IRccw); //read value from IR sensor HIGH or LOW
  
  int valueSwitchUP=digitalRead(switchUP);
  int valueSwitchDOWN=digitalRead(switchDOWN);
  
   if(start==true)
   {
      if(e_stop==false)
      {
         if (valueCW==LOW && stepper.distanceToGo() >= 0 &&  ) //stepper.distanceToGo() for as long stepper did not reaches target position it cannot rotate to other direction
            {
    
            stepper.move(8000);
            }
          else if(valueCCW==LOW && stepper.distanceToGo() <= 0)
            {
            stepper.move(-8000);
            }
           else if(valueSwitchUP==HIGH)
           {
            //HERE I WANT TO MAKE IT ROTATE CLOCKWISE INFINITELY AS UP SWITCH IS ON
           }
           else if(valueSwitchDOWN==HIGH)
           {
           //HERE I WANT TO MAKE IT ROTATE COUNTER CLOCKWISE INFINITELY AS DOWN SWITCH IS ON            
           }
  
             stepper.run();
            }

    else
        {
        start=false;
        }
   }

   
  
}
//--(end loop)--

void e_stop_ISR(void)
{
  detachInterrupt(0);
  e_stop=!e_stop;
}



Sandy Noble

unread,
May 5, 2016, 4:04:14 PM5/5/16
to accels...@googlegroups.com
You mean indefinitely. (Infinitely states there is no end, indefinitely states that you haven't decided when the end will be.)

stepper.setSpeed(500);
stepper.runSpeed();

should work for you, use negative speed for reverse direction. Down side of this is that you don't get acceleration.

By giving an incomplete, non-compiling example, you've limited how helpful we can be here though. There might be better ways to do it, but because your example doesn't run, and it's not clear what it does, I can't really explore it very well.


sn


--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Sandy Noble
Reply all
Reply to author
Forward
0 new messages