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);
}
}