Increase speed of stepper motor

Salut!

I apologize if I did not write well in English!

I want to control a stepper motor from serial command and the Joystick! I do not know programming at all but what I did to take from the internet is shown in video and in the attached code.

The problem is that I can not get the stepper to spin at a higher speed (with more steps per second) and because in each loop the "step" increases / decrease with a unit. I do not know very well how to use a library and that's why I did not use the much used AccelStepper Library.

Can someone show me how to make better use of the AccelStepper Library properties so I can increase stepper speed and have a number of steps (position)!!!??? Or guides me to sites / tutorials. And yes, I watched at those on youtube!!!

link video: Dropbox - VID_20181203_163145.mp4 - Simplify your life

#include "AccelStepper.h" 

AccelStepper stepper(1, 4, 3);

#define home_switch 9
long initial_homing;
long pozitieM;
long np;
#define x_pin A0 
//int MS1 =5;
//int MS2=6;
//int MS3=7;

long lim_min_M;
long lim_max_M;
boolean start = true;
volatile boolean e_stop = false;   // emergency button




void setup() {
     Serial.begin(9600);  

//   digitalWrite(MS1, LOW);      
//   digitalWrite(MS2, HIGH); 
//   digitalWrite(MS3, LOW); 
  pinMode(x_pin, INPUT);
  
       attachInterrupt( 0, e_stop_ISR, RISING);  // emergency button
       
   pinMode(home_switch, INPUT_PULLUP);
   delay(5);
   

  initial_homing=-1;
// started homing
  
  while (digitalRead(home_switch)) {  
    
    stepper.setMaxSpeed(5000);
    stepper.setAcceleration(4000);   
  
    stepper.moveTo(initial_homing);  
    initial_homing--; 
       stepper.runSpeed();
    Serial.print("Nr. pasi buton dezactivat : ");
    Serial.println(initial_homing);

}
   
  stepper.setCurrentPosition(0);   
  initial_homing=1;
 

  while (!digitalRead(home_switch)) {
   
    stepper.moveTo(initial_homing);  
       stepper.runSpeed();
    initial_homing++;
    Serial.print("Nr. pasi buton activat : ");
    Serial.println(initial_homing);


  }
  delay(500);

  stepper.setCurrentPosition(0);

  for (initial_homing = 0; initial_homing <=200; initial_homing++)  // 200***
  {   
    stepper.moveTo(initial_homing);
       stepper.runSpeed(); 
    Serial.print("Nr. pasi Homing: ");
    Serial.println(initial_homing);
    
// end homing

  }
    stepper.setMaxSpeed(1000);
    stepper.setSpeed(800);  
  stepper.setCurrentPosition(0);
    initial_homing = 0;
}

