Not sure what's wrong

183 views
Skip to first unread message

Zack Kummer

unread,
Oct 30, 2020, 6:36:40 PM10/30/20
to accelstepper
I have a sketch that controls a bed leveling and height control system with 4 stepper motors.  I started doing it manually; digitalWrite style.  That worked but I was losing steps to acceleration, so I modified my sketch to use this library.  Problem is that there is now no motor movement at all...but the drivers get hot.  Hopefully I just missed something?

If it matters, I'm using A4988 drivers and a Teensy 2.0





//4 STEPPER MOTORS, 1 = LR (0,500 of bed), 2 = RR (700,500 of bed), 3 = LF (0,0 of bed), 4 = RF (700,0 of bed)
#include <AccelStepper.h>

//PHYSICAL PINS
const int PIN_EN1 = 13; const int PIN_STEP1 = 20; const int PIN_DIR1 = 21; const int PIN_LIMIT1 = 3;
const int PIN_EN2 = 10; const int PIN_STEP2 = 9;  const int PIN_DIR2 = 14; const int PIN_LIMIT2 = 2;
const int PIN_EN3 = 19; const int PIN_STEP3 = 17; const int PIN_DIR3 = 12; const int PIN_LIMIT3 = 1;
const int PIN_EN4 = 18; const int PIN_STEP4 = 15; const int PIN_DIR4 = 16; const int PIN_LIMIT4 = 4;

const int PIN_STEP_IN = 7; const int PIN_DIR_IN = 8;  //MUST BE INT CAPABLE, COMES FROM MAIN CONTROLLER

const int PIN_PROBE = 0;  //FOR A NOZZLE PROBE (NOT USED BUT THE PIN IS THERE FOR EXPANSION)
const int PIN_LED0 = 11;  //ONBOARD LED PIN (NOT USED)
//PINS 5 & 6 RESERVED FOR I2C
//PHYSICAL PINS




//SETTINGS
const int STEPS_PER_S = 1000;  //THE HIGHEST STEPS PER SECOND THAT YOUR MOTORS AND DRIVERS CAN DO
const int STEPPER_ACCEL = 500; //ACCELERATION FOR STEPPER MOTORS
const bool MOT_DIR_INV1 = 1;
const bool MOT_DIR_INV2 = 0;
const bool MOT_DIR_INV3 = 0;
const bool MOT_DIR_INV4 = 0;
const int STEP_DELAY = 1500; //NUMBER OF MICROSECONDS TO DELAY BETWEEN STEPS
const int STEP_HIGH_TIME = 400; //HOW MANY MICROSECONDS TO HOLD MOTOR STEP HIGH
const int DEBOUNCE_MS = 100;  //USED FOR SWITCH DEBOUNCING, DOES THIS DELAY (IN MS) 2 TIMES TO CONFIRM STATE
const int EN_OFF_MS = 1000;  //THE NUMBER OF MS TO WAIT BEFORE TURNING PAUSING DRIVERS DURING INITIALIZATION
int MOT_OFFSET1 = 750;  //HOW MUCH MOTOR 1 SHOULD BE BELOW LIMIT LEVEL WHEN AT TRUE LEVEL.
int MOT_OFFSET2 = 500;  //HOW MUCH MOTOR 2 SHOULD BE BELOW LIMIT LEVEL WHEN AT TRUE LEVEL.
int MOT_OFFSET3 = 640;  //HOW MUCH MOTOR 3 SHOULD BE BELOW LIMIT LEVEL WHEN AT TRUE LEVEL.
int MOT_OFFSET4 = 775;  //HOW MUCH MOTOR 4 SHOULD BE BELOW LIMIT LEVEL WHEN AT TRUE LEVEL.
//SETTINGS

//VARIABLES
int INITIALIZE_CONFIRM = 0;
const int LED_STATE_DIVIDER = 15000;  //CONTROLS SPEED OF BLINK (HIGHER=SLOWER) (MUST BE EVEN NUMBER)
int LED_TEMP = 0;
byte LED_STATE = 0;
int TEMP01 = 0;
bool HOMEDONE1 = 0; bool HOMEDONE2 = 0;
bool INPUT_DIR = 0;
long M0T_NOW1 = 0; long M0T_NOW2 = 0; long M0T_NOW3 = 0; long M0T_NOW4 = 0; bool MOT_EN_NOW = 0;
long M0T_OLD1 = 0; long M0T_OLD2 = 0; long M0T_OLD3 = 0; long M0T_OLD4 = 0; bool MOT_EN_OLD = 0;
bool PROBE_OLD = 0; bool PROBE_NOW = 0;
bool LIMIT_OLD1 = 0; bool LIMIT_NEW1 = 0; bool LIMIT_OLD2 = 0; bool LIMIT_NEW2 = 0;
bool LIMIT_OLD3 = 0; bool LIMIT_NEW3 = 0; bool LIMIT_OLD4 = 0; bool LIMIT_NEW4 = 0;
unsigned long MOT_EN_TIMER = millis();  //FOR INITIALIZATION, PAUSES TO RESET SQUEEL
//VARIABLES





