Trouble running Accelstepper

Trying to use Accelstepper for the first time, no stepper moves at all.

The hardware is tested using other home brewed code and works.
For a project controlling a rotating table on my mill a stepper is attached to the crank handle. An UNO, a TB6600 and some code is put together, all fed by a 24V, 17A, switched supply. The code is verified to work up until Accelstepper is called. No stepper moves so far.
In this version the execution is stuck in myStepper.runToNewPosition(anglePos);//Blocking until done

  myStepper.run();

is called in every turn of loop.
Poking around I find lists of AccelStepper comands. Some are accepted by the controller, some are not. Code below with the current attempt.
What do I miss?

#include<arduino.h>

//I2C for LCD
#include <AccelStepper.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

AccelStepper myStepper (2, 5);

// LCD geometry
#define LCD_COLS 16
#define LCD_ROWS 2

enum mode {Sides, Angle };
int faces = 4;
unsigned long nrOfStepsPerRev = 90L * 200 * 4; // microstep 4
unsigned long currentStep;

enum cmd {increase, decrease, Go, Chg, GoStep, nobutton};
unsigned long anglePos;

void setup() {
  Serial.begin(115200);
  Serial.println("Rotation ");
  /*  //1Hz 90% dutycycle
    pinMode(9, OUTPUT);                               // Set digital pin 9 (D9) to an output
    TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);  // Enable the PWM output OC1A on digital pins 9 and invert output
    TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12);     // Set fast PWM and prescaler of 256 on timer 1
    ICR1 = 62499;                                     // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
    OCR1A = 6249;                                     // Set the duty-cycle to 10%: 62499 / 10 = 6249

    pinMode(10, OUTPUT);                               // 1 Hz pulse output on digital pin 9 (D9)
  */  pinMode(13, OUTPUT); digitalWrite(13, LOW);// Make board LED go off
  delay(10);//allow pwm timers to start

  //  myStepper.begin();
  myStepper.setMaxSpeed(500);
  myStepper.setAcceleration(100);

#define digitalIncreaseButton 2
  pinMode (digitalIncreaseButton, OUTPUT);
#define digitalDecreaseButton 3
  pinMode (digitalDecreaseButton, OUTPUT);
#define digitalChgButton 4
  pinMode(digitalChgButton, OUTPUT);
#define digitalGoButton 5
  pinMode(digitalChgButton, OUTPUT);
#define guard1 6
#define guard2 7

#define cuttingSpeed 100 // when cutting during rotation
#define movingSpeed 2000 //when moving without cutting

  /*
       int status;
    status = mylcd.begin(LCD_COLS, LCD_ROWS);
    if (status) // non zero status means it was unsuccesful
    {
      status = -status; // convert negative status value to positive number

      // begin() failed so blink error code using the onboard LED if possible
      hd44780::fatalError(status); // does not return
    }
    mylcd.clear();
  */
  // Print start message to the LCD
  /*  mylcd.print("221118a Rotating");
    delay(5000);
    mylcd.print("Running");
    delay(1000);
  */
}

enum cmd readButtons(void)
{
  char cmd = 'q';
  /*
    if ((digitalIncreaseButton) == 0)  return (increase);
    if ((digitalDecreaseButton) == 0)  return (decrease);
    if ((digitalGoButton) == 0)        return (Go);
    if ((digitalChgButton) == 0)       return (Chg);
  */
  if (Serial.available() > 0)
  {
    cmd = Serial.read();
    //    Serial.print("Recieved cmd "); Serial.println(cmd);
  }

  if (cmd == 's' )
    return (GoStep);
  if (cmd == 'S' )
    return (GoStep);

  return (nobutton);
}

boolean guardCheck(void)
{
  /* commented out until hardware is designed
    if (!digitalRead(guard1) && !digitalRead(guard2))
    return (true);//Unlocked, clear to go
    else
    return false;//Locked, don't go
  */
  return (true);
}

