Please help to perform a single operation before the endless loop in code

Ok- I have the following program I have written. It works pretty well but I would like it to run a sequence just once after tripping the limit switch the first time before getting into the endless loop of step forward and then step reverse 300 steps. I only want to use the crash/limit switch once per power cycle for reference and then have it move back 10-20 steps and then start the endless back and forth cycle until power is cut instead of hitting the limit and then cycling back and forth against the limit switch every time. I know it doesn't effect the code/function but I may reach the mechanical limits of the micro switch unnecessarily early.

I tried " numberOfSteps - 20 " in one direction but then it migrates out of range quickly as it goes 300 one way and 280 the other rather than 300/300 20 steps away from the contact switch.

Also, if anyone has any tips to slow a NEMA 17 stepper down without excessive noise and vibration I would love to hear it. I am using a TB6600 driver fwiw and I am running in full step mode and set the current to match the peak rated current of the motor. It steps very fast and smoothly at 500 microsecond pulse and 500 microsecond delay but running 1000/1000 is really noisy. I should be able to tweak the settings to get it to slow down equally smoothly and quietly but I cannot figure that out either. I had to set the button debounce very fast as the motor is moving so fasts it doesn't respond to its input otherwise.

Here is the program.



#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;


void setup()



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

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



}

void loop() {

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

  if (button.isPressed()) {
    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);

  }
}



In full step mode, steppers (and the driven mechanism) have resonances at certain step frequencies, which can lead to noise and vibration. Use microstepping to reduce that, or avoid stepping at those frequencies.

To do something once in a loop or function you can use a flag variable, declared static.

void loop() {
static byte first = 1;

if (first) {
do_something();
first = 0;
}
// rest of loop code
}

don't you need to check the limit switch after each step?

consider.


#define dirPin  12
#define stepPin 13

#define butPin  A1
#define lmtPin  A2


byte butLst;
bool run = false;
int  dir = 0;

const int  NumberOfSteps = 10;
int        steps         = 0;

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

    if  (run)  {
        Serial.println (steps);

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

        if (NumberOfSteps == ++steps || LOW == digitalRead (lmtPin))  {
            steps = 0;
            dir   = ! dir;
            digitalWrite  (dirPin, dir);
        }
    }
}

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

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

    pinMode  (lmtPin, INPUT_PULLUP);
    pinMode  (butPin, INPUT_PULLUP);
    butLst = digitalRead  (butPin);
}

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