Indexing a nema 17 stepper and stopping it with home switch in void setup()
Hi, the code below only works if the flag is already located in the photoelectric sensor when I power on the machine.
However, when I power it on and there is no flag it indexes continuously without stopping even after I insert the flag.
The enaPin should disable the stepper motor based on the driver that I have it connected to so I don't understand what the problem is.
Can someone please help as to why it continuously indexes even after I've inserted the flag into the sensor?
I implemented this based on what I found on the forums
(basically at each step it should check the high/low status of the sensor):
for (int x = 0; x < OneRev && OpticalLimitSwitchValue == LOW; x++)
/*
Plate rotates and stops rotating once it hits photoelectric switch
*/
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int enaPin = 5;
const int shortPulse = 10;
const int stepIndexInterval = 4000;
const int OpticalLimitSwitch = 2;
const int OneRev = 6400;
void setup()
{
Serial.begin(9600);
// Sets pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enaPin, OUTPUT);
pinMode(OpticalLimitSwitch, INPUT); // LOW --> No flag , HIGH --> Flag
digitalWrite(enaPin, LOW);
digitalWrite(dirPin, HIGH); // Enables the motor to move in a CW direction
int OpticalLimitSwitchValue = digitalRead(OpticalLimitSwitch);
if ( OpticalLimitSwitchValue == LOW)
{
for (int x = 0; x < OneRev && OpticalLimitSwitchValue == LOW; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(shortPulse); // most stepper drivers just need a short pulse
digitalWrite(stepPin, LOW);
delayMicroseconds(stepIndexInterval); // this value is the interval between steps
}
}
else
{
digitalWrite(enaPin, HIGH);
}
delay(1000);
}
void loop()
{
}