void loop()
{
  unsigned long lastMillis;
  enum cmd lcmd;
  enum mode lmode;
  float tmpfloat;

  lcmd = readButtons();// Read button inputs if any    + - Go Chg mode

  if ( millis() - lastMillis > 200 )
  {
    lastMillis = millis();
    if ( lmode == Sides )  //number of surfaces
    {
      switch (lcmd)
      {
        case increase:
          {
            faces++;
            break;
          }
        case decrease:
          {
            faces--;
            if (faces < 2)
            {
              faces = 1; //1 rotate one turn
              myStepper.setMaxSpeed(cuttingSpeed);
            }
            else
              myStepper.setMaxSpeed(movingSpeed);
            break;
          }
        case Chg:
          {
            lmode == Angle; //switch to angle mode
            myStepper.setMaxSpeed(movingSpeed);// used i angle mode
            break;
          }
        case Go:
          {
            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON
            anglePos += nrOfStepsPerRev * 360 / faces;
            myStepper.runToNewPosition(anglePos);//Blocking until done
            break;
          }
        case GoStep:
          {
            Serial.print("GoStep received. ");
            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON

            Serial.print("Stepping to step "); Serial.println(currentStep + 1);
            anglePos = nrOfStepsPerRev * 360 * ++currentStep / faces;
            currentStep %= faces;
            Serial.print("anglePos = "); Serial.println( anglePos);
            tmpfloat = 360 * float(currentStep) / float(faces);
            Serial.print("Angle = "); Serial.println( tmpfloat);

            myStepper.runToNewPosition(anglePos);//Blocking until done
            //            myStepper.runToPosition(anglePos);//Blocking until done

            if (currentStep == 0)// Back on zero position, update AccelStepper position!!!
              myStepper.setCurrentPosition(0);//Accelstepper ready for a second turn around
            break;
          }
        default:
          break;
      }//end of switch
    }//end of "if (lmode == Sides)"
    else if ( lmode == Angle )  // Angle per step
    {
      switch (lcmd)
      {
        case increase:
          {
            anglePos += nrOfStepsPerRev * 5 / 360; //Increase by 5 degrees
            break;
          }
        case decrease:
          {
            anglePos -= nrOfStepsPerRev * 5 / 360; //Increase by 5 degrees
            //           if (faces < 1) faces = 1; //1 rotate one turn
            break;
          }
        case Chg:
          {
            lmode == Sides; //switch to sides mode
            //          goFlag = false;//
            break;
          }
        case Go:
          {
            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON

            myStepper.setMaxSpeed(movingSpeed);
            //            myStepper.runToNewPosition(anglePos);//Blocking until done
            myStepper.moveTo(anglePos);//Blocking until done
            anglePos += nrOfStepsPerRev * 5 / 360;
            break;
          }
        default:
          break;
      }
    }
  }
  //  if (guardCheck())
  //  {
  //    Serial.print("!");
  myStepper.run();
  //  }
}// End of loop
/*
  moveTo KEYWORD2
  move  KEYWORD2
  run KEYWORD2
  runSpeed  KEYWORD2
  setMaxSpeed KEYWORD2
  setAcceleration KEYWORD2
  setSpeed  KEYWORD2
  speed KEYWORD2
  distanceToGo  KEYWORD2
  targetPosition  KEYWORD2
  currentPosition KEYWORD2
  setCurrentPosition  KEYWORD2
  runToPosition KEYWORD2
  runSpeedToPosition  KEYWORD2
  runToNewPosition  KEYWORD2
  stop  KEYWORD2
  disableOutputs  KEYWORD2
  enableOutputs KEYWORD2
  setMinPulseWidth  KEYWORD2
  setEnablePin  KEYWORD2
  setPinsInverted KEYWORD2
  maxSpeed  KEYWORD2

  enum flag {const1, const2, ..., constN};
  By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).

  // Changing default values of enum constants
  enum suit {
  club = 0,
  diamonds = 10,
  hearts = 20,
  spades = 3,
  };
*/

What angle are you actually trying to run it to?

