This sketch is going to be added to Jim Larsen's great sketch he did for my motorized Z axis on my lathe.
This is for a Travel Limit switch to stop the Z axis motor. I have a heavy NEMA 34 motor on the Z axis and
it could do a lot of damage if it uncontrollably hits my chuck or tailstock. I could use (stop) but with that
the coils are left energized and I want the coils to be deenergized.
Today I was looking at setEnablePin(); which appears would work if I was a programmer and knew how to use it.
I "think" I made your suggested changes but the motor does not run.
While the motor does not run Serial Monitor does reflect the button push:
1++++0
1++++0
1++++0
1++++0
1++++0
1++++0
1++++0
With button push becomes:
0++++0
0++++0
0++++0
0++++0
0++++0
0++++0
0++++0
#include <AccelStepper.h>
const int dirPin = 6;
const int stepPin = 7;
const int ENA = 4;
int Limit_1 = A5;
AccelStepper stepper (AccelStepper::DRIVER, stepPin, dirPin); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
void setup()
{
pinMode(Limit_1,INPUT_PULLUP); // Limit Switch needs to be 'pulled' LOW (connected to GND) to operate
//pinMode(Limit_1,INPUT); // Limit Switch needs to be 'pulled' LOW (connected to GND) to operate
pinMode(ENA,OUTPUT); // ENA, enable pin is set to OUTPUT
Serial.begin(115200);
stepper.setMaxSpeed(5000); // this limits the value of setSpeed(). Raise it if you like. 15000 does not work
stepper.setSpeed(1000); // [12495 works with METRO 12500 does not work]
//digitalWrite(ENA, LOW); // ENA pin is switched LOW, in code to initially stop the stepper from moving.
digitalWrite(ENA, HIGH);
}
void loop()
{
if (digitalRead(Limit_1) == LOW) // First 'if', Limit Switch is operated and pin input is grounded or LOW
{
//digitalWrite(ENA, HIGH); // Switch ENA Pin High, Stepper Runs
digitalWrite(ENA, LOW);
Serial.print(digitalRead(Limit_1));
Serial.print("++++");
Serial.println(digitalRead(ENA));
}
if (digitalRead(Limit_1) == HIGH) // Second 'if', Limit Switch is operated and pin input is 'PULLED UP' or HIGH.
// Basically not connected because you told the Limit-1 pin to be INPUT_PULLUP
{
digitalWrite(ENA, LOW); // Switch ENA Pin LOW Stepper Stops
Serial.print(digitalRead(Limit_1));
Serial.print("++++");
Serial.println(digitalRead(ENA));
}
stepper.runSpeed(); // This will run the motor dependant on which 'if' statement is active.
}