Hi, regarding with my subject above, I want to ask something about the mistake or problem that exists in my coding below.
Here are the items that I use:
2 28BYJ-48 stepper motor + 2 ULN2003 driver
1 Nema 14 stepper motor + A4988 driver
1 push button + 1 LED
The program or the system I want to make is like a pipe scanner.
The system start when you press push button, and then the LED will start light up and so the Nema 14 will rotate forward and reverse (in defined distance) and after that both 28BYJ-48 will start rotate froward in CW and CCW respectively. I want to make the 28BYJ-48 to step or rotate about 50 degree rotation (I just set it 500 in coding, it might be wrong but its okay). The idea is to make the system keep working with LED is lighten up and Nema 14 rotate and then also 28BYJ-48 rotating until I press the push button again which make it stop working.
So, the problem here is, the system stop working after only ONE loop or cycle.
My question is how to make it keep moving until I press the push button again to make it stop?
/*
#include <AccelStepper.h>
/*-----( Declare Constants and Pin Numbers )-----*/
/*declare define for 28byj-48 stepper motor*/
#define dirPin 2 // Nema 14 direction pin
#define stepPin 3 // Nema 14 step pin
//push button and LED
#define buttonPin 12 // push button
#define ledPin 13 // LED
/*-----( Declare objects )-----*/
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
AccelStepper stepper3(1,stepPin, dirPin);
/*-----( Declare Variables )-----*/
int ledState = LOW;
int buttonState;
int previous = LOW;
long time = 0;
long debounce = 200;
void setup() /****** SETUP: RUNS ONCE ******/
{
stepper1.setMaxSpeed(2000.0);
stepper1.setAcceleration(8000.0);
stepper1.setSpeed(1000);
stepper2.setMaxSpeed(2000.0);
stepper2.setAcceleration(8000.0);
stepper2.setSpeed(1000);
stepper3.setMaxSpeed(1000.0);
stepper3.setAcceleration(1000.0);
// Sets the two pins (A4988 driver) as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(buttonPin, INPUT); // set push button as input
pinMode(ledPin, OUTPUT); // set LED as output
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH && previous == LOW && millis() - time > debounce)
{
if(ledState == HIGH)
{
ledState = LOW;
if (stepper1.distanceToGo() == 0 && stepper2.distanceToGo() == 0)
{
stepper1.move(500); // rotate about 50 degree CW
stepper2.move(-500); // rotate about 50 degree CCW
digitalWrite(dirPin,LOW); //Enables the motor to move the pulley(front view) to the left
// Makes 1300 pulses for making full six and half cycle rotation (300 mm distance)
for(int x = 0; x < 1300; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(200);
digitalWrite(dirPin,HIGH); // Enables the motor to move the pulley(front view) to the right
// Makes 1300 pulses for making full six and half cycle rotation (300 mm distance)
for(int x = 0; x < 1300; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
} else {
ledState = HIGH;
}
time = millis();
}
digitalWrite(ledPin, ledState);
stepper1.run();
stepper2.run();
previous == buttonState;
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//none
//*********( THE END )***********