I am confused on why my code is skipping a couple of lines. Below is a set of bullet points its supposed to do. But when I run my code it skips steps 4 and 7. I'm not sure why its skipping.
- Start Button is hit to start Loop
- Stepper goes until LimitSwitch2 is hit at fast speed
- Waits a second
- Stepper drives down a certain distance (1ft) at fast speed
- Stepper then drives down until LimitSwitch1 is hit at slower speed
- Waits a second
- Stepper then reverses a certain distance (1/2in) at slow speed
- Stepper then reverses until LimitSwitch2 is hit at fast speed
- End loop
I have an arduino uno, 2 Nema 23 motors and 2 TB6600 driver.
My Code:
// Define stepper motor connections and steps per revolution:
#define dirPin1 3
#define stepPin1 2
#define dirPin2 5
#define stepPin2 4
#define stepsPerRevolution 3200
#define limitPin1 8
#define limitPin2 9
#define startPin 10
#define downPin 6
#define upPin 7
void setup() {
// Declare pins as output:
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(limitPin1, INPUT);
pinMode(limitPin2, INPUT);
pinMode(startPin, INPUT);
pinMode(downPin, INPUT);
pinMode(upPin, INPUT);
}
void loop() {
////////////////////////////////////////////////////// Auto Movement
{ if(digitalRead(startPin) == LOW)
{ for (int i = 0; i < stepsPerRevolution*50000 && digitalRead(limitPin2); i++) {
// Motor 1
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin1, LOW);
delayMicroseconds(5);
// Motor 2
digitalWrite(dirPin2, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin2, LOW);
delayMicroseconds(5);
}
delay(1000);
// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < stepsPerRevolution*30000; i++) {
// Motor 1
digitalWrite(dirPin1, LOW);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin1, LOW);
delayMicroseconds(5);
// Motor 2
digitalWrite(dirPin2, LOW);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin2, LOW);
delayMicroseconds(5);
}
delay(1000);
for (int i = 0; i < stepsPerRevolution*50000 && digitalRead(limitPin1); i++) {
// Motor 1
digitalWrite(dirPin1, LOW);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(15);
digitalWrite(stepPin1, LOW);
delayMicroseconds(15);
// Motor 2
digitalWrite(dirPin2, LOW);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(15);
digitalWrite(stepPin2, LOW);
delayMicroseconds(15);
}
delay(1000);
for (int i = 0; i < stepsPerRevolution; i++) {
// Motor 1
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(15);
digitalWrite(stepPin1, LOW);
delayMicroseconds(15);
// Motor 2
digitalWrite(dirPin2, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(15);
digitalWrite(stepPin2, LOW);
delayMicroseconds(15);
}
delay(1000);
for (int i = 0; i < stepsPerRevolution*50000 && digitalRead(limitPin2); i++) {
// Motor 1
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin1, LOW);
delayMicroseconds(5);
// Motor 2
digitalWrite(dirPin2, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin2, LOW);
delayMicroseconds(5);
}}}
//////////////////////////////////////// Maunal Movement
{ if(digitalRead(downPin) == LOW) {
// Motor 1
digitalWrite(dirPin1, HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(7);
digitalWrite(stepPin1, LOW);
delayMicroseconds(7);
// Motor 2
digitalWrite(dirPin2, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(7);
digitalWrite(stepPin2, LOW);
delayMicroseconds(7);
}
if(digitalRead(upPin) == LOW) {
// Motor 1
digitalWrite(dirPin1, LOW);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(7);
digitalWrite(stepPin1, LOW);
delayMicroseconds(7);
// Motor 2
digitalWrite(dirPin2, LOW);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(7);
digitalWrite(stepPin2, LOW);
delayMicroseconds(7);
}}}