AccelStepper stepper1(AccelStepper::DRIVER, PIN_STEP1, PIN_DIR1);
AccelStepper stepper2(AccelStepper::DRIVER, PIN_STEP2, PIN_DIR2);
AccelStepper stepper3(AccelStepper::DRIVER, PIN_STEP3, PIN_DIR3);
AccelStepper stepper4(AccelStepper::DRIVER, PIN_STEP4, PIN_DIR4);




void setup() {
  Serial.begin(115200);
  pinMode (PIN_STEP1,OUTPUT); pinMode (PIN_DIR1,OUTPUT); pinMode (PIN_EN1,OUTPUT);  //CONFIGURE MOTOR 1 PINS
  pinMode (PIN_STEP2,OUTPUT); pinMode (PIN_DIR2,OUTPUT); pinMode (PIN_EN2,OUTPUT);  //CONFIGURE MOTOR 2 PINS
  pinMode (PIN_STEP3,OUTPUT); pinMode (PIN_DIR3,OUTPUT); pinMode (PIN_EN3,OUTPUT);  //CONFIGURE MOTOR 3 PINS
  pinMode (PIN_STEP4,OUTPUT); pinMode (PIN_DIR4,OUTPUT); pinMode (PIN_EN4,OUTPUT);  //CONFIGURE MOTOR 4 PINS
 
  pinMode (PIN_LIMIT1,INPUT_PULLUP); pinMode (PIN_LIMIT2,INPUT_PULLUP);  //CONFIGURE LIMIT PINS 1 & 2
  pinMode (PIN_LIMIT3,INPUT_PULLUP); pinMode (PIN_LIMIT4,INPUT_PULLUP);  //CONFIGURE LIMIT PINS 3 & 4
  //pinMode (PIN_PROBE,INPUT_PULLUP);  //CONFIGURE PROBE PIN

  pinMode (PIN_STEP_IN,INPUT);
  pinMode (PIN_DIR_IN,INPUT);

  stepper1.setSpeed(STEPS_PER_S);  //SET STEPPER SPEED
  stepper2.setSpeed(STEPS_PER_S);  //SET STEPPER SPEED
  stepper3.setSpeed(STEPS_PER_S);  //SET STEPPER SPEED
  stepper4.setSpeed(STEPS_PER_S);  //SET STEPPER SPEED
  stepper1.setMaxSpeed(STEPS_PER_S);  //SET STEPPER SPEED
  stepper2.setMaxSpeed(STEPS_PER_S);  //SET STEPPER SPEED
  stepper3.setMaxSpeed(STEPS_PER_S);  //SET STEPPER SPEED
  stepper4.setMaxSpeed(STEPS_PER_S);  //SET STEPPER SPEED
  stepper1.setAcceleration(STEPPER_ACCEL);  //SET STEPPER ACCELERATION
  stepper2.setAcceleration(STEPPER_ACCEL);  //SET STEPPER ACCELERATION
  stepper3.setAcceleration(STEPPER_ACCEL);  //SET STEPPER ACCELERATION
  stepper4.setAcceleration(STEPPER_ACCEL);  //SET STEPPER ACCELERATION
 
  stepper1.setPinsInverted (MOT_DIR_INV1,false,false);  //INVERT DIRECTION PIN IF NEEDED
  stepper2.setPinsInverted (MOT_DIR_INV2,false,false);  //INVERT DIRECTION PIN IF NEEDED
  stepper3.setPinsInverted (MOT_DIR_INV3,false,false);  //INVERT DIRECTION PIN IF NEEDED
  stepper4.setPinsInverted (MOT_DIR_INV4,false,false);  //INVERT DIRECTION PIN IF NEEDED
 

 
  //Initialize();

  //attachInterrupt(digitalPinToInterrupt(PIN_DIR_IN), DIR_NOW, CHANGE);
  //attachInterrupt(digitalPinToInterrupt(PIN_STEP_IN), STEP_NOW, CHANGE);

  Serial.println("Motor # (1-4, 5 for all) , Steps , Dir (0=DOWN (HIGHER NUMBER),1=UP)"); //PRINT INSTRUCTIONS
  Serial.println("99 WILL START INITIALIZATION"); //PRINT INSTRUCTIONS
}

