Hi arduino expert,
I using two stepper motor to make the scanner and driver A4988 .My intention as below :
- Press tact switch - stepper motor 1 run left then hit limit switch left, motor turn to the right then hit limit switch right, moto turn left back. This moving left and right continuosly with limit switch as input to change direction.
- Press tact switch again - stepper motor 1 stop moving or back to initial position. After that, stepper motor 2 moving 1000 step, then stop the stepper motor 2. Program end.
My problem is stepper motor 2 not stop moving after 1000 step but repeat the process in else loop. Need your help and advise from you guys how to stop the stepper motor 2 then end the program.
here I attach my source code.
// defines pins numbers
const int stepPin = 5; //step stepper motor 1
const int dirPin = 6; //direction stepper motor 1
const int stepPin1 = 8; //step stepper motor 2
const int dirPin1 =9; //direction stepper motor 2
const byte sw1 = 2; //tact switch
const byte limitsw = 3; //limit switch as input to change direction
int enableout = 7; //pin enable stepper motor 1
int enableoutsatu = 10; //pin enable stepper motor 2
volatile byte state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = HIGH; // the previous reading from the input pin
long time = 0; // the last time the output pin was toggled
long debounce = 50; // the debounce time, increase if the output flickers
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(stepPin1,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(sw1,INPUT);
pinMode(limitsw,INPUT);
pinMode(enableout,OUTPUT);
pinMode(enableoutsatu,OUTPUT);
attachInterrupt(digitalPinToInterrupt(sw1), toggle, CHANGE);
}
void loop(){
if (state == LOW){
digitalWrite(enableout,LOW); //output stepper motor 1 enable
digitalWrite(enableoutsatu,HIGH); //output stepper motor 2 disable
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; digitalRead(limitsw) == HIGH || x < 200 ; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(100); // One second delay
digitalWrite(dirPin,HIGH); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; digitalRead(limitsw) == HIGH || x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(100);
}
else{
digitalWrite(enableout,HIGH); //disable output stepper motor 1, motor stop
delay(1000);
for(int i = 0; i < 3000 ; i++) { //set total step 3000
digitalWrite(enableoutsatu,LOW); // enable output stepper motor 2
digitalWrite(dirPin1,HIGH); // moving to the left
if( i >= 0 && i<=1000){ // set total step to moving 1000
digitalWrite(stepPin1,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin1,LOW);
delayMicroseconds(1000);
}
else if(i == 1000){ // set if total step more that 1000, stop the stepper motor 2
digitalWrite(enableoutsatu,HIGH); //disable output stepper motor 2, motor stop
break; //stop the loop
}
}
}
}
void toggle() {
reading = digitalRead(sw1);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
previous = reading;
}
stepper_motor_scanner.ino (3.33 KB)