Multiple stepper movement using accelstepper and stepper library

Hi

I am doing code for an 360 product camera using 2 different steppers. ive worked out most of the general movment and in the process of adding customisation. the problem is when i am trying to get the second stepper to rotate a certain amount in a void function the other stepper ( Which is meant to be constantly moving until said otherwise) stops for the duration of this void. I have tried putting a stepper.runspeed inside the void but nothing seems to work, can anyone offer any advice?

#include <AccelStepper.h>
#include <LiquidCrystal.h>
#include <Stepper.h>
#include "IRremote.h"


//
int receiver = 12;
int Count = 0;
int CountAng = 0;
int Rotation = 0;
int Angle;
int Home = 1;

IRrecv irrecv(receiver);    
decode_results results;  

AccelStepper stepper (4,8,9,10,11);
Stepper stepper2 (2048,3,4,5,6);
//LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);//

int Speed = 100;
   
void setup()
{ 
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  stepper.setMaxSpeed(475);
   stepper.setSpeed(Speed);
     //lcd.begin(16, 2);
   stepper2.setSpeed(17); //RPM
      
}

void loop()
{
 //stepper2.runSpeed();


if (irrecv.decode(&results)) 
  {
    switch(results.value)

    {

      case 0xFF02FD: //Play
     Count = (1);
    

      break;
      
   case 0xFF629D: //Vol+           
            Speed = Speed+10;
            Serial.println(Speed);
            stepper.setSpeed(Speed);        
                      break;
                     
   case 0xFFA857: //Vol-
             Speed = Speed-10;
             Serial.println(Speed);
             stepper.setSpeed(Speed);      
                      break;
              
   case 0xFFE21D: //Stop
   
     Count = 0; 
     Speed = 100;
     stepper.setSpeed(Speed);
   
   break;

    case 0xFFC23D: //FAST FORWARD
  //RotationStart(); 
  

    break;

    case 0xFF6897: //0
    
   
    
    break;



    case 0xFF30CF: //1    
Serial.println("Bang"); 
   Angle = 90;
   stepper.runSpeed();
   SetAngle();
 
    
    break;

    case 0xFF18E7: //2 
    
    
    break;

    case 0xFF7A85: //3
    
    
    break;
                
    }
    
      irrecv.resume(); // receive the next value
                    
  }  

  if (Count ==1){
  stepper.runSpeed();
  }
  
  

  
     else{
      
      //digitalWrite(8, LOW);
           //digitalWrite(9, LOW);
           //digitalWrite(10, LOW);
           //digitalWrite(11, LOW);  
     }

   /*lcd.begin(16, 2);
   lcd.print("RPM: ");
   lcd.print(Speed);
*/


  
}/* --end main loop -- */


void SetAngle(){
  Angle = map(Angle,0,90,0,8192);
  stepper2.step(Angle);
  
  digitalWrite(3, LOW);
           digitalWrite(4, LOW);
           digitalWrite(5, LOW);
           digitalWrite(6, LOW);
  

  
}


  



The stepper library is blocking, so if this stepper runs, everything else stops. You must run both stepper with AccelStepper to avoid this blocking.
You could try the MobaTools library instead of AccelStepper and Stepper. With MobaTools it's fairly easy to run 2 stepper, because the steppulses are created in the background. Even if you use delay in your sketch, the steppers would continue running.

Change that to:

  for (int count = 0; count < Angle; count++)
  {
    stepper2.step(1);
    if (Count ==1)
    {
      stepper.runSpeed();
    }
  }

But with this solution 'stepper' can never run faster than 'stepper2'.
I think it is a bad idea to use 2 variable names that differ only by one letter in case. This can be a nasty trap.

Both steppers need to use AccelStepper, and you should avoid any blocking calls, ie just use move/moveTo and ensure run() is called frequently (no delays anywhere in the code).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.