void loop() {
  DRIVER_EN_END();
  SerialControl();
  SerialReport();
  LED_BREATHING();
}






void DRIVER_EN_END(){
  if (stepper1.distanceToGo() == 0){
    if (stepper2.distanceToGo() == 0){
      if (stepper3.distanceToGo() == 0){
        if (stepper4.distanceToGo() == 0){
          MotorEN(1);
        }}}}}
        





void EAR_SAVER(){
  if (millis() > (MOT_EN_TIMER + EN_OFF_MS)){
    MotorEN(1);
    delay(250);
    MotorEN(0);
    MOT_EN_TIMER = millis();
  }
}



void LED_BREATHING(){                   //MAKES THE LED BREATHE AS A "I HAVEN'T LOCKED UP" INDICATOR
LED_TEMP = (LED_TEMP + 1);
if (LED_TEMP == (LED_STATE_DIVIDER / 2)){ digitalWrite(PIN_LED0,HIGH); }
else if (LED_TEMP >= LED_STATE_DIVIDER){ digitalWrite(PIN_LED0,LOW); LED_TEMP = 0; }
}                                       //END OF FUNCTION






void UpdateLimit(int PinNumber){           //PIN NUMBER: 0=PROBE, 1-4 = BED END STOP SWITCHES
  if (PinNumber == 0){                     //IF PROBE HAS BEEN REQUESTED, DO THE FOLLOWING
    TEMP01 = digitalRead(PIN_PROBE);       //READ PIN TO A VARIABLE
    if (TEMP01 != PROBE_NOW){              //IF THE EXISTING PIN STATE DOES NOT MATCH WHAT WAS JUST READ, DO THE FOLLOWING
      delay(DEBOUNCE_MS);                  //WAIT FOR PIN READING TO STABILIZE
      TEMP01 = digitalRead(PIN_PROBE);     //READ PIN TO A VARIABLE
      if (TEMP01 != PROBE_NOW){            //IF THE EXISTING PIN STATE DOES NOT MATCH WHAT WAS JUST READ, DO THE FOLLOWING
        delay(DEBOUNCE_MS);                //WAIT FOR PIN READING TO STABILIZE
        TEMP01 = digitalRead(PIN_PROBE);   //READ PIN TO A VARIABLE
        if (TEMP01 != PROBE_NOW){          //IF THE EXISTING PIN STATE DOES NOT MATCH WHAT WAS JUST READ, DO THE FOLLOWING
          PROBE_NOW = TEMP01;              //AFTER 3 IDENTICAL READINGS, UPDATE THE PIN STATE
  }}}}                                     //CLOSE 4 LAYERS OF IF STATEMENTS
  if (PinNumber == 1){ TEMP01 = digitalRead(PIN_LIMIT1); if (TEMP01 != LIMIT_NEW1){ delay(DEBOUNCE_MS); TEMP01 = digitalRead(PIN_LIMIT1);  //PIN 1 VERSION OF WHAT IS ABOVE
    if (TEMP01 != LIMIT_NEW1){ delay(DEBOUNCE_MS); TEMP01 = digitalRead(PIN_LIMIT1); if (TEMP01 != LIMIT_NEW1){ LIMIT_NEW1 = TEMP01; }}}}  //FOR PIN 0; COMPATED SINCE IT'S SIMILAR
  if (PinNumber == 2){ TEMP01 = digitalRead(PIN_LIMIT2); if (TEMP01 != LIMIT_NEW2){ delay(DEBOUNCE_MS); TEMP01 = digitalRead(PIN_LIMIT2);   //PIN 2 VERSION OF WHAT IS ABOVE
    if (TEMP01 != LIMIT_NEW2){ delay(DEBOUNCE_MS); TEMP01 = digitalRead(PIN_LIMIT2); if (TEMP01 != LIMIT_NEW2){ LIMIT_NEW2 = TEMP01; }}}}   //FOR PIN 0; COMPATED SINCE IT'S SIMILAR
  if (PinNumber == 3){ TEMP01 = digitalRead(PIN_LIMIT3); if (TEMP01 != LIMIT_NEW3){ delay(DEBOUNCE_MS); TEMP01 = digitalRead(PIN_LIMIT3);  //PIN 3 VERSION OF WHAT IS ABOVE
    if (TEMP01 != LIMIT_NEW3){ delay(DEBOUNCE_MS); TEMP01 = digitalRead(PIN_LIMIT3); if (TEMP01 != LIMIT_NEW3){ LIMIT_NEW3 = TEMP01; }}}}  //FOR PIN 0; COMPATED SINCE IT'S SIMILAR
  if (PinNumber == 4){ TEMP01 = digitalRead(PIN_LIMIT4); if (TEMP01 != LIMIT_NEW4){ delay(DEBOUNCE_MS); TEMP01 = digitalRead(PIN_LIMIT4);   //PIN 4 VERSION OF WHAT IS ABOVE
    if (TEMP01 != LIMIT_NEW4){ delay(DEBOUNCE_MS); TEMP01 = digitalRead(PIN_LIMIT4); if (TEMP01 != LIMIT_NEW4){ LIMIT_NEW4 = TEMP01; }}}}   //FOR PIN 0; COMPATED SINCE IT'S SIMILAR
}                                          //END OF FUNCTION