Looks to me like you set nrOfStepsPerRev to 72000:
unsigned long nrOfStepsPerRev = 90L * 200 * 4; // microstep 4
Then later you multiply that by 360 and divide by faces:
anglePos += nrOfStepsPerRev * 360 / faces;
So if you have 4 faces, the anglePos would be set to 6,480,000 degrees?

If I lookup demo-codes of AccelStepper I see

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

You are using it

Does this really work with just 2 parameters?

@Railroader

as you are a railroader (model-railroader) for driving stepper-motors
I recommend to you use the MobaTools from user @MicroBahner

With AccelStepper you have to accept one of two disadvantages:

disadvantage 1: driving the stepper-motor in a blocking way which means nothing else than driving the steppermotor can be done as long as the motor runs

disadvantage 2: keeping your main-loop looping really fast whenever you want to drive the steppermotor in a non-blocking way. The function run() has to be called 1000 times per second to make sure that the step-pulses are created right in time.

The MobaTools create the step--pulses "in the backround" with the help of a timer.
This enables to start the stepper-motor and immitiately change over to executung other code
while the step-pulses are created with the right timing.

best regards Stefan

Not 6480000 degrees, but steps.
In this setup, to 90 degrees. Something is wrong..

To 90 degrees debug prints say 6480000 steps. 90 hand crank turns per 360 degrees. 200 step stepper and factor 4 micro stepping for one hand crank turn. 902004 == 72000...... Hmmm. The factor 360 is already accounted for.
Dropping the 360 factor makes no change.
Still no moves at all.

change your code to

AccelStepper myStepper (1,2, 5);

You coded
AccelStepper myStepper (2, 5);
it has to be three parameters
AccelStepper myStepper (1,2, 5);

Thanks a lot! That made the stepper run!
Next move is to get the rotating board here and mechanically connected for checking accuracy.
I'll come back soon and tell!

You solved this matter. I'll mark Your reply as "Solution".
However there are still some trouble that I posted in the motor section.
The move I got was 111 degrees, not 90. The speed of the stepper is also a bit erratic, not even. During earlier runs, at low speed, even backsteps could be observed.
I've been buying, running and checking up plenty of steppers and several drivers but never faced anything like this. The TB6600 was checked some year or two ago and I never noticed anything like this.
The code for the moment....

#include<arduino.h>

//I2C for LCD
#include <AccelStepper.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

AccelStepper myStepper (1, 2, 5);

// LCD geometry
#define LCD_COLS 16
#define LCD_ROWS 2

enum mode {Sides, Angle };
int faces = 4;
unsigned long nrOfStepsPerRev = 90L * 200 * 4; // microstep 4
unsigned long currentStep;

enum cmd {increase, decrease, Go, Chg, GoStep, nobutton};
unsigned long anglePos;

void setup() {
  Serial.begin(115200);
  Serial.println("Rotation ");
  /*  //1Hz 90% dutycycle
    pinMode(9, OUTPUT);                               // Set digital pin 9 (D9) to an output
    TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);  // Enable the PWM output OC1A on digital pins 9 and invert output
    TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12);     // Set fast PWM and prescaler of 256 on timer 1
    ICR1 = 62499;                                     // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
    OCR1A = 6249;                                     // Set the duty-cycle to 10%: 62499 / 10 = 6249

    pinMode(10, OUTPUT);                               // 1 Hz pulse output on digital pin 9 (D9)
  */  pinMode(13, OUTPUT); digitalWrite(13, LOW);// Make board LED go off
  delay(10);//allow pwm timers to start

  //  myStepper.begin();
  myStepper.setMaxSpeed(2000);
  myStepper.setAcceleration(400);

#define digitalIncreaseButton 2
  pinMode (digitalIncreaseButton, OUTPUT);
#define digitalDecreaseButton 3
  pinMode (digitalDecreaseButton, OUTPUT);
#define digitalChgButton 4
  pinMode(digitalChgButton, OUTPUT);
#define digitalGoButton 5
  pinMode(digitalChgButton, OUTPUT);
#define guard1 6
#define guard2 7

#define cuttingSpeed 100 // when cutting during rotation
#define movingSpeed 2000 //when moving without cutting

  /*
       int status;
    status = mylcd.begin(LCD_COLS, LCD_ROWS);
    if (status) // non zero status means it was unsuccesful
    {
      status = -status; // convert negative status value to positive number

      // begin() failed so blink error code using the onboard LED if possible
      hd44780::fatalError(status); // does not return
    }
    mylcd.clear();
  */
  // Print start message to the LCD
  /*  mylcd.print("221118a Rotating");
    delay(5000);
    mylcd.print("Running");
    delay(1000);
  */
}