void loop() 
{
lim_max_M =  400;
lim_min_M = -400;
   while (Serial.available()>0) 
   { 

//long initial_homing;


        Serial.println("");
        Serial.print("initial_homing:  ");
        Serial.println(initial_homing);
        
        Serial.println("");
        
        Serial.print("Noua pozitie:  ");
        Serial.println(pozitieM);

        Serial.println("");

        Serial.print("initial_homing + pozitieM:  ");
        Serial.println(np);
    
    pozitieM= Serial.parseInt();  
      
    np= pozitieM + initial_homing;   

    if(np < lim_min_M || np > lim_max_M)
    {
      Serial.println("");
      Serial.println("Se depaseste intervalul 'lin_min_M & lim_max_M' :  ");
      Serial.println("");
    }
    else
    {
      stepper.moveTo(np);
    }
     
     if (( np )>= 0 && (np )<= lim_max_M) 
     {
        if(np > initial_homing)
        {
              for (initial_homing = initial_homing; initial_homing <= np; initial_homing++)
              {

                    if ( start == true)
                      {
                        if (e_stop == false)
                        {
                            stepper.moveTo(np);
                            stepper.run(); 
                            Serial.print(" '+' Pasi CW: ");
                            Serial.println(initial_homing );
                        }
                        else
                        {
                            Serial.println(" ");
                            Serial.println("Emergency: ");
                            start = false;      
                        }
                      }      
               } // for
               initial_homing= initial_homing - 1;
        } // if
      
    else
    {
        if(np < initial_homing)
        {
            for (initial_homing = initial_homing; initial_homing >=np; initial_homing--)
           {
                if ( start == true)
                {
                   if (e_stop == false)
                    {
                      stepper.moveTo(np);
                      stepper.run(); 
                      Serial.print(" '+' Pasi CCW: ");
                      Serial.println(initial_homing);
                    }
                    else
                    {
                      Serial.println(" ");
                      Serial.println("Emergency: ");
                      start = false; 
                     }
                  } 
           } // for
           initial_homing= initial_homing + 1;
        }
    }
    }
  
if ((np )>= lim_min_M && (np )<=0) 
  {
        if(np > initial_homing)
        {
              for (initial_homing = initial_homing; initial_homing <= np; initial_homing++)
              {
                  if ( start == true)
                  {
                    if (e_stop == false)
                    {
                      stepper.moveTo(np);
                      stepper.run(); 
                      Serial.print(" '-' Pasi CW: ");
                      Serial.println(initial_homing );             
                    }
                    else
                    {
                      Serial.println(" ");
                      Serial.println("Emergency: ");
                      start = false;
                    }
                  }
               }// for
               initial_homing= initial_homing - 1;
        }
      
    else
    {
        if(np < initial_homing)
        {
            for (initial_homing = initial_homing; initial_homing >=np; initial_homing--)
           {
               if ( start == true)
               {
                  if (e_stop == false)
                  {
                     stepper.moveTo(np);
                     stepper.run(); 
                     Serial.print(" '-' Pasi CCW: ");
                     Serial.println(initial_homing);
                  }
                  else
                  {
                    Serial.println(" ");
                    Serial.println("Emergency: ");
                    start = false;
                  }
               }
           }// for
           initial_homing= initial_homing + 1;
        }
    }
  }    
//  initial_homing = initial_homing -1;
   }
  while (analogRead(x_pin) >= 0 && analogRead(x_pin) <= 100) 
  {
    if (initial_homing >= lim_min_M   && initial_homing <lim_max_M ) 
    {  
                stepper.setMaxSpeed(1000.0);
                stepper.setAcceleration(200.0);
                stepper.moveTo(initial_homing);
                stepper.run(); 
                initial_homing++;
                delay(5);
                Serial.print(" Joystick CW: ");
                Serial.println(initial_homing);       
    }
  }
   
    while (analogRead(x_pin) > 900 && analogRead(x_pin) <= 1024)
    {
       if (initial_homing >= lim_min_M && initial_homing <lim_max_M) 
      {     
                stepper.setMaxSpeed(1000.0);
                stepper.setAcceleration(200.0);
                stepper.moveTo(initial_homing);
                stepper.run(); 
                initial_homing--;
                delay(5);
                Serial.print(" Joystick CCW: ");
                Serial.println(initial_homing);
      }
      if(initial_homing == lim_max_M)
      {
        initial_homing=initial_homing-1;
      }
            if(initial_homing == lim_min_M)
      {
        initial_homing=initial_homing+1;
      }
    }
   }

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

I apologize if you did not post it in the right section!

Thank you!

What is the stepper doing? Do you NEED acceleration and deceleration?

What number of steps per second are you seeing? What number do you need?

What is between the Arduino and the stepper motor? How are you powering the stepper motor?

Salut PaulS!

  1. I intend to make a robotic arm on 3-4 axes and I thought I could give him precise positions taking into account the number of steps! Another method did not pass through my mind borrowing. I tried to borrow the principle of functioning from my 3d printer or at least so I think it works.

Yes. I think I would use acceleration and deceleration! AccelStepper Library can do this just because I have not been able to implement it at the current code.

  1. I do not know by what frequency the steps vary in the loop. I understand from someone today that it could change the frequency of the time when some pins come out,I do not know if that's my problem, I did not have enough time to read about it.

In terms of the number of steps, I do not have exactly a number !! I want somehow to change speed, pike is only possible but my code does not allow me that. I can simplify a code and show you a value order I would like to get to.

  1. Between the arduino plate and the motor is an a4988 module!

Thanks!!!

Take a look at accelstepper's multistepper:

https://www.airspayce.com/mikem/arduino/AccelStepper/classMultiStepper.html

You tell it what step count to move to for each stepper motor, and they all move there in unison. Seems ideal for a robot arm and works the way I would like it to on mine.

Salut!

I partially solved the problem! I had active "serial monitor" and the transfer rate was "9600 baud" and for this reason I think it was not able to send all the information. I increased the speed to "250000 baut". I do not know exactly what it does but it works as good as it is!

Thanks and I hope to help somebody! Cheers !!!