void SerialReport(){                                                                                                 //WRITES TO SERIAL TERMINAL
  if (M0T_OLD1 != M0T_NOW1){  Serial.print("MOTOR 1 POSITION: ");  Serial.println(M0T_NOW1);  M0T_OLD1 = M0T_NOW1;}  //REPORT MOTOR POSITION CHANGE
  if (M0T_OLD2 != M0T_NOW2){  Serial.print("MOTOR 2 POSITION: ");  Serial.println(M0T_NOW2);  M0T_OLD2 = M0T_NOW2;}  //REPORT MOTOR POSITION CHANGE
  if (M0T_OLD3 != M0T_NOW3){  Serial.print("MOTOR 3 POSITION: ");  Serial.println(M0T_NOW3);  M0T_OLD3 = M0T_NOW3;}  //REPORT MOTOR POSITION CHANGE
  if (M0T_OLD4 != M0T_NOW4){  Serial.print("MOTOR 4 POSITION: ");  Serial.println(M0T_NOW4);  M0T_OLD4 = M0T_NOW4;}  //REPORT MOTOR POSITION CHANGE

  UpdateAllLimit();                                                                                                                      //READ ALL THE LIMIT SWITCHES
  if (PROBE_OLD != PROBE_NOW){ Serial.println(" "); PROBE_OLD = PROBE_NOW; Serial.print("Probe: "); Serial.println(PROBE_NOW); }         //REPORT STATE IF CHANGED
  if (LIMIT_OLD1 != LIMIT_NEW1){ Serial.println(" "); LIMIT_OLD1 = LIMIT_NEW1; Serial.print("Limit 1: "); Serial.println(LIMIT_NEW1); }  //REPORT STATE IF CHANGED
  if (LIMIT_OLD2 != LIMIT_NEW2){ Serial.println(" "); LIMIT_OLD2 = LIMIT_NEW2; Serial.print("Limit 2: "); Serial.println(LIMIT_NEW2); }  //REPORT STATE IF CHANGED
  if (LIMIT_OLD3 != LIMIT_NEW3){ Serial.println(" "); LIMIT_OLD3 = LIMIT_NEW3; Serial.print("Limit 3: "); Serial.println(LIMIT_NEW3); }  //REPORT STATE IF CHANGED
  if (LIMIT_OLD4 != LIMIT_NEW4){ Serial.println(" "); LIMIT_OLD4 = LIMIT_NEW4; Serial.print("Limit 4: "); Serial.println(LIMIT_NEW4); }  //REPORT STATE IF CHANGED

  if (MOT_EN_OLD != MOT_EN_NOW){ Serial.println(" "); MOT_EN_OLD = MOT_EN_NOW; Serial.print("DRIVER EN STATE: "); Serial.println(MOT_EN_NOW); }  //REPORT STATE IF CHANGED
}                                                                                                                                           //END OF FUNCTION