enum cmd readButtons(void)
{
  char cmd = 'q';
  /*
    if ((digitalIncreaseButton) == 0)  return (increase);
    if ((digitalDecreaseButton) == 0)  return (decrease);
    if ((digitalGoButton) == 0)        return (Go);
    if ((digitalChgButton) == 0)       return (Chg);
  */
  if (Serial.available() > 0)
  {
    cmd = Serial.read();
    //    Serial.print("Recieved cmd "); Serial.println(cmd);
  }

  if (cmd == 's' )
    return (GoStep);
  if (cmd == 'S' )
    return (GoStep);

  return (nobutton);
}

boolean guardCheck(void)
{
  /* commented out until hardware is designed
    if (!digitalRead(guard1) && !digitalRead(guard2))
    return (true);//Unlocked, clear to go
    else
    return false;//Locked, don't go
  */
  return (true);
}

void loop()
{
  unsigned long lastMillis;
  enum cmd lcmd;
  enum mode lmode;
  float tmpfloat;

  lcmd = readButtons();// Read button inputs if any    + - Go Chg mode

  if ( millis() - lastMillis > 200 )
  {
    lastMillis = millis();
    if ( lmode == Sides )  //number of surfaces
    {
      switch (lcmd)
      {
        case increase:
          {
            faces++;
            break;
          }
        case decrease:
          {
            faces--;
            if (faces < 2)
            {
              faces = 1; //1 rotate one turn
              myStepper.setMaxSpeed(cuttingSpeed);
            }
            else
              myStepper.setMaxSpeed(movingSpeed);
            break;
          }
        case Chg:
          {
            lmode == Angle; //switch to angle mode
            myStepper.setMaxSpeed(movingSpeed);// used i angle mode
            break;
          }
        case Go:
          {
            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON
            anglePos += nrOfStepsPerRev / faces;
            myStepper.runToNewPosition(anglePos);//Blocking until done
            break;
          }
        case GoStep:
          {
            Serial.print("GoStep received. ");
            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON

            Serial.print("Stepping to step "); Serial.println(currentStep + 1);
            anglePos = nrOfStepsPerRev * ++currentStep / faces;
            currentStep %= faces;
            Serial.print("anglePos = "); Serial.println( anglePos);
            tmpfloat = 360 * float(currentStep) / float(faces);
            Serial.print("Angle = "); Serial.println( tmpfloat);

            myStepper.runToNewPosition(anglePos);//Blocking until done
            while (Serial.available() )//Pretyping commands not allowed
              Serial.read();// Empty buffer to awoid runaway.

            Serial.println("Returned from runToNewPosition.");
            //            myStepper.runToPosition(anglePos);//Blocking until done

            if (currentStep == 0)// Back on zero position, update AccelStepper position!!!
              myStepper.setCurrentPosition(0);//Accelstepper ready for a second turn around
            break;
          }
        default:
          break;
      }//end of switch
    }//end of "if (lmode == Sides)"
    else if ( lmode == Angle )  // Angle per step
    {
      switch (lcmd)
      {
        case increase:
          {
            anglePos = nrOfStepsPerRev * ++currentStep / faces;
            break;
          }
        case decrease:
          {
            anglePos = nrOfStepsPerRev * ++currentStep / faces;
            if (faces < 1) faces = 1; //1 rotate one turn
            break;
          }
        case Chg:
          {
            lmode == Sides; //switch to sides mode
            //          goFlag = false;//
            break;
          }
        case Go:
          {
            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON

            myStepper.setMaxSpeed(movingSpeed);
            //            myStepper.runToNewPosition(anglePos);//Blocking until done
            myStepper.moveTo(anglePos);//Blocking until done
            anglePos = nrOfStepsPerRev * ++currentStep / faces;
            break;
          }
        default:
          break;
      }
    }
  }
  //  if (guardCheck())
  //  {
  //    Serial.print("!");
  //myStepper.run();
  //  }
}// End of loop
/*
  moveTo KEYWORD2
  move  KEYWORD2
  run KEYWORD2
  runSpeed  KEYWORD2
  setMaxSpeed KEYWORD2
  setAcceleration KEYWORD2
  setSpeed  KEYWORD2
  speed KEYWORD2
  distanceToGo  KEYWORD2
  targetPosition  KEYWORD2
  currentPosition KEYWORD2
  setCurrentPosition  KEYWORD2
  runToPosition KEYWORD2
  runSpeedToPosition  KEYWORD2
  runToNewPosition  KEYWORD2
  stop  KEYWORD2
  disableOutputs  KEYWORD2
  enableOutputs KEYWORD2
  setMinPulseWidth  KEYWORD2
  setEnablePin  KEYWORD2
  setPinsInverted KEYWORD2
  maxSpeed  KEYWORD2

  enum flag {const1, const2, ..., constN};
  By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).

  // Changing default values of enum constants
  enum suit {
  club = 0,
  diamonds = 10,
  hearts = 20,
  spades = 3,
  };
*/

