Speed problem with stepper library

Hi,

I am trying to figure out how to make a bipolar stepper move faster than in this code
In setup when it zeroes the motor it is going real fast.
I dont get it to follow the input speed.

Here is the Code

#include <Stepper.h>
#define  STEPS  720    // steps per revolution (limited to 315°)
#define  COIL1  4
#define  COIL2  5
#define  COIL3  6
#define  COIL4  7
Stepper stepper(STEPS, COIL1, COIL2, COIL3, COIL4);
 
//Define Variables 
  int rpm = 0;
  int spd = 0;
  volatile int rpmcount = 0;  // counts up the times the tach-in signal falls, volitile because the variable is manipulated during the interrupt
  volatile int spdcount = 0;
  unsigned long timeold = 0;  // used to calculate d_t= millis()-timeold;
  int d_t;  // delta_time
  int tachout = 9;  // Pin 9 is the PWM output to the analog Tach
  
void setup()
{
  stepper.setSpeed(80);    // set the motor speed to 30 RPM (360 PPS aprox.).
  stepper.step(630);       //Reset Position(630 steps counter-clockwise). 
  
   pinMode(tachout,OUTPUT);  // define outputpin as an output (not needed?)
   Serial.begin(9600);  // open serial communications
   
   attachInterrupt(0, tach_counter, FALLING);  //Interrupt 0 is digital pin 2, so that is where the Input is connected
   attachInterrupt(1, speed_counter, FALLING);  //Speedo input is digial pin 3
}
int pos=(0);

void loop()
{
   d_t=millis()-timeold;  //sets delta_time to (current clock time - the clock time of the last fixed interval)
  
   if (d_t >= 150)  // after 150ms, stop endless loop & collection of interrupts & rpmcounts, and move to reporting
    {
      rpm = map(rpmcount, 0, 110, 0, 81);  //map instead of compute - was some concern about variable types being a problem...
      spd = map(spdcount, 0, 110, 0, 81);
      //Serial Port Output
    //  Serial.print("Time(ms) ");
     // Serial.print(timeold);  //clock time as of this interval
    //  Serial.print(" TicsPerInterval ");
    //  Serial.print(rpmcount);  //number of interrupts & rpmcounts collected during the interval
    //  Serial.print(" RPM PWM Out ");
      Serial.println(spd);  //mapped value from rpmcount
      analogWrite(tachout, rpm);
//add code to run stepper motor here

int val = spd;   //get the potentiometer value (range 0-1023)
  val= map(spd,0,1023,0,630);    // map pot range in the stepper range.

  if(abs(val - pos)> 2){         //if diference is greater than 2 steps.
      if((val - pos)> 0){
          stepper.step(-1);      // move one step to the left.
          pos++;
          }
      if((val - pos)< 0){
          stepper.step(1);       // move one step to the right.
          pos--;
          }
      }
      rpmcount = 0;  //reset rpmcount in preparation for the next interval
      spdcount = 0;
      timeold = millis();  //records the clock time of the current interval
      d_t=0;  //reset delta_time   
    }
}

void tach_counter()  //This interrupt is run each time the processor senses a falling slope in the tach input
{
   rpmcount++; //update rpmcount
}

void speed_counter()  //This interrupt is run each time the processor senses a falling slope in the speedo input
{
   spdcount++; //update spdcount
}

I think maybe the problem lies here

if(abs(val - pos)> 2){         //if diference is greater than 2 steps.
      if((val - pos)> 0){
          stepper.step(-1);      // move one step to the left.
          pos++;
          }
      if((val - pos)< 0){
          stepper.step(1);       // move one step to the right.
          pos--;

To provide useful help you must post links to the datasheets for the motor and the stepper driver you are using. Also provide details of the motor power supply - volts and amps.

Stepper motors work faster with higher voltage supplies - but then the driver must be able to limit the current.

You may get useful info in stepper motor basics.

...R

I am using a bipolar X27 from a car instrument. 5volt. Have tested with and without L293D but same result. Also tried with other libraries but same. When i run it to a spesific number of steps it goes fast, so i think it must be in the code.

When i run it to a spesific number of steps it goes fast, so i think it must be in the code.

The comments in your code say one thing. The code says something else. The FIRST thing YOU need to do is make the comments match the code OR make the code match the comments.

Otherwise, you look like a clueless newbie, which I'm sure is not the impression you are trying to make.

ruud1: I am using a bipolar X27 from a car instrument. 5volt. Have tested with and without L293D but same result. Also tried with other libraries but same. When i run it to a spesific number of steps it goes fast, so i think it must be in the code.

What do you mean by "without L293D" - what alternative driver were you using? As indicated in stepper motor basics an L293D is a poor choice for a stepper driver.

Have you tried this simple stepper code which was linked to in stepper motor basics? It is good for testing motors and drivers and uses no libraries.

...R

Robin2: What do you mean by "without L293D" - what alternative driver were you using? As indicated in stepper motor basics an L293D is a poor choice for a stepper driver.

Have you tried this simple stepper code which was linked to in stepper motor basics? It is good for testing motors and drivers and uses no libraries.

...R

Arduino drives small steppers like this without driver, they draw wery little current. Have tested with basic stepper code and it works fine. Even in setup at the code it runs fast to the zero( stepper.step(630))

Arduino drives small steppers like this without driver, they draw wery little current.

Nonsense. Even the smallest steppers draw more current than the Arduino can SAFELY provide.

PaulS: Nonsense. Even the smallest steppers draw more current than the Arduino can SAFELY provide.

That is wrong. I drive two steppers directly in another project, and it shows no problems.

http://guy.carpenter.id.au/gaugette/resources/vid/20091026113525_VID29_manual_EN-080606.pdf

That is wrong. I drive two steppers directly in another project, and it shows no problems.

Yet.

PaulS: Yet.

"So far so good" said the man as he fell past the 17th floor.

...R

ruud1: That is wrong. I drive two steppers directly in another project, and it shows no problems.

http://guy.carpenter.id.au/gaugette/resources/vid/20091026113525_VID29_manual_EN-080606.pdf

Those are the lowest current steppers I've ever seen - not a usual case. I've a batch of tiny 50 ohm stepper motors about 7mm across and they need more current than an Arduino can provide. Even if they could you'd need 8 schottky diodes to protect the protection diodes.

Remember most readers of this site will never encounter a 20mA stepper motor!