void SerialControl(){                                                         //READS FROM SERIAL TERMINAL AND MOVES MOTORS
  while (Serial.available() > 0) {                                            //ONLY ACT IF A MESSAGE IS WAITING
    int SerialUNIT = Serial.parseInt();                                       //GRABS FIRST NUMBER FROM MESSAGE
    int SerialSTEP = Serial.parseInt();                                       //GRABS SECOND NUMBER FROM MESSAGE
    int SerialDIR = Serial.parseInt();                                        //GRABS THIRD NUMBER FROM MESSAGE
    if (SerialUNIT == 99){
      Initialize();
      }
    else if (Serial.read() == '\n') {                                              //IF THE LAST NUMBER IS THE END OF THE LINE, DO THESE THINGS
      TurnMotor(SerialDIR, SerialSTEP, SerialUNIT);                           //TURN THE MOTOR
      Serial.println(" ");                                                    //PRINT A BLANK LINE TO THE TERMINAL
      Serial.println("Motor # (1-4, 5 for all) , Steps , Dir (0=DOWN (HIGHER NUMBER),1=UP)"); //REPRINT INSTRUCTIONS
      Serial.println("99 WILL START INITIALIZATION"); //REPRINT INSTRUCTIONS
      
}}}                                                                           //CLOSE TWO IF STATEMENTS AND END OF FUNCTION











void Initialize(){   //RUN AT START TO DO INITIAL HOMING
  Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" "); //5 EMPTY LINES
  Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" "); //5 EMPTY LINES
  Serial.println("!!!INITIALIZATION PROCEDURE INITIALIZED!!!");
  Serial.println(" ");
  Serial.println("!!!CLEAR THE BED BEFORE PROCEEDING!!!");
  Serial.println(" ");
  Serial.println("CONTINUE?  (1=YES, 2=NO)");
  while(INITIALIZE_CONFIRM == 0){ if (Serial.available() > 0){ INITIALIZE_CONFIRM = Serial.parseInt(); } delay(50);}

