The following code works until insert:
** lim1_st = digitalRead(lim1);**
** if(lim1_st == LOW);{**
** x = stepsPerRevolution;**
** }**
My intention is have a limit switch stop the stepper motor.
When I comment out just : x = stepsPerRevolution;
It works.
So that tells me for some reason x is getting set to stepsPerRevolution and making it stop.
But the pin stays high the entire time so I don't know why it enters that if statement and makes x = stepsPerRevolution;.
Thank you for your time.
// Define pin connections & motor's steps per revolution
const int dirPin = 10;
const int stepPin = 9;
const int lim1 = 2;
const int lim2 = 3;
const int remote = 1;
const int drvEN = 0;
const int stepsPerRevolution = 400;
int lim1_st = 0; //Button state initial value
int lim2_st = 0; //Button state initial value
int flag1 = 1; //flag to keep track of button presses
int flag2 = 0;
int remote_st = 0;
void setup()
{
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(lim1, INPUT);
pinMode(lim2, INPUT);
pinMode(remote, INPUT);
pinMode(drvEN, OUTPUT);
digitalWrite(drvEN, HIGH);
}
void loop()
{
remote_st = digitalRead(remote);
if(remote_st == LOW) {
if(flag1 == 1){
digitalWrite(drvEN,LOW);
// Set motor direction clockwise
digitalWrite(dirPin, HIGH);
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
lim2_st = digitalRead(lim2);
if(lim2_st == LOW);{
x = stepsPerRevolution;
}
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
digitalWrite(drvEN,HIGH);
delay(1000); // Wait a second
flag1 = 2;
}
}
remote_st = digitalRead(remote);
if(remote_st == LOW) {
if(flag1 == 2) {
digitalWrite(drvEN,LOW);
// Set motor direction counterclockwise
digitalWrite(dirPin, LOW);
// Spin motor quickly
for(int x = 0; x < stepsPerRevolution; x++)
{
lim1_st = digitalRead(lim1);
if(lim1_st == LOW);{
x = stepsPerRevolution;
}
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
digitalWrite(drvEN,HIGH);
delay(1000); // Wait a second
flag1 = 1;
}
}
}