possible reasons:
unreliabe direction-signal

half damaged stepper-driver through loose-contact of motorwires and/or connecting disconnecting motorwires while power-supply is switched on.
If you connect a motorwire while power-supply is switched on it can happen that you loose contact for a short time and this creates a high voltage-spike that could damage the stepper-driver
If you run a very simple code that does nothing else than drive the stepper-motor
can you observe the same things?

Thanks for keeping on.
Unreliable dir signal... Yes, worth attacking. So far the commonly used Arduino test cables have been working well. I changed from pins 2 5 to 6 and 7. The same result.

I have a master degree in electronics and 50 years of experience and I know...Never disconnect motor cables while motor power is on. The same over stepping on 2 different TB6600.
Yes. This simple test code shows the same: (X motor used)

#define fwd HIGH
#define bwd LOW
#define motorX 1
#define motorY 2
#define motorZ 3

void setup()
{
  Serial.begin(115200);
  Serial.println("NEMA 23 3.0 Nm test 201110");
  delay(5000);//Show program name
  pinMode(2, OUTPUT); //can be used as x-step
  pinMode(5, OUTPUT); //can be used as x-dir
//  pinMode(8, OUTPUT); //enable active low
  pinMode(13, OUTPUT);
}

void stepmotor(int, int);

void stepmotor(int (motor), int(dir))
{
  unsigned long start;
  start = millis();
  if ( dir == fwd )
  {
    Serial.println("Forward");
  }
  else
  {
    Serial.println("Backward");
  }
  for (unsigned int count = 0; count < 800 ; count++)
  {
    if (motor == motorX )
    {
      digitalWrite( 5, dir);//digital dir does not owerride analoWrite(5, dir)
      digitalWrite( 2, HIGH);// digital x-step
      delayMicroseconds(50);
      digitalWrite( 2, LOW);// digital x-step
      delayMicroseconds(4950);
    }
    if (motor == motorY)
    {
      digitalWrite( 6, dir);
      digitalWrite( 3, LOW);
      digitalWrite( 3, HIGH);
      delay(5);
    }
    if (motor == motorZ)
    {
      digitalWrite( 7, dir);
      digitalWrite( 4, LOW);
      digitalWrite( 4, HIGH);
      delay(5);
    }
  }
  Serial.print("Stopp :"); Serial.println(millis() - start);
  //  digitalWrite(8, HIGH);//disable motorpower

}
void loop() {
  // put you()r main code here, to run repeatedly:

  //  analogWrite( 0, Low);//tst
  //  analogWrite( 1, Low);//tst
  //  analogWrite( 2, Low);// x-step analog KLAR
  //  analogWrite( 3, Low);// y-step analog KLAR
  //  analogWrite( 4, Low);// z-step analog¨KLAR
  //  analogWrite( 5, Low);// x-dir  analog KLAR
  //  digitalWrite(2, LOW);// x-step digitalt
  //  digitalWrite(3, LOW);// y-step digitalt
  //  digitalWrite(4, LOW);// z-step digitalt
  //  digitalWrite(5,LOW); // x-dir digitalt
  //  digitalWrite(6,HIGH);// y-dir KLAR HIGH == CW LOW == CCW
  //  digitalWrite(7,HIGH);// z-dir KLAR HIGH == CW LOW == CCW KLAR
  //  digitalWrite(8,LOW);//
  //  digitalWrite(9,LOW);//
  //  digitalWrite(10,LOW);//
  //  digitalWrite(11,LOW);//
  //  digitalWrite(12,LOW);
  stepmotor( motorX, fwd );
  //  pulse (motorY, fwd );
  //  pulse (motorZ, fwd );
  digitalWrite(13, LOW);
  delay(4000);

  //  analogWrite( 0, High);//
  //  analogWrite( 1, High);//
  //  analogWrite( 2, High);//x-channel KLAR
  //  analogWrite( 3, High);//y-channel KLAR
  //  analogWrite( 4, High);//z-channel KLAR
  //  analogWrite( 5, High);//x-dir     KLAR
  //  digitalWrite(2, HIGH); //x-step!!!
  //  digitalWrite(3, HIGH); //y-step!!!
  //  digitalWrite(4, HIGH); //z-step!!!
  //  digitalWrite(5, HIGH); //X-DIR CW ON HIGH CCW ON LOW
  //  digitalWrite(6, HIGH); //Y-DIR CW ON HIGH CCW ON LOW KLAR
  //  digitalWrite(7, LOW); //Z-DIR CW ON HIGH, CCW ON LOW KLAR
  //  digitalWrite(8,HIGH);//
  //  digitalWrite(9,HIGH);//
  //  digitalWrite(10,HIGH);//
  //  digitalWrite(11,HIGH);//
  //  digitalWrite(12,HIGH);//
  stepmotor( motorX, bwd );// går finfint +200, -200 steg
  //  pulse (motorY, bwd );
  //  pulse (motorZ, bwd );
  digitalWrite(13, HIGH); //

  delay(4000);

}

The most simple program would be something like this

const byte dirPin  = 2;
const byte StepPin = 5;

unsigned long delayTime = 10;

void setup() {
  pinMode(dirPin,OUTPUT);
  pinMode(StepPin,OUTPUT);
  digitalWrite(dirPin,LOW);
  digitalWrite(StepPin,LOW);  
}


void loop() {
  delay(1000);

  for (int i = 0; i < 1000; i++) {
    digitalWrite(StepPin,LOW);
    delay(delayTime);  
    digitalWrite(StepPin,HIGH);
    delay(delayTime);  
  }
}

In one case I had a bad GND-connection between microcontroller and stepper-drives which caused weird behaviour.

Stripping my test code it is as You suggest.
Moved the GND cable to another GND on the board. No change.
The surprise to me is (now) 30% too many steps, not steps being lost. Yes,
the next attempt will be soldering pins to the UNO bord and using the better female gender test cables. Then there is a connector on the stepper...
Once I received a stepper having a broken cable! The insulation was ok but the wire broken...
But this, using 3 different steppers and 2 different drivers and the issue remains...
Tomorrow is another day. New games, new bets....
Thanks for Your time! Accelstepper at least works.

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