if (INITIALIZE_CONFIRM == 1){
  TEMP01 = 0;        //RESETS VARIABLE VALUE TO PREVENT UNWANTED REPEATS
  UpdateAllLimit();  //READ ALL THE LIMIT SWITCHES

  if (LIMIT_NEW1 == 1){ TEMP01 = 1; }       //IF ONE OF THESE PINS IS HIGH, THE LIMIT SW IS PRESSED
  if (LIMIT_NEW2 == 1){ TEMP01 = 1; }       //IF ONE OF THESE PINS IS HIGH, THE LIMIT SW IS PRESSED
  if (LIMIT_NEW3 == 1){ TEMP01 = 1; }       //IF ONE OF THESE PINS IS HIGH, THE LIMIT SW IS PRESSED
  if (LIMIT_NEW4 == 1){ TEMP01 = 1; }       //IF ONE OF THESE PINS IS HIGH, THE LIMIT SW IS PRESSED
  if (TEMP01 == 1){TurnMotor(0,1500,5); }  //IF ANY OF THE PINS WERE HIGH, MOVE THE BED DOWN 1500 STEPS.

  while(HOMEDONE1 == 0){                                           //THIS DOES INITIAL HOMING BASED ON LIMIT SWITCHES
    UpdateAllLimit();                                              //READ ALL THE LIMIT SWITCHES
    if (LIMIT_NEW1 == 0){TurnMotor(1,1,1);}                        //IF LIMIT SWITCH NOT PRESSED, MOVE UP ONE STEP.
    if (LIMIT_NEW2 == 0){TurnMotor(1,1,2);}                        //IF LIMIT SWITCH NOT PRESSED, MOVE UP ONE STEP.
    if (LIMIT_NEW3 == 0){TurnMotor(1,1,3);}                        //IF LIMIT SWITCH NOT PRESSED, MOVE UP ONE STEP.
    if (LIMIT_NEW4 == 0){TurnMotor(1,1,4);}                        //IF LIMIT SWITCH NOT PRESSED, MOVE UP ONE STEP.
    UpdateAllLimit();                                              //READ ALL THE LIMIT SWITCHES
    TEMP01 = (LIMIT_NEW1 + LIMIT_NEW2 + LIMIT_NEW3 + LIMIT_NEW4);  //MAKING A VARIABLE
    if (TEMP01 == 4){HOMEDONE1 = 1;}                               //IF ALL 4 SWITCHES ARE PRESSED AT ONCE, HOMING IS DONE
    if (TEMP01 < 4){                                               //IF LESS THAN 4 SWITCHES ARE PRESSED AT ONCE...
      if (LIMIT_NEW1 == 1){TurnMotor(0,1,1);}                      //IF THIS LIMIT SW IS PRESSED BUT NOT OTHERS, LOWER BY ONE STEP.
      if (LIMIT_NEW2 == 1){TurnMotor(0,1,2);}                      //IF THIS LIMIT SW IS PRESSED BUT NOT OTHERS, LOWER BY ONE STEP.
      if (LIMIT_NEW3 == 1){TurnMotor(0,1,3);}                      //IF THIS LIMIT SW IS PRESSED BUT NOT OTHERS, LOWER BY ONE STEP.
      if (LIMIT_NEW4 == 1){TurnMotor(0,1,4);}                      //IF THIS LIMIT SW IS PRESSED BUT NOT OTHERS, LOWER BY ONE STEP.
    }                                                              //END OF IF LESS THAN 4
    EAR_SAVER();
  }                                                                //END OF INITIAL HOMING
  Serial.println("INITIAL HOMING DONE!");
  while (HOMEDONE2 == 0){                                                     //THIS OFFSETS HOME BASED ON THE USER DEFINED OFFSETS
    TEMP01 = (MOT_OFFSET1 + MOT_OFFSET2 + MOT_OFFSET3 + MOT_OFFSET4);         //ADDS UP THE HOMING OFFSETS (THE REMAINING OFFSETS ON SUBSEQUENT LOOPS)
    if (TEMP01 == 0){HOMEDONE2 = 1;}                                          //IF THERE ARE NO MORE OFFSETS TO DO, HOMING IS DONE AND THE LOOP ENDS
    if (MOT_OFFSET1 > 0){TurnMotor(0,1,1);  MOT_OFFSET1 = (MOT_OFFSET1 - 1);} //IF NOT FULLY OFFSET, MOVE DOWN ONE STEP
    if (MOT_OFFSET2 > 0){TurnMotor(0,1,2);  MOT_OFFSET2 = (MOT_OFFSET2 - 1);} //IF NOT FULLY OFFSET, MOVE DOWN ONE STEP
    if (MOT_OFFSET3 > 0){TurnMotor(0,1,3);  MOT_OFFSET3 = (MOT_OFFSET3 - 1);} //IF NOT FULLY OFFSET, MOVE DOWN ONE STEP
    if (MOT_OFFSET4 > 0){TurnMotor(0,1,4);  MOT_OFFSET4 = (MOT_OFFSET4 - 1);} //IF NOT FULLY OFFSET, MOVE DOWN ONE STEP
    EAR_SAVER();
  }                                                                           //END OF OFFSETS APPLICATION FUNCTION
  M0T_NOW1 = 0; M0T_NOW2 = 0; M0T_NOW3 = 0; M0T_NOW4 = 0; //DEFINES CURRENT LOCATION AS 'HOME'
  Serial.println("OFFSETS APPLIED!");
}
INITIALIZE_CONFIRM = 0; HOMEDONE1 = 0; HOMEDONE2 = 0;       //RESET VARIABLE VALUES
Serial.println(" ");                                                    //PRINT A BLANK LINE TO THE TERMINAL
Serial.println("Motor # (1-4, 5 for all) , Steps , Dir (0=DOWN (HIGHER NUMBER),1=UP)"); //REPRINT INSTRUCTIONS
Serial.println("99 WILL START INITIALIZATION"); //REPRINT INSTRUCTIONS
}                                                         //END OF INITIALIZE FUNCTION





////const int STEP_HIGH_TIME = 300; //HOW MANY MICROSECONDS TO HOLD MOTOR STEP HIGH

