Please help me tweak my stepper motor program

I have read and researched and individually the sections work well but when I combine the elements I cannot make it function. I am using a NEMA 17 stepper motor and a 1660 stepper driver.

Basically, I intend for the stepper motor to rotate clockwise until the limit/crash switch at pin 7 is activated. Once this occurs the stepper motor should reverse direction counterclockwise and step 10 steps (to distance itself from the crash sensor and set a safe zero) and pause for one second thus completing the first state of the loop. Afterwards, the stepper motor will continue the rest of the loop sequence and go 300 steps counterclockwise and then 300 steps clockwise back and forth like this until the power is cut.

Here is the code:

#include <ezButton.h>

#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1

ezButton button(7);  // create ezButton object that attach to pin 7;
int loopState = LOOP_STATE_STOPPED;



#define dirPin 2
#define stepPin 3


int numberOfSteps = 300;
const int  initialSteps = 10;
int        steps         = 0;

void setup()




{
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);

  button.setDebounceTime(8); // set debounce time to 50 milliseconds



}

void loop() {
static byte first = 1;

if (first) {
byte butLst;
bool run = false;
int  dir = 0;


void loop  ()
{
    byte but = digitalRead  (button);
    if  (butLst != but)  {
        butLst = but;
        if  (LOW == but)
            run = ! run;
        delay  (10);     // debounce
    }

    if  (run)  {
 

        digitalWrite       (stepPin, HIGH);
        delay              (250);
        digitalWrite       (stepPin, LOW);
        delay              (250);

        if (initialSteps == ++steps || LOW == digitalRead (button))  {
            steps = 0;
            dir   = ! dir;
            digitalWrite  (dirPin, dir);

            delay (1000);
        }
    }
};
first = 0;
}

  button.loop(); // MUST call the loop() function first

  if (button.isReleased()) {
    if (loopState == LOOP_STATE_STOPPED)
      loopState = LOOP_STATE_STARTED;
    else // if(loopState == LOOP_STATE_STARTED)
      loopState = LOOP_STATE_STOPPED;
  }

  if (loopState == LOOP_STATE_STARTED) {

    // turn stepper on:

    for (int n = 0; n < numberOfSteps; n++) {
      digitalWrite(dirPin, LOW);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);

    }


    for (int n = 0; n < numberOfSteps; n++) {
      digitalWrite(dirPin, HIGH);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);

    }

  }

  if (loopState == LOOP_STATE_STOPPED) {
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);

  }
}

Your code contains loop() procedure twice. You have to merge this two subroutines in one.

Did you try to compile the code?
Compiler test it and highlights the other problems, if any.

You code does not compile. However, here are some issues I see already.

The following doesn't make any sense because button is an ezButton object and NOT a pin! Also, butLst is a local variable and is destroyed every time loop() runs.

      byte but = digitalRead  (button);
      if  (butLst != but)  {
        butLst = but;
        if  (LOW == but)
          run = ! run;
        delay  (10);     // debounce
      }

Why are you delaying 250ms between steps?

        digitalWrite       (stepPin, HIGH);
        delay              (250);
        digitalWrite       (stepPin, LOW);
        delay              (250);

You don't have to set the dirPin every time through the for loop. Just set it before you enter the for loop.

    for (int n = 0; n < numberOfSteps; n++) {
      digitalWrite(dirPin, LOW);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }

That makes sense! I modified the ezbutton code to fit my limited pin understanding. The delay time adjusts the speed of the stepper motor I believe. I will try your suggestion of using n< instead of the n++

Yes I know it won’t compile, I was trying to share that to give a general feel for my approach.

With a 250ms delay the motor would only move 2 steps in 1 second!

Not sure what you are talking about here. I don't see where I suggested that.

Ok, I figured it out. I ended up starting over from scratch. I did have a question someone may be able to help me with.

I set my stepper driver to max microstepping (1/32) to cut down the vibration and noise.

I ran it for about an hour and it gradually is losing steps. It didn't conflict as I watched it but it seems like it would. I plan to run it 8-10 hours at a time unattended and if it gets too far out of range it will keep banging against the limit switch or another obstacle for hours and prob ruin something.

Is there an easy way to make the arduino reset itself every hour through coding so it will start over the homing procedure?

A common indication of lack of power. Either too much load on the motor of current setting too low on the controller.

nice! I can definitely go higher power as I am only at 0.5 amps current and 0.7 amps peak. 0.5/0.7

The driver can go to 3.5/4.0 amps. I didnt want to create excess heat or burn up the motor but maybe it will only take what it needs? It is rated at 1.0 amp/phase. The next setting is 1.0/1.2. I was worried about the 1.2 peak rating burning it up.

If a stepper motor is working properly, it will be quite hot. So the next setting would be fine. The motor should not be fully enclosed so air cannot flow around it.

The stepper-motor will take as much as he can get including fast overheating and catching fire if the voltage and the resulting current is high enough.

You have to limit the current with the stepper-motor-driver to the specified value that is written in the stepper-motors datasheet.

If you are loosing steps this means your load is too much for this stepper-motor
or the acceleration is too high.

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