void TurnMotor(int DIRR, int STEPP, int UNITT){ //DIR: 0=DOWN (INCREASING NUMBERS),1=UP (DECREASING NUMBERS)
                                                //STEP: NUMBER OF STEPS TO TAKE
                                                //UNIT: MOTOR NUMBER (1, 2, 3, 4...5 is ALL).
  if (DIRR == 1){
    if (UNITT == 1){
      digitalWrite(PIN_EN1,0);  //ENABLE MOTOR 1
      M0T_NOW1 = (M0T_NOW1 - STEPP);
      stepper1.moveTo(M0T_NOW1);
      stepper1.run();
    }

    if (UNITT == 2){
      digitalWrite(PIN_EN2,0);  //ENABLE MOTOR 2
      M0T_NOW2 = (M0T_NOW2 - STEPP);
      stepper2.moveTo(M0T_NOW2);
      stepper2.run();
    }

    if (UNITT == 3){
      digitalWrite(PIN_EN3,0);  //ENABLE MOTOR 1
      M0T_NOW3 = (M0T_NOW3 - STEPP);
      stepper3.moveTo(M0T_NOW3);
      stepper3.run();
    }

    if (UNITT == 4){
      digitalWrite(PIN_EN4,0);  //ENABLE MOTOR 1
      M0T_NOW4 = (M0T_NOW4 - STEPP);
      stepper4.moveTo(M0T_NOW4);
      stepper4.run();
    }

    if (UNITT == 5){
      digitalWrite(PIN_EN1,0);  //ENABLE MOTOR 1
      digitalWrite(PIN_EN2,0);  //ENABLE MOTOR 2
      digitalWrite(PIN_EN3,0);  //ENABLE MOTOR 3
      digitalWrite(PIN_EN4,0);  //ENABLE MOTOR 4
      M0T_NOW1 = (M0T_NOW1 - STEPP);
      M0T_NOW2 = (M0T_NOW2 - STEPP);
      M0T_NOW3 = (M0T_NOW3 - STEPP);
      M0T_NOW4 = (M0T_NOW4 - STEPP);
      stepper1.moveTo(M0T_NOW1);
      stepper2.moveTo(M0T_NOW2);
      stepper3.moveTo(M0T_NOW3);
      stepper4.moveTo(M0T_NOW4);
      stepper1.run();
      stepper2.run();
      stepper3.run();
      stepper4.run();
    }}
    
  if (DIRR == 0){
    if (UNITT == 1){
      digitalWrite(PIN_EN1,0);  //ENABLE MOTOR 1
      M0T_NOW1 = (M0T_NOW1 + STEPP);
      stepper1.moveTo(M0T_NOW1);
      stepper1.run();
    }

    if (UNITT == 2){
      digitalWrite(PIN_EN2,0);  //ENABLE MOTOR 2
      M0T_NOW2 = (M0T_NOW2 + STEPP);
      stepper2.moveTo(M0T_NOW2);
      stepper2.run();
    }

    if (UNITT == 3){
      digitalWrite(PIN_EN3,0);  //ENABLE MOTOR 1
      M0T_NOW3 = (M0T_NOW3 + STEPP);
      stepper3.moveTo(M0T_NOW3);
      stepper3.run();
    }

    if (UNITT == 4){
      digitalWrite(PIN_EN4,0);  //ENABLE MOTOR 1
      M0T_NOW4 = (M0T_NOW4 + STEPP);
      stepper4.moveTo(M0T_NOW4);
      stepper4.run();
    }

    if (UNITT == 5){
      digitalWrite(PIN_EN1,0);  //ENABLE MOTOR 1
      digitalWrite(PIN_EN2,0);  //ENABLE MOTOR 2
      digitalWrite(PIN_EN3,0);  //ENABLE MOTOR 3
      digitalWrite(PIN_EN4,0);  //ENABLE MOTOR 4
      M0T_NOW1 = (M0T_NOW1 + STEPP);
      M0T_NOW2 = (M0T_NOW2 + STEPP);
      M0T_NOW3 = (M0T_NOW3 + STEPP);
      M0T_NOW4 = (M0T_NOW4 + STEPP);
      stepper1.moveTo(M0T_NOW1);
      stepper2.moveTo(M0T_NOW2);
      stepper3.moveTo(M0T_NOW3);
      stepper4.moveTo(M0T_NOW4);
      stepper1.run();
      stepper2.run();
      stepper3.run();
      stepper4.run();
    }}
}




//INT FUNCTIONS GO HERE
void DIR_NOW(){ INPUT_DIR = digitalRead(PIN_DIR_IN); }  //TIED TO INT: CHANGES DIRECTION TO MATCH INPUT

void STEP_NOW(){ TurnMotor(INPUT_DIR,1,0); }  //TIED TO INT: MOVES ALL MOTORS BY 1 STEP
//INT FUNCTIONS GO HERE




//SHORTCUT FUNCTIONS GO HERE
void MotorEN(bool MENV){
  digitalWrite(PIN_EN1,MENV);
  digitalWrite(PIN_EN2,MENV);
  digitalWrite(PIN_EN3,MENV);
  digitalWrite(PIN_EN4,MENV);
  MOT_EN_NOW = MENV; }

void UpdateAllLimit(){ UpdateLimit(1);  UpdateLimit(2);  UpdateLimit(3);  UpdateLimit(4); }  //BASICALLY JUST A SHORTCUT

//SHORTCUT FUNCTIONS GO HERE

gjgsm...@gmail.com

unread,
Oct 30, 2020, 11:29:20 PM10/30/20
to accelstepper
I had a quick look at your code and I sorta see what you are trying to achieve, there is a lot going on in the loop which I think might be the problem. On each pass of the loop at each 'run' command, the steppers will step once, if required, but this might be held up with the updating of variables particularly via 'serial' data input. If I'm understanding it correctly, the main 'loop' could be stalled and the motors too, but they will still be drawing max current (amps) as set by the current limiting settings on the drivers. Depending on your motor supply voltage level (max 35V), ensuring that your drivers are limitted to the max 1A current would be essential, otherwise the drivers could be pulling more than the max allowed and possibly over-heating.

Not knowing all the details of your project, my perception of the complexity in your code may well be incorrect but, I'm thinking it could possibly be simplified somewhat. This is not a criticism, just an observation from my quick look at it.

Zack Kummer

unread,
Oct 31, 2020, 12:40:58 AM10/31/20
to accelstepper
I appreciate the analysis.

More project details are here, including the (sorta) working previous version without this library: https://hackaday.io/project/175647-laser-cutter-bed-leveling-system

I have the motors turned down to 1/2 power so safe there...maybe I should have said the drivers get noticeably warm indicating that they are on...that's more accurate than a general "hot" that could mean 'the higher end of spec' or 'on fire'.

So do I have this correct...if I'm at 0, and I use this:
stepper1.moveTo(200);
stepper1.run();
it will only move to 1, and I have to keep doing .run 200 times in a While statement?  Is there something I can use instead of .run to just do all the steps (keeping in mind that sometimes I need all the motors to step together)?  Or should I just do something like this?:
while (stepper1.distanceToGo() != 0){ stepper1.run();}
...or do I need to slow that down like:
while (stepper1.distanceToGo() != 0){
  stepper1.run();
  delay(25);

gjgsm...@gmail.com

unread,
Oct 31, 2020, 1:12:05 AM10/31/20
to accelstepper
Keep in mind that a stepper could be at any position (eg. 49 or 0 or 101 or 500 or -250) but will still go to your 200 target on;
stepper1.moveTo(200);
stepper1.run();

You can always establish stepper '0' position by going to a known predetermined position (maybe 'Home') and resetting the step position with;
stepper1.setCurrentPosition(0); 
Of course, step positions are always '0' on boot.

Here is a simple example for running four steppers to their targets once the target variables have been established.

void mov1() 
{
    stepperA.moveTo(A_target1);
    stepperB.moveTo(B_target1);
    stepperC.moveTo(C_target1);
    stepperD.moveTo(D_target1);
    
  while(stepperA.isRunning()|| stepperB.isRunning()|| stepperC.isRunning() || stepperD.isRunning())
    {
     stepperA.run();
     stepperB.run();
     stepperC.run();
     stepperD.run();
    }
 }

gjgsm...@gmail.com

unread,
Oct 31, 2020, 1:18:56 AM10/31/20
to accelstepper
Another example with conditional moves...

 void mov3()
{
    stepperA.moveTo(A_target4);
    stepperB.moveTo(B_target3);
    stepperC.moveTo(C_target3);
    stepperD.moveTo(D_target3);
    
    unsigned long startTimerMillis = millis();
    A_delay = 700;
    
   stepperA.setMaxSpeed(900);
   stepperC.setMaxSpeed(1000);
   
  while(stepperA.isRunning() || stepperB.isRunning() || stepperC.isRunning() || stepperD.isRunning())
     {
        if(millis() - startTimerMillis >= A_delay)  
        stepperA.run();
    
        if (stepperB.distanceToGo() == 0)
        stepperB.moveTo(B_target4); 
        stepperB.run();

//    if(stepperB.currentPosition() <= -150) stepperA.run();   // Another option.
  
        if (stepperC.distanceToGo() == 0)
        stepperC.moveTo(C_target4);
        stepperC.run();
    
      if (stepperD.distanceToGo() == 0)
        stepperD.moveTo(D_target4);
        stepperD.run();
     }

   stepperA.setMaxSpeed(1500);
   stepperC.setMaxSpeed(3000);
 }

Zack Kummer

unread,
Oct 31, 2020, 8:54:32 PM10/31/20
to accelstepper
GOT IT WORKING!

I could kiss you if there wasn't a pandemic!

Thank you so much!

gjgsm...@gmail.com

unread,
Oct 31, 2020, 11:32:55 PM10/31/20
to accelstepper
Hi Zack,

If you were in Australia that may well be possible as in the last 24 hours we have recorded 'zero'  Covid cases across the whole country.

Glad to hear that it's all working now.

Cheers
Geoff

Reply all
Reply to author
Forward
0 new messages